background/main.go

77 lines
2.2 KiB
Go
Raw Normal View History

2019-01-25 09:11:15 +00:00
package main
import (
"github.com/gin-gonic/gin"
2019-01-29 13:57:53 +00:00
"github.com/tommy351/gin-sessions"
2019-01-25 09:11:15 +00:00
"log"
"strconv"
"user/controller"
"user/config"
2019-02-23 06:19:29 +00:00
"user/controller/mail"
2019-01-25 09:11:15 +00:00
"user/db"
"user/logs"
)
func InitMysql() {
c := config.GetMysqlConfig()
if c == nil{
logs.Error("cannnot connect mysql server")
}else {
db.Init()
}
}
2019-02-23 06:19:29 +00:00
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)
log.Println(c.Request.Header.Get("Origin"))
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()
}
}
2019-01-25 09:11:15 +00:00
func main() {
e := config.Init("user.yaml")
if nil != e{
log.Println(e.Error())
}
2019-01-29 13:57:53 +00:00
logs.Init(config.GetLogConfig().Dir,config.GetLogConfig().File,config.GetLogConfig().Level,config.GetLogConfig().SaveFile)
2019-01-25 09:11:15 +00:00
db.Init()
2019-01-31 08:57:22 +00:00
e = config.InitRedis()
if nil != e{
logs.Error(e.Error())
return
}
2019-01-25 09:11:15 +00:00
r := gin.Default()
2019-01-29 13:57:53 +00:00
store := sessions.NewCookieStore([]byte("secret123"))
r.Use(sessions.Middleware("my_session", store))
2019-02-23 06:19:29 +00:00
r.Use(CORSMiddleware)
2019-01-25 09:11:15 +00:00
{
/** 添加或修改用户 **/
r.POST("/api/user", controller.SetUser)
/** 删除用户 **/
r.DELETE("/api/user", controller.DelUser)
/** 获取单独用户详情信息 methods(id) **/
r.GET("/api/user", controller.GetUser)
/** 获取所有用户 **/
2019-02-23 06:19:29 +00:00
r.GET("/api/users", controller.Users)
r.POST("/api/search_users", controller.SerarchUsers)
2019-01-25 09:11:15 +00:00
/** 用户登录 **/
r.POST("/api/login", controller.Login)
/** 用户注册 **/
r.POST("/api/register", controller.Register)
/** 用户退出登陆 **/
r.GET("/api/logout", controller.Logout)
2019-02-23 06:19:29 +00:00
r.POST("/api/verify",mail.OnSendEmailCode)
2019-01-25 09:11:15 +00:00
}
r.Run(":" + strconv.Itoa(config.GetPort()))
}