forked from hantmac/fuckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (84 loc) · 2.14 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
package main
import (
"context"
"flag"
"fmt"
"fuckdb/config"
"fuckdb/routers"
"fuckdb/routers/middleware"
"io/ioutil"
"net/http"
"os"
"os/signal"
"time"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/lexkong/log"
"github.com/spf13/viper"
_ "github.com/go-sql-driver/mysql"
)
func main() {
g := gin.Default()
//Use frontend static
g.Use(static.Serve("/", static.LocalFile("./frontend/dist", false)))
// LoggerWithFormatter middleware
// By default gin.DefaultWriter = os.Stdout
g.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
b, _ := ioutil.ReadAll(param.Request.Body)
// your custom format
return fmt.Sprintf("StatusCode:%d--%s-ClientIp:%s - TimeStamp:[%s] \"ReqMethod:%s--"+
"API:%s--ReqProto%s--CostTime:%s \"UserAgent:%s-\" -Error:%s\"\n",
param.StatusCode,
string(b),
param.ClientIP,
param.TimeStamp.Format(time.RFC1123),
param.Method,
param.Path,
param.Request.Proto,
param.Latency,
param.Request.UserAgent(),
param.ErrorMessage,
)
}))
//Add middleware in this slice
var middlewares = []gin.HandlerFunc{middleware.NoCache, middleware.Secure}
routers.Load(
g,
middlewares...,
)
// init config
if err := config.InitConfig(""); err != nil {
log.Error("init config error:%s", err)
panic(err)
}
log.Info("config init success")
var host = viper.GetString("server.host")
var port = viper.GetString("server.port")
fmt.Print(host + ":" + port)
var addr = flag.String("server addr", host+":"+port, "server addr")
// run server
srv := http.Server{
Addr: *addr,
Handler: g,
}
processd := make(chan struct{})
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Error("server shutdown failed,err:%V\n", err)
}
log.Info("server shutdown gracefully")
close(processd)
}()
err := srv.ListenAndServe()
fmt.Println(*addr)
fmt.Println("server successly")
if err != http.ErrServerClosed {
log.Error("server not shutdown gracefully,err:%v", err)
}
<-processd
}