4 Star 2 Fork 1

Gitee 极速下载/ip-laslant

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/vicanso/location/
克隆/下载
main.go 2.01 KB
一键复制 编辑 原始数据 按行查看 历史
vicanso 提交于 2022-01-15 11:36 . chore: update go modules
package main
import (
"net/http"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"github.com/vicanso/elton"
"github.com/vicanso/elton/middleware"
_ "github.com/vicanso/location/controller"
"github.com/vicanso/location/router"
humanize "github.com/dustin/go-humanize"
)
var (
runMode string
)
// 获取监听地址
func getListen() string {
listen := os.Getenv("LISTEN")
if listen == "" {
listen = ":7001"
}
return listen
}
func main() {
listen := getListen()
c := zap.NewProductionConfig()
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
// 只针对panic 以上的日志增加stack trace
logger, err := c.Build(zap.AddStacktrace(zap.DPanicLevel))
if err != nil {
panic(err)
}
e := elton.New()
e.OnError(func(c *elton.Context, err error) {
logger.DPanic("unexpected error",
zap.String("uri", c.Request.RequestURI),
zap.Error(err),
)
})
e.Use(middleware.NewRecover())
e.Use(middleware.NewStats(middleware.StatsConfig{
OnStats: func(statsInfo *middleware.StatsInfo, _ *elton.Context) {
logger.Info("access log",
zap.String("ip", statsInfo.IP),
zap.String("method", statsInfo.Method),
zap.String("uri", statsInfo.URI),
zap.Int("status", statsInfo.Status),
zap.String("latency", statsInfo.Latency.String()),
zap.String("size", humanize.Bytes(uint64(statsInfo.Size))),
)
},
}))
e.Use(middleware.NewDefaultError())
e.Use(func(c *elton.Context) error {
c.NoCache()
return c.Next()
})
e.Use(middleware.NewDefaultFresh())
e.Use(middleware.NewDefaultETag())
e.Use(middleware.NewDefaultResponder())
// health check
e.GET("/ping", func(c *elton.Context) (err error) {
c.Body = "pong"
return
})
groups := router.GetGroups()
for _, g := range groups {
e.AddGroup(g)
}
// http1与http2均支持
e.Server = &http.Server{
Handler: h2c.NewHandler(e, &http2.Server{}),
}
logger.Info("server will listen on " + listen)
err = e.ListenAndServe(listen)
if err != nil {
panic(err)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/ip-laslant.git
git@gitee.com:mirrors/ip-laslant.git
mirrors
ip-laslant
ip-laslant
master

搜索帮助