levi service
This commit is contained in:
parent
13923a1828
commit
8212f7da51
|
@ -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 := 9900
|
||||
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
|
||||
}
|
|
@ -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,
|
||||
})
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -171,3 +171,31 @@ func StartFirenzeServer(srv *http.Server, cfg *configcenter.DefaultConfig, ip st
|
|||
logger.Info("Server exited")
|
||||
//setServerStatusFD(ServerStatusFDStop)
|
||||
}
|
||||
|
||||
func StartLeviServer(srv *http.Server, ip string, port int) {
|
||||
go func() {
|
||||
logger.Info("Start HTTP Server, ip: %v, port: %v", ip, port)
|
||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
logger.Fatal("listen: %v", err)
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
logger.Info("Shutdown Server ...")
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
if err := srv.Shutdown(ctx); err != nil {
|
||||
logger.Fatal("Server Shutdown:", err)
|
||||
}
|
||||
defer cancel()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
logger.Info("timeout of 1 seconds.")
|
||||
}
|
||||
|
||||
logger.Info("Server exited")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue