重构代码结构与认证体系,集成异步任务消费者

This commit is contained in:
2025-11-17 18:38:10 +08:00
parent a97c970166
commit a245229bc2
70 changed files with 2000 additions and 2334 deletions

View File

@@ -1,6 +1,8 @@
package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
@@ -11,45 +13,16 @@ import (
)
func main() {
// 退出信号
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
// 初始化应用
env.Init()
logs.Init()
// 创建服务
app, err := web.New(&web.Config{
Listen: ":8080",
})
err := web.RunApp(ctx)
if err != nil {
slog.Error("Failed to create server", slog.Any("err", err))
return
}
// 异步运行服务
errCh := make(chan error)
defer close(errCh)
go func() {
err := app.Run()
if err != nil {
slog.Error("Failed to run server", slog.Any("err", err))
errCh <- err
}
errCh <- nil
}()
// 关闭服务
select {
case err = <-errCh:
case <-shutdown:
slog.Debug("捕获结束信号")
app.Stop()
err = <-errCh
}
if err != nil {
slog.Error("Server error", slog.Any("err", err))
slog.Error(fmt.Sprintf("%v", err))
}
}