diff --git a/app/levi/cmd/main.go b/app/levi/cmd/main.go new file mode 100644 index 00000000..89f13533 --- /dev/null +++ b/app/levi/cmd/main.go @@ -0,0 +1,64 @@ +package main + +import ( + "fmt" + "net" + "net/http" + "os" + "runtime" + "service/app/levi/controller" + "service/library/httpengine" + "service/library/httpserver" + "service/library/logger" + "strings" + "time" +) + +func main() { + defer func() { + logger.Recover() + }() + + // 初始化http server + ip := GetIp() + port := 9888 + router := httpengine.NewRouter() + controller.Init(router) + srv := &http.Server{ + Addr: fmt.Sprintf("%s:%d", ip, port), + Handler: router, + } + + httpserver.StartLeviServer(srv, ip, port) +} + +func PrintAndExit(msg string) { + _, file, line, _ := runtime.Caller(1) + errorMsg := fmt.Sprintf("file %s, line %d, %s\n", file, line, msg) + _, _ = fmt.Fprintf(os.Stderr, errorMsg) + logger.Fatal(errorMsg) + time.Sleep(1) //wait logger flush + //os.Exit(1) +} + +func GetIp() string { + addrs, err := net.InterfaceAddrs() + if err != nil { + PrintAndExit("get ip fail") + return "127.0.0.1" + } + retIp := "" + for _, address := range addrs { + // 检查ip地址判断是否回环地址 + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + ip := ipnet.IP.String() + if strings.HasPrefix(ip, "172.") { + retIp = ip + break + } + } + } + } + return retIp +} diff --git a/app/levi/controller/init.go b/app/levi/controller/init.go new file mode 100644 index 00000000..ea3a0de9 --- /dev/null +++ b/app/levi/controller/init.go @@ -0,0 +1,30 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "net/http" + "service/api/base" + "service/api/consts" +) + +func Init(r *gin.Engine) { + r.HandleMethodNotAllowed = true + + r.GET("/seeyouagain", SeeYouAgainHandler) +} + +func ReplyOk(ctx *gin.Context, data any) { + ctx.JSON(http.StatusOK, base.BaseResponse{ + Ret: consts.RetCodeSuccess, + ErrCode: consts.ErrorCodeSuccess, + Data: data, + }) +} + +func ReplyErrorMsg(ctx *gin.Context, msg string) { + ctx.AbortWithStatusJSON(http.StatusOK, base.BaseResponse{ + Ret: consts.RetCodeFail, + ErrCode: consts.ErrorCodeFail, + Msg: msg, + }) +} diff --git a/app/levi/controller/levi.go b/app/levi/controller/levi.go new file mode 100644 index 00000000..2f0bfe00 --- /dev/null +++ b/app/levi/controller/levi.go @@ -0,0 +1,13 @@ +package controller + +import ( + "github.com/gin-gonic/gin" +) + +func SeeYouAgainHandler(ctx *gin.Context) { + data := map[string]any{ + "api_domain": "api.tiefen.space", + "ws_domain": "ws.tiefen.space", + } + ReplyOk(ctx, data) +}