-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
105 lines (81 loc) · 2.43 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"
"net/http"
"os"
"os/signal"
"syscall"
c "github.com/totoval/framework/config"
"github.com/totoval/framework/graceful"
"github.com/totoval/framework/helpers/log"
"github.com/totoval/framework/helpers/toto"
"github.com/totoval/framework/helpers/zone"
"github.com/totoval/framework/http/middleware"
"github.com/totoval/framework/request"
"github.com/totoval/framework/sentry"
"totoval/bootstrap"
"totoval/resources/views"
"totoval/routes"
)
func init() {
bootstrap.Initialize()
}
// @caution cannot use config methods to get config in init function
func main() {
//j := &jobs.ExampleJob{}
//j.SetParam(&pbs.ExampleJob{Query: "test", PageNumber: 111, ResultPerPage: 222})
////j.SetDelay(5 * zone.Second)
//err := job.Dispatch(j)
//fmt.Println(err)
//go hub.On("add-user-affiliation") // go run artisan.go queue:listen add-user-affiliation
ctx, cancel := context.WithCancel(context.Background())
quit := make(chan os.Signal, 1)
// kill (no param) default send syscanll.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall. SIGKILL but can"t be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
go func() {
call := <-quit
log.Info("system call", toto.V{"call": call})
cancel()
}()
httpServe(ctx)
}
func httpServe(ctx context.Context) {
r := request.New()
sentry.Use(r.GinEngine(), false)
if c.GetBool("app.debug") {
r.Use(middleware.RequestLogger())
}
if c.GetString("app.env") == "production" {
r.Use(middleware.Logger())
r.Use(middleware.Recovery())
}
r.Use(middleware.Locale())
routes.Register(r)
views.Initialize(r)
s := &http.Server{
Addr: ":" + c.GetString("app.port"),
Handler: r,
ReadTimeout: zone.Duration(c.GetInt64("app.read_timeout_seconds")) * zone.Second,
WriteTimeout: zone.Duration(c.GetInt64("app.write_timeout_seconds")) * zone.Second,
MaxHeaderBytes: 1 << 20,
}
go func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err.Error())
}
}()
<-ctx.Done()
log.Info("Shutdown Server ...")
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
_ctx, cancel := context.WithTimeout(ctx, 5*zone.Second)
defer cancel()
if err := s.Shutdown(_ctx); err != nil {
log.Fatal("Server Shutdown: ", toto.V{"error": err})
}
// totoval framework shutdown
graceful.ShutDown(false)
log.Info("Server exiting")
}