main.*application.initModels   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
nop 0
1
package main
2
3
import (
4
	"fmt"
5
	"log"
6
	"net/http"
7
	"os"
8
	"runtime/debug"
9
10
	"github.com/gin-gonic/gin"
11
12
	"github.com/rfinochi/golang-workshop-todo/pkg/common"
13
	"github.com/rfinochi/golang-workshop-todo/pkg/models"
14
	"github.com/rfinochi/golang-workshop-todo/pkg/models/google"
15
	"github.com/rfinochi/golang-workshop-todo/pkg/models/memory"
16
	"github.com/rfinochi/golang-workshop-todo/pkg/models/mongo"
17
)
18
19
type application struct {
20
	errorLog  *log.Logger
21
	infoLog   *log.Logger
22
	router    *gin.Engine
23
	itemModel *models.ItemModel
24
}
25
26
func (app *application) initModels() {
27
	app.itemModel = &models.ItemModel{}
28
29
	repositoryType := os.Getenv(common.RepositoryEnvVarName)
30
31
	if repositoryType == common.RepositoryMongo {
32
		app.itemModel.Repository = &mongo.ItemRepository{}
33
	} else if repositoryType == common.RepositoryGoogle {
34
		app.itemModel.Repository = &google.ItemRepository{}
35
	} else {
36
		app.itemModel.Repository = &memory.ItemRepository{}
37
	}
38
}
39
40
func (app *application) notFound(w http.ResponseWriter) {
41
	app.clientError(w, http.StatusNotFound)
42
}
43
44
func (app *application) conflict(w http.ResponseWriter, statusText string) {
45
	http.Error(w, statusText, http.StatusConflict)
46
}
47
48
func (app *application) serverError(w http.ResponseWriter, err error) {
49
	trace := fmt.Sprintf("%s\n%s", err.Error(), debug.Stack())
50
	app.errorLog.Output(2, trace)
51
52
	http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
53
}
54
55
func (app *application) clientError(w http.ResponseWriter, status int) {
56
	http.Error(w, http.StatusText(status), status)
57
}
58