-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (100 loc) · 3.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"net/http"
"net/http/pprof"
"github.com/go-chi/chi"
"github.com/hashmup/QuestionBankAPI/src/application"
"github.com/hashmup/QuestionBankAPI/src/config"
"github.com/hashmup/QuestionBankAPI/src/infrastructure/persistence"
"github.com/hashmup/QuestionBankAPI/src/interfaces/class"
"github.com/hashmup/QuestionBankAPI/src/interfaces/folder"
"github.com/hashmup/QuestionBankAPI/src/interfaces/question"
"github.com/hashmup/QuestionBankAPI/src/interfaces/school"
"github.com/hashmup/QuestionBankAPI/src/interfaces/session"
"github.com/hashmup/QuestionBankAPI/src/interfaces/user"
"github.com/justinas/alice"
"github.com/kelseyhightower/envconfig"
)
func main() {
h := initHandlers()
http.ListenAndServe(":9000", h)
}
func initHandlers() http.Handler {
// Routing
r := chi.NewRouter()
// middleware chain
chain := alice.New(
// loggingMiddleware,
// middleware.AccessControl,
// middleware.Authenticator,
)
// DB connection
var dbConfig config.DBConfig
err := envconfig.Process("qb", &dbConfig)
if err != nil {
panic(err)
}
dbConn, err := config.NewDBConnection(dbConfig)
if err != nil {
panic(err)
}
// Redis Connection
var redisConfig config.RedisConfig
err = envconfig.Process("qb", &redisConfig)
if err != nil {
panic(err)
}
redisConn, err := config.NewRedisConnection(redisConfig)
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", dbConn)
fmt.Printf("%#v\n", redisConn)
classRepo := persistence.NewClassRepository(dbConn)
schoolRepo := persistence.NewSchoolRepository(dbConn)
schoolService := application.NewSchoolService(schoolRepo, classRepo)
schoolDependency := &school.Dependency{
SchoolService: schoolService,
}
r = school.MakeSchoolHandler(schoolDependency, r)
userRepo := persistence.NewUserRepository(dbConn)
userService := application.NewUserService(userRepo)
userDependency := &user.Dependency{
UserService: userService,
}
r = user.MakeUserHandler(userDependency, r)
sessionRepo := persistence.NewSessionRepository(dbConn, redisConn)
sessionService := application.NewSessionService(sessionRepo, userRepo)
sessionDependency := &session.Dependency{
SessionService: sessionService,
}
r = session.MakeSessionHandler(sessionDependency, r)
classService := application.NewClassService(classRepo)
classDependency := &class.Dependency{
ClassService: classService,
SessionService: sessionService,
}
r = class.MakeClassHandler(classDependency, r)
folderRepo := persistence.NewFolderRepository(dbConn)
folderService := application.NewFolderService(folderRepo)
folderDependency := &folder.Dependency{
FolderService: folderService,
SessionService: sessionService,
}
r = folder.MakeFolderHandler(folderDependency, r)
questionRepo := persistence.NewQuestionRepository(dbConn)
questionService := application.NewQuestionService(questionRepo)
questionDependency := &question.Dependency{
QuestionService: questionService,
SessionService: sessionService,
}
r = question.MakeQuestionHandler(questionDependency, r)
r.HandleFunc("/debug/pprof/", pprof.Index)
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.HandleFunc("/debug/pprof/trace", pprof.Trace)
h := chain.Then(r)
return h
}