package main import ( "log" "yuyue/config" "yuyue/database" "yuyue/handlers" "yuyue/middleware" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/recover" ) func main() { // 加载配置 cfg := config.LoadConfig() // 连接数据库 database.ConnectDatabase(cfg) //// 初始化两个月的预约时间槽 //if err := utils.GenerateTwoMonthsTimeSlots(); err != nil { // log.Printf("生成时间槽失败: %v", err) //} else { // log.Println("✅ 成功生成最近两个月的预约时间槽") //} // 创建 Fiber 应用 app := fiber.New(fiber.Config{ AppName: "预约系统", }) // 中间件 app.Use(logger.New()) app.Use(recover.New()) app.Use(cors.New(cors.Config{ AllowOrigins: "*", AllowHeaders: "Origin, Content-Type, Accept, Authorization", AllowMethods: "GET, POST, PUT, DELETE, OPTIONS", })) // 设置路由 setupRoutes(app) // 启动服务器 log.Printf("🚀 服务器启动在端口 %s", cfg.ServerPort) log.Fatal(app.Listen(":" + cfg.ServerPort)) } func setupRoutes(app *fiber.App) { // 公开路由 auth := app.Group("/api/auth") auth.Post("/register", handlers.Register) auth.Post("/send-code", handlers.SendVerificationCode) // 发送验证码 auth.Post("/verification-login", handlers.VerificationLogin) // 验证码登录 auth.Post("/one-click-login", handlers.OneClickLogin) // 一键登录 // 时间槽相关路由(公开) timeslots := app.Group("/api/timeslots") timeslots.Get("/", handlers.GetTimeSlots) timeslots.Get("/:id", handlers.GetTimeSlot) // 需要认证的路由 protected := app.Group("/api") protected.Use(middleware.AuthMiddleware()) // 用户相关路由 users := protected.Group("/users") users.Get("/profile", handlers.GetProfile) users.Put("/profile", handlers.UpdateProfile) // 预约相关路由 appointments := protected.Group("/appointments") appointments.Post("/", handlers.CreateAppointment) appointments.Get("/", handlers.GetAppointments) appointments.Get("/:id", handlers.GetAppointment) appointments.Delete("/:id", handlers.CancelAppointment) // 管理员路由 admin := protected.Group("/admin") admin.Use(middleware.AdminMiddleware()) // 管理时间槽 adminTimeSlots := admin.Group("/timeslots") adminTimeSlots.Post("/", handlers.CreateTimeSlot) adminTimeSlots.Put("/:id", handlers.UpdateTimeSlot) adminTimeSlots.Delete("/:id", handlers.DeleteTimeSlot) // 管理预约 adminAppointments := admin.Group("/appointments") adminAppointments.Put("/:id/status", handlers.UpdateAppointmentStatus) // 健康检查 app.Get("/health", func(c *fiber.Ctx) error { return c.JSON(fiber.Map{ "status": "ok", "message": "预约系统运行正常", }) }) }