Files
goodBaby/main.go
T

50 lines
1.2 KiB
Go
Raw Normal View History

2025-10-06 16:23:56 +08:00
package main
import (
2026-04-05 10:36:22 +08:00
"crypto/rand"
"fmt"
2025-10-06 16:23:56 +08:00
2026-04-05 10:36:22 +08:00
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
2025-10-06 16:23:56 +08:00
"github.com/gin-gonic/gin"
2026-04-05 10:36:22 +08:00
"github.com/ssdomei232/goodBaby/api/account"
"github.com/ssdomei232/goodBaby/api/rule"
"github.com/ssdomei232/goodBaby/api/user"
2025-10-06 16:23:56 +08:00
)
func main() {
r := gin.Default()
2026-04-05 10:36:22 +08:00
store := cookie.NewStore(generateRandomKey(32))
r.Use(sessions.Sessions("goodbaby-session", store))
v1 := r.Group("/api/v1")
{
v1.POST("/user/registry", user.HandleRegistry)
v1.POST("/user/login", user.HandleLogin)
}
// 需要认证的路由组
authorized := v1.Group("/")
authorized.Use(user.AuthMiddleware())
{
authorized.GET("/user/info", user.HandleGetUserInfo)
authorized.GET("/rules", rule.HandleGetAllRules)
authorized.POST("/rules", rule.HandleCreateRule)
2026-04-05 22:28:38 +08:00
authorized.DELETE("/rules/:ruleID", rule.HandleDeleteRule)
2026-04-05 10:36:22 +08:00
authorized.GET("/accounts", account.HandleGetAllAccounts)
authorized.POST("/accounts", account.HandleAddAccount)
2026-04-05 22:46:11 +08:00
authorized.GET("/accounts/:accountID/check", account.HandleCheckDeleteAccount)
2026-04-05 10:36:22 +08:00
}
2025-10-06 16:23:56 +08:00
r.Run(":8088")
}
2025-10-06 20:02:47 +08:00
2026-04-05 10:36:22 +08:00
func generateRandomKey(length int) []byte {
key := make([]byte, length)
_, err := rand.Read(key)
2025-10-07 14:49:00 +08:00
if err != nil {
2026-04-05 10:36:22 +08:00
panic(fmt.Sprintf("无法生成随机密钥: %v", err))
2025-10-07 14:49:00 +08:00
}
2026-04-05 10:36:22 +08:00
return key
2025-10-07 14:49:00 +08:00
}