blog_backend_api/main.go

105 lines
2.8 KiB
Go
Raw Normal View History

2019-01-25 09:11:15 +00:00
package main
import (
2019-04-07 04:25:07 +00:00
"background/config"
"background/controller"
"background/controller/middle"
"background/db"
"background/logs"
2019-01-25 09:11:15 +00:00
"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"
)
2019-03-07 06:06:29 +00:00
var (
userController = controller.UserController{}
mailContoller = controller.MailController{}
)
2019-03-12 06:01:25 +00:00
func InitConfig() {
2019-03-07 06:06:29 +00:00
e := config.Init("user.yaml")
if nil != e {
log.Println(e.Error())
}
}
2019-03-07 02:36:09 +00:00
func InitMysql() {
2019-01-25 09:11:15 +00:00
c := config.GetMysqlConfig()
2019-03-07 02:36:09 +00:00
if c == nil {
2019-01-25 09:11:15 +00:00
logs.Error("cannnot connect mysql server")
2019-03-07 02:36:09 +00:00
} else {
2019-01-25 09:11:15 +00:00
db.Init()
}
}
2019-03-12 06:01:25 +00:00
func InitRedis() {
2019-03-07 06:06:29 +00:00
e := config.InitRedis()
if nil != e {
logs.Error(e.Error())
return
}
}
2019-03-12 06:01:25 +00:00
func InitLogs() {
2019-03-07 06:06:29 +00:00
logs.Init(config.GetLogConfig().Dir, config.GetLogConfig().File, config.GetLogConfig().Level, config.GetLogConfig().SaveFile)
}
2019-02-23 06:19:29 +00:00
func CORSMiddleware(c *gin.Context) {
2019-03-07 02:36:09 +00:00
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")
2019-02-23 06:19:29 +00:00
2019-03-07 02:36:09 +00:00
if c.Request.Method == "OPTIONS" {
log.Println("OPTIONS")
c.AbortWithStatus(200)
} else {
c.Next()
}
2019-02-23 06:19:29 +00:00
}
2019-01-25 09:11:15 +00:00
2019-03-07 02:36:09 +00:00
func main() {
2019-03-07 06:06:29 +00:00
InitConfig()
InitLogs()
InitRedis()
InitMysql()
2019-06-23 11:33:45 +00:00
mgo := db.GetMongoDb()
mgo = mgo
config.GetMongoDBConfig()
2019-01-25 09:11:15 +00:00
r := gin.Default()
2019-01-29 13:57:53 +00:00
store := sessions.NewCookieStore([]byte("secret123"))
2019-03-07 06:06:29 +00:00
r.Use(sessions.Middleware("sess_store", store))
2019-02-23 06:19:29 +00:00
r.Use(CORSMiddleware)
2019-03-07 06:06:29 +00:00
api := r.Group("/api")
2019-01-25 09:11:15 +00:00
{
/** 添加或修改用户 **/
2019-03-27 07:03:45 +00:00
api.POST("/user", userController.SetUser)
2019-01-25 09:11:15 +00:00
/** 删除用户 **/
2019-03-27 07:03:45 +00:00
api.DELETE("/user", userController.DelUser)
2019-01-25 09:11:15 +00:00
/** 获取单独用户详情信息 methods(id) **/
2019-04-07 04:25:07 +00:00
api.GET("/user", middle.AuthMiddle,userController.GetUser)
2019-01-25 09:11:15 +00:00
/** 获取所有用户 **/
2019-04-07 04:25:07 +00:00
api.GET("/users", middle.AuthMiddle,userController.Users)
api.POST("/search_users",middle.AuthMiddle,userController.SerarchUsers)
2019-01-25 09:11:15 +00:00
/** 用户登录 **/
2019-03-27 07:03:45 +00:00
api.POST("/login", userController.Login)
2019-01-25 09:11:15 +00:00
/** 用户注册 **/
2019-03-27 07:03:45 +00:00
api.POST("/register", userController.Register)
2019-01-25 09:11:15 +00:00
/** 用户退出登陆 **/
2019-04-07 04:25:07 +00:00
api.GET("/logout", middle.AuthMiddle,userController.Logout)
2019-03-27 07:03:45 +00:00
api.POST("/verify", mailContoller.OnSendEmailCode)
2019-04-07 04:25:07 +00:00
/** 修改密码**/
api.POST("modify_pass",middle.AuthMiddle,userController.ModifyPasswd)
2019-05-16 10:05:20 +00:00
2019-05-17 09:45:50 +00:00
api.POST("articles",controller.GetArticles)
2019-05-16 10:05:20 +00:00
api.PUT("article",controller.AddArticle) // 添加文章
2019-05-17 09:45:50 +00:00
api.GET("article_type",controller.ArticlesType) //获取所有文章分类
2019-03-07 02:36:09 +00:00
}
2019-04-07 04:25:07 +00:00
2019-03-07 06:06:29 +00:00
e := r.Run(":" + strconv.Itoa(config.GetPort()))
if nil != e {
2019-03-07 02:36:09 +00:00
log.Print(e.Error())
2019-01-25 09:11:15 +00:00
}
}