forked from hantmac/fuckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (54 loc) · 1.35 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
package main
import (
"fmt"
"fuckdb/routers/middleware"
"fuckdb/config"
"fuckdb/routers"
"github.com/gin-gonic/gin"
"github.com/lexkong/log"
"github.com/spf13/viper"
"io/ioutil"
"time"
_ "github.com/go-sql-driver/mysql"
)
func main() {
g := gin.New()
// 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")
// run server
host := viper.GetString("server.host")
port := viper.GetString("server.port")
if err := g.Run(host + ":" + port); err != nil {
log.Error("run server error:", err)
panic(err)
}
}