Features: - Tag system with multi-language support (Chinese/English) - Auto-create tags when associating with articles - Tag filtering on homepage with visual indicators - Full-text search across title, summary, and content - Tag cloud sidebar showing article counts - Search page with results display Technical Implementation: - Database models: Tag, ArticleTag (many-to-many) - Tag count caching for performance - Automatic tag slug generation - Responsive design with mobile support - I18n support for all new UI elements Bug Fix: - Fixed SQL column ambiguity error in tag filtering - Added table prefix to ORDER BY clause in publishedArticleOrder Routes: - GET /search - Search results page - GET /?tag=<slug> - Filter articles by tag - GET /api/articles?tag=<slug> - API with tag filter support Templates: - Added tag input field to article create/edit form - Added search box to homepage header - Added tag cloud sidebar on homepage and search page - Created new search results page template Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
170 lines
6.0 KiB
Go
170 lines
6.0 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("/search", handlers.SearchPage(db))
|
|
router.GET("/api/articles", handlers.HomeArticlesAPI(db))
|
|
router.GET("/rss", handlers.RSSFeed(db))
|
|
router.GET("/feed", handlers.RSSFeed(db))
|
|
router.GET("/login", handlers.LoginPage())
|
|
router.POST("/login", handlers.Login(db))
|
|
router.GET("/register", handlers.RegisterPage(db))
|
|
router.POST("/register", handlers.Register(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("/navlinks", handlers.NavLinksSettingsPage(db))
|
|
settings.POST("/navlinks", handlers.NavLinksSettingsSave(db))
|
|
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 admin analytics routes (reading statistics).
|
|
analytics := router.Group("/admin/analytics")
|
|
analytics.Use(middleware.AuthRequired(), middleware.AdminRequired(db))
|
|
{
|
|
analytics.GET("/views", handlers.ViewAnalyticsPage(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)
|
|
}
|
|
}
|