bbs-backend/main.go

36 lines
793 B
Go
Raw Normal View History

2024-11-19 21:08:28 +08:00
package main
import (
"bbs-backend/api/router"
"bbs-backend/config"
"bbs-backend/dal"
"fmt"
"github.com/gin-contrib/cors"
)
func main() {
// Load configuration
config.LoadConfig("config/config.yaml")
// Initialize the database
dal.InitDB()
// Migrate and seed the database
dal.MigrateAndSeedDB()
// Setup the router
r := router.SetupRouter()
// 添加 CORS 中间件
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:8080"}, // 允许的源
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}))
// Run the server
r.Run(fmt.Sprintf(":%d", config.GetConfig().Server.Port))
}