添加在线调试 api

This commit is contained in:
2025-03-08 10:59:31 +08:00
parent 053041ae34
commit 5786ac9d99
28 changed files with 236 additions and 539 deletions

View File

@@ -4,42 +4,37 @@ import (
"context"
"log/slog"
"net/http"
"os"
"proxy-server/server/web/auth"
"proxy-server/server/web/router"
"proxy-server/server/pkg/env"
"strconv"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
var server *http.Server
func Start(ctx context.Context, errCh chan error) {
address := ":" + os.Getenv("PORT")
func Start(ctx context.Context) error {
address := ":" + strconv.Itoa(int(env.AppWebPort))
engine := gin.Default()
server = &http.Server{Addr: address, Handler: engine}
// 配置中间件和路由
Router(engine)
// 监听关闭信号
go func() {
<-ctx.Done()
slog.Info("web 服务被动关闭")
err := server.Shutdown(ctx)
err := server.Shutdown(context.Background())
if err != nil {
slog.Error("web 服务关闭失败", err)
return
}
}()
// 配置中间件和路由
auth.Apply(engine, nil)
router.Apply(engine)
// 启动服务
err := server.ListenAndServe()
if err != nil {
errCh <- err
return
return errors.Wrap(err, "web 服务启动失败")
}
slog.Debug("web 服务主动结束")
errCh <- nil
return nil
}