146 lines
3.4 KiB
Go
146 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"pblog/config"
|
|
"pblog/handlers"
|
|
"pblog/middleware"
|
|
"pblog/models"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func main() {
|
|
// 初始化数据库
|
|
config.InitDB()
|
|
|
|
// 自动迁移数据库
|
|
db := config.GetDB()
|
|
if err := models.AutoMigrate(db); err != nil {
|
|
log.Fatal("Failed to migrate database:", err)
|
|
}
|
|
|
|
// 创建Gin引擎
|
|
r := gin.Default()
|
|
|
|
// 添加CORS中间件
|
|
r.Use(func(c *gin.Context) {
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
|
c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
|
|
|
if c.Request.Method == "OPTIONS" {
|
|
c.AbortWithStatus(http.StatusOK)
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
})
|
|
|
|
// 静态文件服务
|
|
r.Static("/uploads", "./uploads")
|
|
|
|
// API路由组
|
|
api := r.Group("/api")
|
|
|
|
// 认证相关路由
|
|
auth := api.Group("/auth")
|
|
{
|
|
auth.POST("/register", handlers.Register)
|
|
auth.POST("/login", handlers.Login)
|
|
auth.POST("/refresh", handlers.RefreshToken)
|
|
}
|
|
|
|
// 需要认证的路由
|
|
authenticated := api.Group("/")
|
|
authenticated.Use(middleware.AuthMiddleware())
|
|
{
|
|
authenticated.GET("/profile", handlers.GetProfile)
|
|
}
|
|
|
|
// 文章相关路由
|
|
articles := api.Group("/articles")
|
|
{
|
|
// 公开路由
|
|
articles.GET("", handlers.GetArticles)
|
|
articles.GET("/:slug", middleware.OptionalAuthMiddleware(), handlers.GetArticle)
|
|
|
|
// 需要认证的路由
|
|
articlesAuth := articles.Group("/")
|
|
articlesAuth.Use(middleware.AuthMiddleware())
|
|
{
|
|
articlesAuth.POST("", handlers.CreateArticle)
|
|
articlesAuth.PUT("/:id", handlers.UpdateArticle)
|
|
articlesAuth.DELETE("/:id", handlers.DeleteArticle)
|
|
articlesAuth.GET("/:id/versions", handlers.GetArticleVersions)
|
|
}
|
|
}
|
|
|
|
// 评论相关路由
|
|
comments := api.Group("/comments")
|
|
{
|
|
// 公开路由
|
|
comments.GET("/article/:article_id", handlers.GetComments)
|
|
comments.GET("/:id", handlers.GetComment)
|
|
|
|
// 需要认证的路由
|
|
commentsAuth := comments.Group("/")
|
|
commentsAuth.Use(middleware.AuthMiddleware())
|
|
{
|
|
commentsAuth.POST("/article/:article_id", handlers.AddComment)
|
|
commentsAuth.PUT("/:id", handlers.UpdateComment)
|
|
commentsAuth.DELETE("/:id", handlers.DeleteComment)
|
|
}
|
|
}
|
|
|
|
// 媒体相关路由
|
|
media := api.Group("/media")
|
|
media.Use(middleware.AuthMiddleware())
|
|
{
|
|
media.POST("/upload", handlers.UploadMedia)
|
|
media.GET("", handlers.GetMediaList)
|
|
media.GET("/:id", handlers.GetMediaInfo)
|
|
media.DELETE("/:id", handlers.DeleteMedia)
|
|
}
|
|
|
|
// 管理员相关路由
|
|
admin := api.Group("/admin")
|
|
admin.Use(middleware.AuthMiddleware())
|
|
admin.Use(middleware.AdminMiddleware())
|
|
{
|
|
// 用户管理
|
|
admin.GET("/users", handlers.GetUsers)
|
|
admin.PUT("/users/:id/role", handlers.UpdateUserRole)
|
|
admin.DELETE("/users/:id", handlers.DeleteUser)
|
|
|
|
// 文章管理
|
|
admin.DELETE("/articles", handlers.BatchDeleteArticles)
|
|
|
|
// 评论管理
|
|
admin.POST("/comments/manage", handlers.ManageComments)
|
|
|
|
// 标签管理
|
|
admin.GET("/tags", handlers.GetTags)
|
|
admin.DELETE("/tags/:id", handlers.DeleteTag)
|
|
|
|
// 系统统计
|
|
admin.GET("/stats", handlers.GetSystemStats)
|
|
admin.GET("/activities", handlers.GetRecentActivities)
|
|
}
|
|
|
|
// 健康检查
|
|
r.GET("/health", func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "ok",
|
|
"message": "PBlog API is running",
|
|
})
|
|
})
|
|
|
|
// 启动服务器
|
|
log.Println("Server starting on :8080")
|
|
if err := r.Run(":8080"); err != nil {
|
|
log.Fatal("Failed to start server:", err)
|
|
}
|
|
}
|