-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (142 loc) · 4.14 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"encoding/json"
"flag"
"github.com/Kyuubang/shopiea/db"
"github.com/Kyuubang/shopiea/handlers"
"github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"net/http"
"os"
"time"
)
var (
migrate bool
)
func init() {
// set flag migrate to run database migration
flag.BoolVar(&migrate, "migrate", false, "run database migration")
// set gin mode
if os.Getenv("SHOPIEA_MODE") == "release" {
gin.SetMode(gin.ReleaseMode)
} else {
gin.SetMode(gin.DebugMode)
}
}
func main() {
// init db
var conn = db.Config{
Host: os.Getenv("SHOPIEA_DB_HOST"),
Port: os.Getenv("SHOPIEA_DB_PORT"),
User: os.Getenv("SHOPIEA_DB_USER"),
Password: os.Getenv("SHOPIEA_DB_PASSWORD"),
DBName: os.Getenv("SHOPIEA_DB_NAME"),
TZ: os.Getenv("SHOPIEA_TZ"),
}
err := conn.InitDB(migrate)
if err != nil {
panic(err)
}
// init router
var router *gin.Engine
// check if environment is production
if os.Getenv("SHOPIEA_ENV") == "production" {
// create a new logger
logger, _ := zap.NewProduction()
defer logger.Sync() // flushes buffer, if any
// create a new Gin engine
router = gin.New()
// add the JSON logging middleware to the router
router.Use(ginzap.Ginzap(logger, time.RFC3339, true))
} else {
router = gin.Default()
}
// authentication endpoints
// handlers for login user
router.POST("/auth/login", handlers.Login)
// handlers for anonymous function config
router.GET("/info", func(c *gin.Context) {
// json object that include date, config
data, err := json.Marshal(map[string]interface{}{
"date": time.Now().Format(time.RFC822),
"version": "1.0.0",
"config": map[string]string{
"case_repo": os.Getenv("SHOPIEA_CASE_REPO"),
"case_branch": os.Getenv("SHOPIEA_CASE_BRANCH"),
"infra_repo": os.Getenv("SHOPIEA_INFRA_REPO"),
"infra_branch": os.Getenv("SHOPIEA_INFRA_BRANCH"),
},
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "error when marshal json",
})
return
}
// return data json object to client with status code 200
c.Data(http.StatusOK, "application/json", data)
return
})
// handlers versioning - version 1
apiV1 := router.Group("/v1", handlers.AuthMiddleware())
{
// handlers for get all course
apiV1.GET("/course", handlers.GetCourses)
// handlers for get all labs
apiV1.GET("/labs", handlers.GetLabs)
// handlers for push score
apiV1.POST("/score", handlers.PushScore)
// handlers for get score
apiV1.GET("/score", handlers.GetScore)
// handlers for check token with middleware
apiV1.POST("/auth/check", handlers.CheckToken)
// admin handlers
admin := apiV1.Group("/admin", handlers.AdminOnly())
{
// handlers for check admin
admin.POST("/check", handlers.CheckToken)
// handlers for get all user
admin.GET("/user", handlers.GetUsers)
// handlers for create user
admin.POST("/user", handlers.CreateUser)
// handlers for delete user
admin.DELETE("/user", handlers.DeleteUser)
// handlers for update user
admin.PUT("/user", handlers.UpdateUser)
// handlers for get all class
admin.GET("/class", handlers.GetClasses)
// handlers for create class
admin.POST("/class", handlers.CreateClass)
// handlers for delete class
admin.DELETE("/class", handlers.DeleteClass)
// handlers for update class
admin.PUT("/class", handlers.UpdateClass)
// handlers for create course
admin.POST("/course", handlers.CreateCourse)
// handlers for update course
admin.PUT("/course", handlers.UpdateCourse)
// handlers for delete course
admin.DELETE("/course", handlers.DeleteCourse)
// handlers for create labs
admin.POST("/labs", handlers.CreateLabs)
// handlers for update labs
admin.PUT("/labs", handlers.UpdateLabs)
// handlers for delete labs
admin.DELETE("/labs", handlers.DeleteLabs)
// handlers for export score
admin.GET("/export", handlers.ExportScore)
}
}
// create http server on port 8080
server := &http.Server{
Addr: ":9898",
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
err = server.ListenAndServe()
if err != nil {
panic(err)
}
}