37 lines
649 B
Go
37 lines
649 B
Go
package main
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"wqyblog_api_go/db"
|
|
"wqyblog_api_go/handlers"
|
|
)
|
|
|
|
func main() {
|
|
db.Init()
|
|
|
|
r := gin.Default()
|
|
|
|
// 开启跨域
|
|
r.Use(handlers.Cors())
|
|
|
|
r.GET("/", handlers.HalloWorld)
|
|
|
|
r.POST("/api/auth/login", handlers.Login)
|
|
|
|
r.GET("/api/posts", handlers.GetPosts)
|
|
r.GET("/api/posts/:id", handlers.GetPost)
|
|
|
|
authorized := r.Group("/")
|
|
authorized.Use(handlers.Authenticate)
|
|
{
|
|
authorized.POST("/api/posts", handlers.CreatePost)
|
|
authorized.PUT("/api/posts/:id", handlers.UpdatePost)
|
|
authorized.DELETE("/api/posts/:id", handlers.DeletePost)
|
|
}
|
|
|
|
err := r.Run(":8080")
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|