1 Star 0 Fork 0

dpwgc/sqlmidway

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 2.73 KB
一键复制 编辑 原始数据 按行查看 历史
dpwgc 提交于 2024-06-07 15:28 +08:00 . view
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/dpwgc/easierweb"
"log/slog"
"net/http"
"strings"
)
func main() {
InitConfig()
InitLog()
api, err := NewAPI()
if err != nil {
Logger.Error(err.Error())
return
}
router := easierweb.New(easierweb.RouterOptions{
ErrorHandle: errorHandle(),
Logger: Logger,
RequestHandle: requestHandle(),
ResponseHandle: responseHandle(),
CloseConsolePrint: true,
}).Use(middleware())
router.EasyPOST("/query/:db/:group/:api", api.Query)
router.EasyPOST("/command/:db/:group/:api", api.Command)
router.EasyGET("/info", api.Info)
router.EasyGET("/health", api.Health)
router.Static("/view/*filepath", "view")
host := fmt.Sprintf("%s:%v", Config().Server.Addr, Config().Server.Port)
if Config().Server.TLS {
err = router.RunTLS(host, Config().Server.CertFile, Config().Server.KeyFile, &tls.Config{})
} else {
err = router.Run(host)
}
Logger.Error(err.Error())
}
func errorHandle() easierweb.ErrorHandle {
return func(ctx *easierweb.Context, err any) {
errStr := fmt.Sprintf("%v", err)
ctx.Logger.Error(errStr, slog.String("url", ctx.Request.URL.String()),
slog.String("client", ctx.Request.RemoteAddr),
slog.String("body", string(ctx.Body)))
ctx.WriteJSON(http.StatusBadRequest, ErrorReply{
Error: errStr,
})
}
}
func requestHandle() easierweb.RequestHandle {
return func(ctx *easierweb.Context, reqObj any) error {
if len(ctx.Body) > 0 {
decoder := json.NewDecoder(strings.NewReader(string(ctx.Body)))
decoder.UseNumber() // UseNumber causes the Decoder to unmarshal a number into an interface{} as a Number instead of as a float64.
if err := decoder.Decode(reqObj); err != nil {
return err
}
}
return nil
}
}
func responseHandle() easierweb.ResponseHandle {
return func(ctx *easierweb.Context, result any, err error) {
if err != nil {
if result != nil {
errStr := fmt.Sprintf("%v", err)
ctx.Logger.Error(errStr, slog.String("url", ctx.Request.URL.String()),
slog.String("client", ctx.Request.RemoteAddr),
slog.String("body", string(ctx.Body)))
ctx.WriteJSON(http.StatusBadRequest, result)
return
}
panic(err)
}
if result == nil {
ctx.NoContent(http.StatusNoContent)
return
}
ctx.WriteJSON(http.StatusOK, result)
}
}
func middleware() easierweb.Handle {
return func(ctx *easierweb.Context) {
if ctx.Route == "/health" {
return
}
body := ""
sizeLimit := 1024 * 1024
if len(ctx.Body) > 0 {
if len(ctx.Body) > sizeLimit {
body = "body is too large"
} else {
body = string(ctx.Body)
}
}
ctx.Logger.Info(fmt.Sprintf("%s -> %s", ctx.Request.RemoteAddr, ctx.Request.URL.String()),
slog.String("body", body),
slog.Int("code", ctx.Code))
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dpwgc/sqlmidway.git
git@gitee.com:dpwgc/sqlmidway.git
dpwgc
sqlmidway
sqlmidway
master

搜索帮助