加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 1.38 KB
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/air-gases/defibrillator"
"github.com/air-gases/limiter"
"github.com/air-gases/logger"
"github.com/air-gases/redirector"
"github.com/aofei/air"
_ "github.com/goproxy/goproxy.cn/handler"
"github.com/rs/zerolog/log"
)
// a is the `air.Default`.
var a = air.Default
func main() {
a.ErrorHandler = errorHandler
a.Pregases = []air.Gas{
logger.Gas(logger.GasConfig{
IncludeClientAddress: true,
}),
defibrillator.Gas(defibrillator.GasConfig{}),
redirector.WWW2NonWWWGas(redirector.WWW2NonWWWGasConfig{}),
limiter.BodySizeGas(limiter.BodySizeGasConfig{
MaxBytes: 1 << 20,
}),
}
shutdownChan := make(chan os.Signal, 1)
signal.Notify(shutdownChan, os.Interrupt, syscall.SIGTERM)
go func() {
if err := a.Serve(); err != nil {
log.Error().Err(err).
Str("app_name", a.AppName).
Msg("server error")
}
}()
<-shutdownChan
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
a.Shutdown(ctx)
}
// errorHandler is the error handler.
func errorHandler(err error, req *air.Request, res *air.Response) {
if res.Written {
return
}
m := ""
if !req.Air.DebugMode && res.Status == http.StatusInternalServerError {
m = http.StatusText(res.Status)
} else {
m = err.Error()
}
res.WriteJSON(map[string]interface{}{
"Error": m,
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化