-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (92 loc) · 2.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
111
112
113
114
115
package main
import (
"context"
"fmt"
configpackage "miniwebserver/config"
"miniwebserver/controller"
loggerpackage "miniwebserver/logger"
"miniwebserver/router"
serverpackage "miniwebserver/server"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
var (
configPath string
config *configpackage.AppConfiguration
err error
)
func init() {
// reading from command line
var args = os.Args
configPath = args[1]
// config file path
config, err = configpackage.NewConfiguration(configPath)
if err != nil {
panic(err)
}
}
func main() {
/*
SETUP CONTEXT
*/
// https://dasarpemrogramangolang.novalagung.com/A-pipeline-context-cancellation.html
ctx := context.Background()
/*
SETUP LOGGER
*/
logger, err := loggerpackage.NewLogger(config)
if err != nil {
panic(fmt.Sprintf("unable to init logger: %v\n", err.Error()))
}
logger.CheckPathFile()
/*
SETUP CONTROLLER
*/
// init controller response
httpresponse := controller.NewHTTPResponse(logger)
// init default http controller
handler := controller.NewHTTPController(config, logger, httpresponse)
/*
SETUP ROUTER
*/
// init router
httprouter := router.NewRouter(config, handler)
/*
SETUP SERVER
*/
// init http server
appIp := *config.AppIP + ":" + *config.AppPort
httpserver := serverpackage.NewHTTPServer(&http.Server{
Addr: appIp,
Handler: httprouter.Router,
}, logger)
if *config.Production == `true` {
httpserver.SetCertificate(*config.Certificate, *config.CertificateKey)
// run server
httpserver.RunTLS()
} else {
httpserver.Run()
}
/*
Graceful shutdown
*/
// Wait for kill signal of channel
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
// This blocks until a signal is passed into the quit channel
<-quit
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
// Shutdown server
logger.Info("Shutting down server...")
err = httpserver.Shutdown(ctx)
if err != nil {
// fmt.Errorf(fmt.Sprintf("Server forced to shutdown : %v\n", err.Error()))
logger.Fatal(fmt.Sprintf("Server forced to shutdown : %v\n", err.Error()))
}
}