-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
105 lines (86 loc) · 2.57 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
implementations "leanmeal/api/Implementations"
"leanmeal/api/interfaces"
"leanmeal/api/middlewhere"
"leanmeal/api/routes"
)
func main() {
// DI Service registration
config := implementations.Configuration{}
config.Load()
connectionString := config.GetKey("ConnectionString").(string)
firebaseKey := config.GetKey("FirebaseServerKey").(string)
jwt := implementations.JwtService{}
jwt.Secret = config.GetKey("jwt-key").(string)
jwt.Issuer = config.GetKey("jwt-issuer").(string)
storage := implementations.Storage{
ConnectionString: connectionString,
}
passwordService := implementations.PasswordService{}
initializationService := implementations.Initialization{
Storage: storage,
}
firebaseMessageService := implementations.FirebaseCloudMessaging{
ServerKey: firebaseKey,
}
// Middlewhere setups
authMiddlewhere := middlewhere.AuthenticationMiddlewhere{
JwtService: &jwt,
}
cors := middlewhere.Cors()
if !initializationService.Initialized() {
initializationService.Database()
initializationService.Seed()
}
//Init the server
startServer(&config, &storage, &passwordService, &jwt, authMiddlewhere, &cors, &firebaseMessageService)
}
func startServer(configuration interfaces.Configuration, storage interfaces.Storage, passwordService interfaces.PasswordService,
jwt interfaces.JwtService, authMiddlewhere middlewhere.AuthenticationMiddlewhere, cors *gin.HandlerFunc, firebaseCloudMessaging interfaces.FirebaseCloudMessaging) {
port := configuration.GetKey("Port").(string)
router := gin.New()
router.Use(*cors)
v1 := router.Group("/v1")
appRouter := routes.ApplicationRouter{
Configuration: configuration,
Storage: storage,
PasswordService: passwordService,
AuthMiddlewhere: &authMiddlewhere,
Jwt: jwt,
FirebaseMessaging: firebaseCloudMessaging,
V1: v1,
}
appRouter.Init()
srv := &http.Server{
Addr: port,
Handler: router,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
got := <-quit
fmt.Println(got)
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
connectionDone := <-ctx.Done()
fmt.Println(connectionDone)
log.Println("Server exiting")
}