- Add AdminRequired middleware to all /admin routes for proper authorization - Restrict admin panel, comments, users, settings to admin role only - Create separate /my/articles routes for regular users to manage their own articles - Add MyArticlesPage handler for users to view/edit/delete their own content - Update navigation menus with role-based visibility - Admin dropdown shows only 'Admin Panel' link, other features in dashboard - Regular users see 'My Articles' link in profile dropdown - Add i18n translations for 'my_articles' and 'comment_manage' keys - Fix permission boundary issues where author role could access admin settings Security improvements: - Platform settings now require admin role - Comment moderation requires admin role - User management requires admin role - Regular users can only manage their own articles Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
155 lines
5.4 KiB
Go
155 lines
5.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/gin-contrib/sessions"
|
|
"github.com/gin-contrib/sessions/cookie"
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"go_blog/config"
|
|
"go_blog/handlers"
|
|
"go_blog/middleware"
|
|
"go_blog/models"
|
|
)
|
|
|
|
func main() {
|
|
// 1. Load configuration (auto-creates if missing).
|
|
cfg := config.LoadConfig()
|
|
|
|
// 2. Initialize the database (auto-migrates, seeds admin).
|
|
db := models.InitDB(cfg)
|
|
|
|
// 2b. Warm the platform configuration cache from the database.
|
|
models.LoadConfigCache(db)
|
|
|
|
// 3. Create session store (cookie-based).
|
|
store := cookie.NewStore([]byte(cfg.Secret))
|
|
store.Options(sessions.Options{
|
|
Path: "/",
|
|
MaxAge: 86400, // 24 hours
|
|
HttpOnly: true, // prevent XSS access
|
|
Secure: false, // set true in production with HTTPS
|
|
})
|
|
|
|
// 4. Create Gin router.
|
|
router := gin.Default()
|
|
|
|
// 5. Load HTML templates.
|
|
router.LoadHTMLGlob("templates/**/*.html")
|
|
|
|
// 6. Serve uploaded files (avatars etc.) from the storage path.
|
|
router.Static("/uploads", cfg.Path)
|
|
|
|
// 6. Global session middleware.
|
|
router.Use(sessions.Sessions("blog_session", store))
|
|
|
|
// 7. Global context middleware (sets IsLoggedIn, Username for templates).
|
|
router.Use(middleware.SetUserContext(db))
|
|
|
|
// 8. Register routes.
|
|
router.GET("/", handlers.HomePage(db))
|
|
router.GET("/login", handlers.LoginPage())
|
|
router.POST("/login", handlers.Login(db))
|
|
router.POST("/logout", handlers.Logout())
|
|
router.GET("/article/:slug", handlers.ArticleDetail(db))
|
|
router.POST("/article/:slug/comments", handlers.PostComment(db))
|
|
|
|
// Protected admin routes (admin role only).
|
|
admin := router.Group("/admin")
|
|
admin.Use(middleware.AuthRequired(), middleware.AdminRequired(db))
|
|
{
|
|
admin.GET("", handlers.AdminDashboard(db))
|
|
admin.GET("/articles", handlers.ArticleListPage(db))
|
|
admin.GET("/articles/new", handlers.ArticleCreatePage(db))
|
|
admin.POST("/articles/new", handlers.ArticleCreate(db))
|
|
admin.GET("/articles/:id/edit", handlers.ArticleEditPage(db))
|
|
admin.POST("/articles/:id/edit", handlers.ArticleUpdate(db))
|
|
admin.POST("/articles/:id/delete", handlers.ArticleDelete(db))
|
|
|
|
}
|
|
|
|
// Protected admin comment management routes (admin role only).
|
|
comments := router.Group("/admin/comments")
|
|
comments.Use(middleware.AuthRequired(), middleware.AdminRequired(db))
|
|
{
|
|
comments.GET("", handlers.CommentListPage(db))
|
|
comments.POST("/:id/approve", handlers.CommentApprove(db))
|
|
comments.POST("/:id/reject", handlers.CommentReject(db))
|
|
comments.POST("/:id/delete", handlers.CommentDelete(db))
|
|
}
|
|
|
|
// Protected admin user-management routes (admin role only).
|
|
users := router.Group("/admin/users")
|
|
users.Use(middleware.AuthRequired(), middleware.AdminRequired(db))
|
|
{
|
|
users.GET("", handlers.UserListPage(db))
|
|
users.GET("/new", handlers.UserCreatePage(db))
|
|
users.POST("/new", handlers.UserCreate(db))
|
|
users.GET("/:id/edit", handlers.UserEditPage(db))
|
|
users.POST("/:id/edit", handlers.UserUpdate(db))
|
|
users.POST("/:id/delete", handlers.UserDelete(db))
|
|
}
|
|
|
|
// Protected article attachment routes (admin role only).
|
|
attachments := router.Group("/admin/articles")
|
|
attachments.Use(middleware.AuthRequired(), middleware.AdminRequired(db))
|
|
{
|
|
attachments.POST("/attachments", handlers.UploadAttachment(db, cfg.Path))
|
|
attachments.POST("/attachments/:id/delete", handlers.DeleteAttachment(db, cfg.Path))
|
|
attachments.GET("/:id/attachments", handlers.ListAttachments(db))
|
|
}
|
|
|
|
// Protected admin settings routes (platform configuration).
|
|
settings := router.Group("/admin/settings")
|
|
settings.Use(middleware.AuthRequired(), middleware.AdminRequired(db))
|
|
{
|
|
settings.GET("/site", handlers.SiteSettingsPage(db))
|
|
settings.POST("/site", handlers.SiteSettingsSave(db, cfg.Path))
|
|
settings.GET("/upload", handlers.UploadSettingsPage(db))
|
|
settings.POST("/upload", handlers.UploadSettingsSave(db))
|
|
settings.GET("/download", handlers.DownloadSettingsPage(db))
|
|
settings.POST("/download", handlers.DownloadSettingsSave(db))
|
|
settings.GET("/comments", handlers.CommentSettingsPage(db))
|
|
settings.POST("/comments", handlers.CommentSettingsSave(db))
|
|
}
|
|
|
|
// Protected profile routes.
|
|
profile := router.Group("/profile")
|
|
profile.Use(middleware.AuthRequired())
|
|
{
|
|
profile.GET("", handlers.ProfilePage(db))
|
|
profile.POST("", handlers.UpdateProfile(db, cfg.Path))
|
|
profile.POST("/avatar", handlers.UploadAvatar(db, cfg.Path))
|
|
}
|
|
|
|
// Protected user article management routes (for non-admin users).
|
|
myArticles := router.Group("/my")
|
|
myArticles.Use(middleware.AuthRequired())
|
|
{
|
|
myArticles.GET("/articles", handlers.MyArticlesPage(db))
|
|
myArticles.GET("/articles/new", handlers.MyArticleCreatePage(db))
|
|
myArticles.POST("/articles/new", handlers.MyArticleCreate(db))
|
|
myArticles.GET("/articles/:id/edit", handlers.MyArticleEditPage(db))
|
|
myArticles.POST("/articles/:id/edit", handlers.MyArticleUpdate(db))
|
|
myArticles.POST("/articles/:id/delete", handlers.MyArticleDelete(db))
|
|
}
|
|
|
|
// Protected article attachment routes for user articles.
|
|
myAttachments := router.Group("/my/articles")
|
|
myAttachments.Use(middleware.AuthRequired())
|
|
{
|
|
myAttachments.POST("/attachments", handlers.UploadAttachment(db, cfg.Path))
|
|
myAttachments.POST("/attachments/:id/delete", handlers.DeleteAttachment(db, cfg.Path))
|
|
myAttachments.GET("/:id/attachments", handlers.ListAttachments(db))
|
|
}
|
|
|
|
// 9. Start the server.
|
|
addr := fmt.Sprintf(":%s", cfg.Port)
|
|
log.Printf("Go Blog starting on http://localhost%s", addr)
|
|
if err := router.Run(addr); err != nil {
|
|
log.Fatalf("Failed to start server: %v", err)
|
|
}
|
|
}
|