2024-08-19 21:37:34 +08:00
|
|
|
package main
|
|
|
|
|
2024-08-20 14:01:55 +08:00
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"wqyblog_api_go/db"
|
|
|
|
"wqyblog_api_go/handlers"
|
|
|
|
)
|
2024-08-19 21:37:34 +08:00
|
|
|
|
|
|
|
func main() {
|
2024-08-20 14:01:55 +08:00
|
|
|
db.Init()
|
|
|
|
|
|
|
|
r := gin.Default()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-08-19 21:37:34 +08:00
|
|
|
}
|