Add four DB-backed configuration tables for site-wide settings: site_settings (logo, top-left title, header/footer text with zh/en variants), upload_configs (master switch, default size, storage dir), upload_file_types (per-extension whitelist with per-type size limits), and download_baseurls (multiple download sources with priority/default). - Models, AutoMigrate, and first-run seed (18 common file types) - Process-level config cache warmed at startup, refreshed on admin save - SetUserContext injects cached site settings into templates - base.html renders logo (local upload or external URL), title, header banner, and footer with i18n fallback - Upload validator drives avatar uploads (form + AJAX) through the configured switch/type/size policy - Admin pages for site/upload/download settings with i18n keys Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
99 lines
2.9 KiB
Go
99 lines
2.9 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))
|
|
|
|
// Protected admin routes.
|
|
admin := router.Group("/admin")
|
|
admin.Use(middleware.AuthRequired())
|
|
{
|
|
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 settings routes (platform configuration).
|
|
settings := router.Group("/admin/settings")
|
|
settings.Use(middleware.AuthRequired())
|
|
{
|
|
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))
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|