86 lines
2.0 KiB
Go
86 lines
2.0 KiB
Go
// Code generated by hertz generator.
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"time"
|
|
|
|
"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/suyiiyii/hertz101/app/facade/biz/router"
|
|
"github.com/suyiiyii/hertz101/app/facade/conf"
|
|
"go.uber.org/zap/zapcore"
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
)
|
|
|
|
func main() {
|
|
// init dal
|
|
// dal.Init()
|
|
address := conf.GetConf().Hertz.Address
|
|
h := server.New(server.WithHostPorts(address))
|
|
|
|
registerMiddleware(h)
|
|
|
|
// 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)
|
|
|
|
h.Spin()
|
|
}
|
|
|
|
func registerMiddleware(h *server.Hertz) {
|
|
// log
|
|
logger := hertzlogrus.NewLogger()
|
|
hlog.SetLogger(logger)
|
|
hlog.SetLevel(conf.LogLevel())
|
|
asyncWriter := &zapcore.BufferedWriteSyncer{
|
|
WS: zapcore.AddSync(&lumberjack.Logger{
|
|
Filename: conf.GetConf().Hertz.LogFileName,
|
|
MaxSize: conf.GetConf().Hertz.LogMaxSize,
|
|
MaxBackups: conf.GetConf().Hertz.LogMaxBackups,
|
|
MaxAge: conf.GetConf().Hertz.LogMaxAge,
|
|
}),
|
|
FlushInterval: time.Minute,
|
|
}
|
|
hlog.SetOutput(asyncWriter)
|
|
h.OnShutdown = append(h.OnShutdown, func(ctx context.Context) {
|
|
asyncWriter.Sync()
|
|
})
|
|
hlog.SetOutput(os.Stdout)
|
|
|
|
// 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())
|
|
}
|
|
|
|
// recovery
|
|
h.Use(recovery.Recovery())
|
|
|
|
// cores
|
|
h.Use(cors.Default())
|
|
}
|