-
Notifications
You must be signed in to change notification settings - Fork 124
/
main.go
executable file
·75 lines (62 loc) · 1.75 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
// Code generated by hertz generator.
package main
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/middlewares/server/recovery"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/hertz-contrib/cors"
"github.com/hertz-contrib/gzip"
"github.com/hertz-contrib/logger/accesslog"
hertzlogrus "github.com/hertz-contrib/logger/logrus"
"github.com/hertz-contrib/pprof"
"github.com/hertz/hello/biz/router"
"github.com/hertz/hello/conf"
"gopkg.in/natefinch/lumberjack.v2"
)
func main() {
// init dal
// dal.Init()
address := conf.GetConf().Hertz.Address
h := server.New(server.WithHostPorts(address))
// add a ping route to test
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"})
})
router.GeneratedRegister(h)
// do what you wanted
// add some render data: <no value>
registerMiddleware(h)
h.Spin()
}
func registerMiddleware(h *server.Hertz) {
// pprof
if conf.GetConf().Hertz.EnablePprof {
pprof.Register(h)
}
// gzip
if conf.GetConf().Hertz.EnableGzip {
h.Use(gzip.Gzip(gzip.DefaultCompression))
}
// access log
if conf.GetConf().Hertz.EnableAccessLog {
h.Use(accesslog.New())
}
// log
logger := hertzlogrus.NewLogger()
hlog.SetLogger(logger)
hlog.SetLevel(conf.LogLevel())
hlog.SetOutput(&lumberjack.Logger{
Filename: conf.GetConf().Hertz.LogFileName,
MaxSize: conf.GetConf().Hertz.LogMaxSize,
MaxBackups: conf.GetConf().Hertz.LogMaxBackups,
MaxAge: conf.GetConf().Hertz.LogMaxAge,
})
// recovery
h.Use(recovery.Recovery())
// cores
h.Use(cors.Default())
}