-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
286 lines (264 loc) · 15.9 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"context"
"fmt"
"gateway/consul"
consulagent "gateway/consul/agent"
"gateway/entity/validator"
"gateway/handler"
"gateway/middleware"
announcementproto "gateway/proto/golang/announcement"
authproto "gateway/proto/golang/auth"
clubproto "gateway/proto/golang/club"
outingproto "gateway/proto/golang/outing"
scheduleproto "gateway/proto/golang/schedule"
customrouter "gateway/router"
"gateway/subscriber"
"gateway/tool/env"
customlogrus "gateway/tool/logrus"
topic "gateway/utils/topic/golang"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/hashicorp/consul/api"
grpccli "github.com/micro/go-micro/v2/client/grpc"
"github.com/micro/go-micro/v2/client/selector"
"github.com/sirupsen/logrus"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
"log"
"net/http"
"os"
"time"
)
// start profiling in this package init function (add in v.1.0.2)
import _ "gateway/tool/profiling"
func main() {
// create consul connection & consul agent
consulCfg := api.DefaultConfig()
consulCfg.Address = env.GetAndFatalIfNotExits("CONSUL_ADDRESS") // change how to get env from local in v.1.0.2
consulCli, err := api.NewClient(consulCfg)
if err != nil {
log.Fatalf("unable to connect consul agent, err: %v", err)
}
consulAgent := consulagent.Default(
consulagent.Strategy(selector.RoundRobin),
consulagent.Client(consulCli),
consulagent.Services([]consul.ServiceName{topic.AuthServiceName, topic.ClubServiceName, // add in v.1.0.2
topic.OutingServiceName, topic.ScheduleServiceName, topic.AnnouncementServiceName}),
)
// create jaeger connection
jaegerAddr := env.GetAndFatalIfNotExits("JAEGER_ADDRESS")
apiTracer, closer, err := jaegercfg.Configuration{
ServiceName: "DMS.SMS.v1.api.gateway", // add const in topic
Reporter: &jaegercfg.ReporterConfig{LogSpans: true, LocalAgentHostPort: jaegerAddr},
Sampler: &jaegercfg.SamplerConfig{Type: jaeger.SamplerTypeConst, Param: 1},
}.NewTracer()
if err != nil {
log.Fatalf("error while creating new tracer for service, err: %v", err)
}
defer func() {
_ = closer.Close()
}()
// create aws session (add in v.1.0.2)
awsId := env.GetAndFatalIfNotExits("SMS_AWS_ID")
awsKey := env.GetAndFatalIfNotExits("SMS_AWS_KEY")
s3Region := env.GetAndFatalIfNotExits("SMS_AWS_REGION")
awsSession, err := session.NewSession(&aws.Config{
Region: aws.String(s3Region),
Credentials: credentials.NewStaticCredentials(awsId, awsKey, ""),
})
// create redis client (add in v.1.0.3)
redisConf, err := consulAgent.GetRedisConfigFromKV("redis/gateway/local")
if err != nil {
log.Fatalf("unable to get redis connection config from consul KV, err: %v", err)
}
redisCli := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", redisConf.Host, redisConf.Port),
DB: redisConf.DB,
})
if err := redisCli.Ping(context.Background()).Err(); err != nil {
log.Fatalf("unable to connect to redis server, connection config: %v, err: %v", redisConf, err)
}
defer func() {
_ = redisCli.Close()
}()
// gRPC service client
gRPCCli := grpccli.NewClient()
authSrvCli := authproto.NewAuthService(topic.AuthServiceName, gRPCCli)
clubSrvCli := clubproto.NewClubService(topic.ClubServiceName, gRPCCli)
outingSrvCli := outingproto.NewOutingService("", gRPCCli)
scheduleSrvCli := scheduleproto.NewScheduleSrv("schedule", gRPCCli)
announcementSrvCli := announcementproto.NewAnnouncementSrv("announcement", gRPCCli)
// create http request & event handler
defaultHandler := handler.Default(
handler.ConsulAgent(consulAgent),
handler.Validate(validator.New()),
handler.Tracer(apiTracer),
handler.AWSSession(awsSession),
handler.RedisClient(redisCli),
handler.Location(time.UTC),
handler.AuthService(authSrvCli),
handler.ClubService(clubSrvCli),
handler.OutingService(outingSrvCli),
handler.ScheduleService(scheduleSrvCli),
handler.AnnouncementService(announcementSrvCli),
)
// create subscriber & register aws sqs, redis listener (add in v.1.0.2)
//consulChangeQueue := env.GetAndFatalIfNotExits("CHANGE_CONSUL_SQS_GATEWAY")
redisDelTopic := env.GetAndFatalIfNotExits("REDIS_DELETE_TOPIC")
redisSetTopic := env.GetAndFatalIfNotExits("REDIS_SET_TOPIC")
subscriber.SetAwsSession(awsSession)
subscriber.SetRedisClient(redisCli)
defaultSubscriber := subscriber.Default()
//defaultSubscriber.RegisterBeforeStart(
// subscriber.SqsQueuePurger(consulChangeQueue),
//)
defaultSubscriber.RegisterListeners(
//subscriber.SqsMsgListener(consulChangeQueue, defaultHandler.ChangeConsulNodes, &sqs.ReceiveMessageInput{
// MaxNumberOfMessages: aws.Int64(10),
// WaitTimeSeconds: aws.Int64(2),
//}),
subscriber.RedisListener(redisDelTopic, defaultHandler.DeleteAssociatedRedisKey, 5), // add in v.1.0.3
subscriber.RedisListener(redisSetTopic, defaultHandler.SetRedisKeyWithResponse, 5), // add in v.1.0.4
)
// create logger & add hooks
if _, err := os.Stat("/usr/share/filebeat/log/dms-sms"); os.IsNotExist(err) {
if err = os.MkdirAll("/usr/share/filebeat/log/dms-sms", os.ModePerm); err != nil { log.Fatal(err) }
}
authLogger := customlogrus.New("/usr/share/filebeat/log/dms-sms/auth.log", logrus.Fields{"service": "auth"})
clubLogger := customlogrus.New("/usr/share/filebeat/log/dms-sms/club.log", logrus.Fields{"service": "club"})
outingLogger := customlogrus.New("/usr/share/filebeat/log/dms-sms/outing.log", logrus.Fields{"service": "outing"})
scheduleLogger := customlogrus.New("/usr/share/filebeat/log/dms-sms/schedule.log", logrus.Fields{"service": "schedule"})
announcementLogger := customlogrus.New("/usr/share/filebeat/log/dms-sms/announcement.log", logrus.Fields{"service": "announcement"})
openApiLogger := customlogrus.New("/usr/share/filebeat/log/dms-sms/open-api.log", logrus.Fields{"service": "open-api"})
excelApiLogger := customlogrus.New("/usr/share/filebeat/log/dms-sms/excel-api.log", logrus.Fields{"service": "excel-api"})
// create custom router & register function to execute before run
gin.SetMode(gin.ReleaseMode)
globalRouter := customrouter.New(gin.Default())
globalRouter.RegisterBeforeRun(
defaultHandler.ConsulChangeEventPublisher(),
consulAgent.ChangeAllServiceNodes,
defaultSubscriber.StartListening,
)
// routing ping & pong API
healthCheckRouter := globalRouter.Group("/")
healthCheckRouter.GET("/ping", func(c *gin.Context) { // add in v.1.0.2
c.JSON(http.StatusOK, "pong")
})
// routing API to use in consul watch
consulWatchRouter := globalRouter.Group("/")
consulWatchRouter.POST("/events/types/consul-change", defaultHandler.PublishConsulChangeEvent) // add in v.1.0.2
// register middleware in global router & handler
corsConfig := cors.DefaultConfig()
corsConfig.AllowAllOrigins = true
corsConfig.AllowHeaders = append(corsConfig.AllowHeaders, "Authorization", "authorization", "Request-Security")
// run middleware before routing matching
globalRouter.Use(
cors.New(corsConfig), // handle CORS request behind of AWS API Gateway
middleware.SecurityFilter(), // filter if verified client with algorithm using aes256
middleware.Correlator(), // set X-Request-ID field in request header to express correlate
// middleware.DosDetector(), // count request number per client IP to detect dos attack
)
// run middleware after successful routing matching
router := globalRouter.CustomGroup("/",
middleware.GinHResponseWriter(), // change ResponseWriter in *gin.Context to custom writer overriding that (add in v.1.0.3)
middleware.TracerSpanStarter(apiTracer), // start, end top span of tracer & set log, tag about response (add in v.1.0.3)
)
router.Validator = validator.New()
redisHandler := middleware.RedisHandler(redisCli, apiTracer, redisSetTopic, redisDelTopic)
// routing auth service API
authRouter := router.CustomGroup("/", middleware.LogEntrySetter(authLogger))
// auth service api for admin
authRouter.POSTWithAuth("/v1/students", defaultHandler.CreateNewStudent)
authRouter.POSTWithAuth("/v1/parents", defaultHandler.CreateNewParent)
authRouter.POST("/v1/login/admin", defaultHandler.LoginAdminAuth)
authRouter.POSTWithAuth("/v1/join-sms/unsigned-students", defaultHandler.SendJoinSMSToUnsignedStudents)
// auth service api for student
authRouter.POST("/v1/login/student", defaultHandler.LoginStudentAuth)
authRouter.PUTWithAuth("/v1/students/uuid/:student_uuid/password", defaultHandler.ChangeStudentPW)
authRouter.GETWithAuth("/v1/students/uuid/:student_uuid", defaultHandler.GetStudentInformWithUUID)
authRouter.GETWithAuth("/v1/student-uuids", defaultHandler.GetStudentUUIDsWithInform)
authRouter.POSTWithAuth("/v1/students/with-uuids", defaultHandler.GetStudentInformsWithUUIDs)
authRouter.GETWithAuth("/v1/students/uuid/:student_uuid/parent", defaultHandler.GetParentWithStudentUUID)
authRouter.GET("/v1/students/auth-code/:auth_code", defaultHandler.GetUnsignedStudentWithAuthCode)
authRouter.POST("/v1/students/with-code", defaultHandler.CreateNewStudentWithAuthCode)
// auth service api for teacher
authRouter.POST("/v1/teachers", defaultHandler.CreateNewTeacher)
authRouter.POST("/v1/login/teacher", defaultHandler.LoginTeacherAuth)
authRouter.POST("/v1/login/teacher/with-pick", defaultHandler.LoginTeacherAuthWithPICK)
authRouter.PUTWithAuth("/v1/teachers/uuid/:teacher_uuid/password", defaultHandler.ChangeTeacherPW)
authRouter.GETWithAuth("/v1/teachers/uuid/:teacher_uuid", defaultHandler.GetTeacherInformWithUUID)
authRouter.GETWithAuth("/v1/teacher-uuids", defaultHandler.GetTeacherUUIDsWithInform)
authRouter.PATCHWithAuth("/v1/teachers/uuid/:teacher_uuid", defaultHandler.ChangeTeacherInform)
// auth service api for parent
authRouter.POST("/v1/login/parent", defaultHandler.LoginParentAuth)
authRouter.PUTWithAuth("/v1/parents/uuid/:parent_uuid/password", defaultHandler.ChangeParentPW)
authRouter.GETWithAuth("/v1/parents/uuid/:parent_uuid", defaultHandler.GetParentInformWithUUID)
authRouter.GETWithAuth("/v1/parent-uuids", defaultHandler.GetParentUUIDsWithInform)
authRouter.GETWithAuth("/v1/parents/uuid/:parent_uuid/children", defaultHandler.GetChildrenInformsWithUUID)
// routing club service API
clubRouter := router.CustomGroup("/", middleware.LogEntrySetter(clubLogger))
// club service api for admin
clubRouter.POSTWithAuth("/v1/clubs", defaultHandler.CreateNewClub)
// club service api for student
clubRouter.GETWithAuth("/v1/clubs/sorted-by/update-time", defaultHandler.GetClubsSortByUpdateTime)
clubRouter.GETWithAuth("/v1/recruitments/sorted-by/create-time", defaultHandler.GetRecruitmentsSortByCreateTime)
clubRouter.GETWithAuth("/v1/clubs/uuid/:club_uuid", defaultHandler.GetClubInformWithUUID)
clubRouter.GETWithAuth("/v1/clubs", defaultHandler.GetClubInformsWithUUIDs)
clubRouter.GETWithAuth("/v1/recruitments/uuid/:recruitment_uuid", defaultHandler.GetRecruitmentInformWithUUID)
clubRouter.GETWithAuth("/v1/clubs/uuid/:club_uuid/recruitment-uuid", defaultHandler.GetRecruitmentUUIDWithClubUUID)
clubRouter.GETWithAuth("/v1/recruitment-uuids", defaultHandler.GetRecruitmentUUIDsWithClubUUIDs)
clubRouter.GETWithAuth("/v1/clubs/property/fields", defaultHandler.GetAllClubFields)
clubRouter.GETWithAuth("/v1/clubs/count", defaultHandler.GetTotalCountOfClubs)
clubRouter.GETWithAuth("/v1/recruitments/count", defaultHandler.GetTotalCountOfCurrentRecruitments)
clubRouter.GETWithAuth("/v1/leaders/uuid/:leader_uuid/club-uuid", defaultHandler.GetClubUUIDWithLeaderUUID)
// club service api for club leader
clubRouter.DELETEWithAuth("/v1/clubs/uuid/:club_uuid", defaultHandler.DeleteClubWithUUID)
clubRouter.POSTWithAuth("/v1/clubs/uuid/:club_uuid/members", defaultHandler.AddClubMember)
clubRouter.DELETEWithAuth("/v1/clubs/uuid/:club_uuid/members/:student_uuid", defaultHandler.DeleteClubMember)
clubRouter.PUTWithAuth("/v1/clubs/uuid/:club_uuid/leader", defaultHandler.ChangeClubLeader)
clubRouter.PATCHWithAuth("/v1/clubs/uuid/:club_uuid", defaultHandler.ModifyClubInform)
clubRouter.POSTWithAuth("/v1/recruitments", defaultHandler.RegisterRecruitment)
clubRouter.PATCHWithAuth("/v1/recruitments/uuid/:recruitment_uuid", defaultHandler.ModifyRecruitment)
clubRouter.DELETEWithAuth("/v1/recruitments/uuid/:recruitment_uuid", defaultHandler.DeleteRecruitment)
// routing outing service API
outingRouter := router.CustomGroup("/", middleware.LogEntrySetter(outingLogger))
outingRouter.POSTWithAuth("/v1/outings", defaultHandler.CreateOuting, redisHandler.CreateOuting()...)
outingRouter.GETWithAuth("/v1/students/uuid/:student_uuid/outings", defaultHandler.GetStudentOutings, redisHandler.GetStudentOutings()...)
outingRouter.GETWithAuth("/v1/outings/uuid/:outing_uuid", defaultHandler.GetOutingInform, redisHandler.GetOutingInform()...)
outingRouter.GETWithAuth("/v1/outings/uuid/:outing_uuid/card", defaultHandler.GetCardAboutOuting, redisHandler.GetCardAboutOuting()...)
outingRouter.POST("/v1/outings/uuid/:outing_uuid/actions/:action", defaultHandler.TakeActionInOuting, redisHandler.TakeActionInOuting()...)
outingRouter.GETWithAuth("/v1/outings/with-filter", defaultHandler.GetOutingWithFilter, redisHandler.GetOutingWithFilter()...)
outingRouter.GET("/v1/outings/code/:OCode", defaultHandler.GetOutingByOCode)
outingRouter.PATCHWithAuth("/v1/outings/uuid/:outing_uuid", defaultHandler.ModifyOuting, redisHandler.ModifyOuting()...)
// routing schedule service API
scheduleRouter := router.CustomGroup("/", middleware.LogEntrySetter(scheduleLogger))
scheduleRouter.POSTWithAuth("/v1/schedules", defaultHandler.CreateSchedule, redisHandler.CreateSchedule()...)
scheduleRouter.GETWithAuth("/v1/schedules/years/:year/months/:month", defaultHandler.GetSchedule, redisHandler.GetSchedule()...)
scheduleRouter.GETWithAuth("/v1/time-tables/years/:year/months/:month/days/:day", defaultHandler.GetTimeTable, redisHandler.GetTimeTable()...)
scheduleRouter.PATCHWithAuth("/v1/schedules/uuid/:schedule_uuid", defaultHandler.UpdateSchedule, redisHandler.UpdateSchedule()...)
scheduleRouter.DELETEWithAuth("/v1/schedules/uuid/:schedule_uuid", defaultHandler.DeleteSchedule, redisHandler.DeleteSchedule()...)
// routing announcement service API
announcementRouter := router.CustomGroup("/", middleware.LogEntrySetter(announcementLogger))
announcementRouter.POSTWithAuth("/v1/announcements", defaultHandler.CreateAnnouncement, redisHandler.CreateAnnouncement()...)
announcementRouter.GETWithAuth("/v1/announcements/types/:type", defaultHandler.GetAnnouncements, redisHandler.GetAnnouncements()...)
announcementRouter.GETWithAuth("/v1/announcements/uuid/:announcement_uuid", defaultHandler.GetAnnouncementDetail, redisHandler.GetAnnouncementDetail()...)
announcementRouter.PATCHWithAuth("/v1/announcements/uuid/:announcement_uuid", defaultHandler.UpdateAnnouncement, redisHandler.UpdateAnnouncement()...)
announcementRouter.DELETEWithAuth("/v1/announcements/uuid/:announcement_uuid", defaultHandler.DeleteAnnouncement, redisHandler.DeleteAnnouncement()...)
announcementRouter.GETWithAuth("/v1/students/uuid/:student_uuid/announcement-check", defaultHandler.CheckAnnouncement, redisHandler.CheckAnnouncement()...)
announcementRouter.GETWithAuth("/v1/announcements/types/:type/query/:search_query", defaultHandler.SearchAnnouncements, redisHandler.SearchAnnouncements()...)
announcementRouter.GETWithAuth("/v1/announcements/writer-uuid/:writer_uuid", defaultHandler.GetMyAnnouncements, redisHandler.GetMyAnnouncements()...)
// routing open-api agent API
openApiRouter := router.CustomGroup("/", middleware.LogEntrySetter(openApiLogger))
openApiRouter.GETWithAuth("/naver-open-api/search/local", defaultHandler.GetPlaceWithNaverOpenAPI)
// routing excel handling API
excelApiRouter := router.CustomGroup("/", middleware.LogEntrySetter(excelApiLogger))
excelApiRouter.POSTWithAuth("/v1/unsigned-students/parsed-by/excel", defaultHandler.AddUnsignedStudentsFromExcel)
excelApiRouter.POSTWithAuth("/v1/unsigned-students/parsed-by/excel/sheets/:sheet", defaultHandler.AddUnsignedStudentsFromExcel)
// run server
log.Fatal(globalRouter.Run(":80"))
}