79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"ai-gateway/apps"
|
|
"ai-gateway/common/constant"
|
|
"ai-gateway/common/filter"
|
|
"ai-gateway/common/utils"
|
|
"code.freebrio.com/fb-go/lib/echo/helper"
|
|
"context"
|
|
"fmt"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/echo/v4/middleware"
|
|
"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.HTTPErrorHandler = helper.FBHTTPErrorHandler
|
|
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, "wx-server") })
|
|
|
|
//user
|
|
apps.InitUserGroup(e.Group("/user/v1/"))
|
|
|
|
//ai
|
|
apps.InitAiGroup(e.Group("/ai/v1/", 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")
|
|
}
|
|
}()
|
|
|
|
// 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())
|
|
}
|
|
}
|