-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (60 loc) · 1.47 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/spark-golang/spark-url/api/routes"
"github.com/spark-golang/spark-url/database/mysql"
"github.com/spark-golang/spark-url/database/redis"
"github.com/spark-golang/spark-url/utils"
"github.com/spark-golang/spark-url/utils/env"
"github.com/spark-golang/spark-url/utils/gp"
)
func main() {
env.InitEnv()
utils.InitLog()
utils.Zlog(utils.LogLevelInfo, "init_server", "start server", "")
redis.Demo = &redis.Redis{}
redis.Demo.Conn()
gp.GoPool = gp.New(15)
myMysql.Conn()
routes.InitAPP(env.Getenv("ENV"))
routes.DispatchForLocal()
host := env.Getenv("HOST")
port := env.Getenv("PORT")
srv := &http.Server{
Addr: host + ":" + port,
Handler: routes.APP,
}
go reloadConfig()
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Fatal("start error")
}
}()
utils.Zlog(utils.LogLevelInfo, "init_server", "ready to handle request", fmt.Sprintf("host: %s, port: %s", host, port))
// graceful shutdown
quitCh := make(chan os.Signal)
signal.Notify(quitCh, os.Interrupt)
<-quitCh
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 error:", err)
}
log.Println("Server exiting")
}
func reloadConfig() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGUSR2)
for {
<-c
env.ReloadEnv()
}
}