This commit is contained in:
Sakurasan
2025-02-01 23:52:55 +08:00
parent 65d6d12972
commit bc223d6530
30 changed files with 2683 additions and 242 deletions

92
cmd/openteam/main.go Normal file
View File

@@ -0,0 +1,92 @@
package main
import (
"context"
"log"
"net/http"
"opencatd-open/pkg/store"
"opencatd-open/team/dashboard"
"opencatd-open/wire"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
_, err := store.InitDB()
if err != nil {
panic(err)
}
team, err := wire.InitTeamHandler(ctx, store.DB)
if err != nil {
panic(err)
}
r := gin.Default()
teamGroup := r.Group("/1")
teamGroup.Use(team.AuthMiddleware())
{
teamGroup.POST("/users/init", team.InitAdmin)
// 获取当前用户信息
teamGroup.GET("/me", team.Me)
//// team.GET("/me/usages", team.HandleMeUsage)
teamGroup.POST("/keys", team.CreateKey)
teamGroup.GET("/keys", team.ListKeys)
teamGroup.POST("/keys/:id", team.UpdateKey)
teamGroup.DELETE("/keys/:id", team.DeleteKey)
teamGroup.POST("/users", team.CreateUser)
teamGroup.GET("/users", team.ListUsers)
teamGroup.POST("/users/:id/reset", team.ResetUserToken)
teamGroup.DELETE("/users/:id", team.DeleteUser)
teamGroup.GET("/1/usages", team.ListUsages)
}
api := r.Group("/api")
{
api.POST("/login", dashboard.HandleLogin)
}
srv := &http.Server{
Addr: ":8080",
Handler: r,
}
go func() {
// 服务启动
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// 等待中断信号来优雅地关闭服务器
quit := make(chan os.Signal, 1)
// kill (no param) default send syscall.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall.SIGKILL but can't be catch
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutdown Server ...")
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
db, _ := store.DB.DB()
db.Close()
// catching ctx.Done(). timeout of 1 seconds.
select {
case <-ctx.Done():
log.Println("timeout of 5 seconds.")
}
log.Println("Server exiting")
}