-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (69 loc) · 2.19 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
package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
"github.com/quochungphp/go-test-assignment/src/domain/auth"
"github.com/quochungphp/go-test-assignment/src/domain/tasks"
"github.com/quochungphp/go-test-assignment/src/domain/users"
"github.com/quochungphp/go-test-assignment/src/infrastructure/middlewares"
"github.com/quochungphp/go-test-assignment/src/infrastructure/pg_driver"
"github.com/quochungphp/go-test-assignment/src/pkgs/settings"
"github.com/tylerb/graceful"
)
func main() {
// Init postgresql
pgSession, err := pg_driver.Setup(pg_driver.DBConfiguration{
Driver: os.Getenv(settings.DbDriver),
Host: os.Getenv(settings.PgHost),
Port: os.Getenv(settings.PgPort),
Database: os.Getenv(settings.PgDB),
User: os.Getenv(settings.PgUser),
Password: os.Getenv(settings.PgPass),
})
if err != nil {
panic(err)
}
// Init Gorilla Router
router := mux.NewRouter()
// User create action
userCreatAction := users.UserCreateAction{pgSession}
userCtrl := users.UserController{
userCreatAction,
}
userRouter := router.PathPrefix("/users").Subrouter()
userRouter.HandleFunc("", userCtrl.Create).Methods("POST").Name("UserCreateAction")
// Login action
authLoginAction := auth.AuthLoginAction{pgSession}
authCtrl := auth.AuthController{
authLoginAction,
}
authRouter := router.PathPrefix("/auth").Subrouter()
authRouter.HandleFunc("/login", authCtrl.Login).Methods("POST").Name("AuthLoginAction")
// Create task action
createTaskAction := tasks.TaskCreateAction{pgSession}
taskCtrl := tasks.TaskController{
createTaskAction,
}
taskRouter := router.PathPrefix("/tasks").Subrouter()
taskRouter.HandleFunc("/create", taskCtrl.Create).Methods("POST").Name("TaskCreateAction")
taskRouter.Use(middlewares.AuthMiddleware)
srv := &graceful.Server{
Timeout: 5 * time.Second,
BeforeShutdown: func() bool {
pgSession.Close()
fmt.Printf("shutting down database connection")
return true
},
Server: &http.Server{
Addr: ":" + os.Getenv(settings.Port),
Handler: router,
},
}
fmt.Printf("Server is runing port: %v\n", os.Getenv(settings.Port))
if err := srv.ListenAndServe(); err != nil {
fmt.Errorf("while start server")
}
}