shaoyongjun ab00b85c8f to:sync
2024-10-31 01:32:52 +08:00

102 lines
3.0 KiB
Go

package main
import (
"context"
"fmt"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"mylomen_server/apps"
"mylomen_server/common/constant"
"mylomen_server/common/utils"
"mylomen_server/static"
"mylomen_server/static/css"
"mylomen_server/static/js"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
e := echo.New()
// Echo instance
e.HideBanner = true
e.HidePort = true
e.Validator = utils.NewValidator()
e.Use(middleware.Recover())
// Middleware
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
w := c.Response()
r := c.Request()
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Credentials", "true")
w.Header().Add("Access-Control-Allow-Headers", "*")
w.Header().Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
if r.Method == "OPTIONS" {
http.Error(w, "No Content", http.StatusNoContent)
return nil
}
return next(c)
}
})
//健康检测
e.GET("/health", func(c echo.Context) error { return c.JSON(http.StatusOK, "ok") })
e.GET("/name", func(c echo.Context) error { return c.JSON(http.StatusOK, "mylomen-server") })
e.GET("/v1/subscribeEmail", func(c echo.Context) error { return c.JSON(http.StatusOK, "ok") })
e.GET("/v1/unsubscribeEmail", func(c echo.Context) error { return c.JSON(http.StatusOK, "ok") })
e.GET("/v1/subscribeTopic", func(c echo.Context) error { return c.JSON(http.StatusOK, "ok") })
e.GET("/v1/unsubscribeTopic", func(c echo.Context) error { return c.JSON(http.StatusOK, "ok") })
e.GET("/WW_verify_aMVgO6pcg3RRrMvk.txt", func(c echo.Context) error {
//c.Response().Header().Set("Content-Type", "image/gif")
return c.String(http.StatusOK, "aMVgO6pcg3RRrMvk")
})
//登录界面
e.GET("/", static.Login)
e.GET("/favicon.ico", static.Favicon)
e.GET("/chat", static.Chat)
e.GET("/note.html", static.Note)
e.GET("/chat1", static.Chat1)
e.GET("/chat2", static.Chat2)
e.GET("/chat3", static.Chat3)
//user
apps.InitUserGroup(e.Group("/v1/user/"))
//static
static.InitStaticGroup(e.Group("/v1/static/"))
css.InitCssGroup(e.Group("/css"))
js.InitJsGroup(e.Group("/js"))
//ai
apps.InitAiGroup(e.Group("/v1/ai/", func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Set(constant.BizId, constant.BizIdAI)
return next(c)
}
} /*filter.UserAuth,*/ /*filter.UseLimit*/))
// Start server
go func() {
if err := e.Start(":8080"); err != nil && err != http.ErrServerClosed {
fmt.Println("shutting down the server")
panic(err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
// Use a buffered channel to avoid missing signals as recommended for signal.Notify
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
fmt.Println(err.Error())
}
}