-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (90 loc) · 3.21 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
package main
import (
"context"
"github.com/Abdulsametileri/messaging-service/config"
"github.com/Abdulsametileri/messaging-service/controllers"
"github.com/Abdulsametileri/messaging-service/database"
"github.com/Abdulsametileri/messaging-service/infra/mailgunclient"
"github.com/Abdulsametileri/messaging-service/middlewares"
"github.com/Abdulsametileri/messaging-service/repository/logrepo"
"github.com/Abdulsametileri/messaging-service/repository/messagerepo"
"github.com/Abdulsametileri/messaging-service/repository/userrepo"
"github.com/Abdulsametileri/messaging-service/services/authservice"
"github.com/Abdulsametileri/messaging-service/services/logservice"
"github.com/Abdulsametileri/messaging-service/services/messageservice"
"github.com/Abdulsametileri/messaging-service/services/userservice"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
"os/signal"
"time"
)
func main() {
config.Setup()
db := database.Setup()
database.Migrate()
/*
====== Setup infra ==============
*/
emailClient := mailgunclient.NewMailgunClient()
_ = emailClient
/*
====== Setup repositories =======
*/
userRepo := userrepo.NewUserRepo(db)
logRepo := logrepo.NewLogRepo(db)
messageRepo := messagerepo.NewMessageRepo(db)
/*
====== Setup services ===========
*/
authService := authservice.NewAuthService()
userService := userservice.NewUserService(userRepo)
logService := logservice.NewLogService(logRepo)
messageService := messageservice.NewMessageService(messageRepo)
/*
====== Setup controllers ========
*/
baseCtl := controllers.NewBaseController(logService)
userCtl := controllers.NewUserController(baseCtl, authService, userService)
messageCtl := controllers.NewMessageController(baseCtl, userService, messageService)
if !config.IsDebug {
gin.SetMode(gin.ReleaseMode)
}
var router = gin.New()
if config.IsDebug {
router.Use(gin.Logger())
}
router.Use(middlewares.CustomRecoveryMiddleware(baseCtl))
router.GET("/ping", func(c *gin.Context) { c.String(http.StatusOK, "pong") })
v1 := router.Group("api/v1")
{
v1.POST("register", userCtl.Register)
v1.POST("login", userCtl.Login)
v1.GET("mutateUser/:mutateUserId", middlewares.RequireLoggedIn(baseCtl), userCtl.MutateUser)
v1.GET("users", middlewares.RequireLoggedIn(baseCtl), userCtl.GetUserList)
v1.GET("messagesWith/:userName", middlewares.RequireLoggedIn(baseCtl), messageCtl.GetMessages)
v1.POST("sendMessage/:userName", middlewares.RequireLoggedIn(baseCtl), messageCtl.SendMessage)
}
srv := &http.Server{
Addr: ":8080",
Handler: router,
ReadTimeout: 5 * time.Second, // max time to read request from the client
WriteTimeout: 10 * time.Second, // max time to write response to the client
IdleTimeout: 120 * time.Second, // max time for connections using TCP Keep-Alive
}
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err.Error())
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, os.Kill)
// Block until a signal is received.
sig := <-c
log.Println("Got signal:", sig)
// gracefully shutdown the server, waiting max 30 seconds for current operations to complete
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
srv.Shutdown(ctx)
}