-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
73 lines (61 loc) · 1.45 KB
/
main.go
File metadata and controls
73 lines (61 loc) · 1.45 KB
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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
conf "trypto-server/config"
ctl "trypto-server/controller"
"trypto-server/logger"
"trypto-server/model"
rt "trypto-server/router"
"golang.org/x/sync/errgroup"
)
var (
g errgroup.Group
)
func main() {
mod, _ := model.NewModel()
controller, _ := ctl.NewCTL(mod)
rt, _ := rt.NewRouter(controller)
config := conf.GetConfig("./config/config.toml")
if err := logger.InitLogger(config); err != nil {
fmt.Printf("init logger failed, err:%v\n", err)
return
}
mapi := &http.Server{
Addr: config.Server.Port,
Handler: rt.Idx(),
ReadTimeout: 0,
WriteTimeout: 0,
MaxHeaderBytes: 1 << 20,
}
//고루틴 서버 동작
g.Go(func() error {
return mapi.ListenAndServe()
})
quit := make(chan os.Signal) //chan 선언
// 해당 chan 핸들링 선언, SIGINT, SIGTERM에 대한 메세지 notify
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit //메세지 등록
// 해당 context 타임아웃 설정, 5초후 server stop
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := mapi.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
// catching ctx.Done(). timeout of 5 seconds.
select {
case <-ctx.Done():
logger.Info("timeout of 5 seconds.")
}
logger.Info("Server exiting")
//우아한 종료
if err := g.Wait(); err != nil {
logger.Error(err)
}
}