Files
asset_assistant/backend/user/src/main.go
2025-11-13 17:56:03 +08:00

53 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"user/db"
"user/logger"
"user/logic"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
"go.uber.org/zap"
"time"
)
func main() {
logger.Init()
zap.L().Info("🚀 用户服务初始化")
zap.L().Info("⌛️ 数据库初始化开始")
db.Init()
defer db.DB.Close() // 应用退出时关闭连接
zap.L().Info("✅ 数据库初始化成功")
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
// 配置跨域中间件
r.Use(cors.New(cors.Config{
// 允许所有来源(生产环境建议指定具体域名)
AllowOrigins: []string{"*"},
// 允许的请求方法
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
// 允许的请求头
AllowHeaders: []string{"Origin", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization", "X-LoginRequest-ID"},
// 允许前端读取的响应头
ExposeHeaders: []string{"Content-Length"},
// 是否允许携带cookie
AllowCredentials: true,
// 预检请求的缓存时间
MaxAge: 12 * time.Hour,
}))
zap.L().Info("✅ 配置跨域中间件完成")
// 登录接口
r.POST("/user/login", logic.LoginHandler)
zap.L().Info("✅ 登录接口注册完成: POST /user/login")
// 注册接口
r.POST("/user/register", logic.RegisterHandler)
zap.L().Info("✅ 注册接口注册完成: POST /user/register")
// 启动服务监听80端口
zap.L().Info("✅ 服务启动在80端口")
r.Run(":80")
}