package main import ( "github.com/gin-gonic/gin" "github.com/tommy351/gin-sessions" "log" "strconv" "user/config" "user/controller" "user/controller/mail" "user/db" "user/logs" ) func InitMysql() { c := config.GetMysqlConfig() if c == nil { logs.Error("cannnot connect mysql server") } else { db.Init() } } func CORSMiddleware(c *gin.Context) { ori := c.Request.Header.Get("Origin") c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") c.Writer.Header().Set("Access-Control-Allow-Origin", ori) c.Writer.Header().Set("Access-Control-Max-Age", "86400") c.Writer.Header().Set("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding, x-access-token") c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length") c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") if c.Request.Method == "OPTIONS" { log.Println("OPTIONS") c.AbortWithStatus(200) } else { c.Next() } } func main() { e := config.Init("user.yaml") if nil != e { log.Println(e.Error()) } logs.Init(config.GetLogConfig().Dir, config.GetLogConfig().File, config.GetLogConfig().Level, config.GetLogConfig().SaveFile) db.Init() e = config.InitRedis() if nil != e { logs.Error(e.Error()) return } r := gin.Default() store := sessions.NewCookieStore([]byte("secret123")) r.Use(sessions.Middleware("my_session", store)) r.Use(CORSMiddleware) { /** 添加或修改用户 **/ r.POST("/api/user", controller.SetUser) /** 删除用户 **/ r.DELETE("/api/user", controller.DelUser) /** 获取单独用户详情信息 methods(id) **/ r.GET("/api/user", controller.GetUser) /** 获取所有用户 **/ r.GET("/api/users", controller.Users) r.POST("/api/search_users", controller.SerarchUsers) /** 用户登录 **/ r.POST("/api/login", controller.Login) /** 用户注册 **/ r.POST("/api/register", controller.Register) /** 用户退出登陆 **/ r.GET("/api/logout", controller.Logout) r.POST("/api/verify", mail.OnSendEmailCode) } e = r.Run(":" + strconv.Itoa(config.GetPort())) if nil != e{ log.Print(e.Error()) } }