From f5d570eb2daba3d5045cb569c7ce8829881228e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=B6=85?= <17805310388@139.com> Date: Thu, 22 May 2025 13:56:57 +0800 Subject: [PATCH] test manager --- main.go | 9 +- models/init.go | 7 +- routers/router.go | 8 ++ templates/index.tmpl | 31 +++++- themes/manager.go | 259 +++++++++++++++++++++++++++---------------- 5 files changed, 212 insertions(+), 102 deletions(-) diff --git a/main.go b/main.go index ab0f885..7f76d70 100644 --- a/main.go +++ b/main.go @@ -41,9 +41,14 @@ func main() { // 3. 初始化数据库 models.InitDatabase(conf) - + sqlDB, _ := models.DB.DB() + defer func() { + if err := sqlDB.Close(); err != nil { + slog.Error("数据库关闭异常", "error", err) + } + }() // 4. 初始化主题系统 - themeManager := themes.NewManager(conf.Theme.Current) + themeManager := themes.NewManager("web\\themes") if err := themeManager.LoadTheme(conf.Theme.Current); err != nil { slog.Error("主题系统初始化失败", "error", err) os.Exit(1) diff --git a/models/init.go b/models/init.go index 9b7b171..5f8bcf9 100644 --- a/models/init.go +++ b/models/init.go @@ -41,12 +41,7 @@ func InitDatabase(conf *config.Config) { slog.Error("数据库连接失败", "error", err) os.Exit(1) } - sqlDB, _ := db.DB() - defer func() { - if err := sqlDB.Close(); err != nil { - slog.Error("数据库关闭异常", "error", err) - } - }() + DB = db // 3. 自动迁移数据模型 db.AutoMigrate(&Account{}) diff --git a/routers/router.go b/routers/router.go index 98b3765..a457ff0 100644 --- a/routers/router.go +++ b/routers/router.go @@ -7,6 +7,7 @@ import ( "go_blog/serializers" "log" "net/http" + "time" "github.com/gin-gonic/gin" "gorm.io/gorm" @@ -91,6 +92,13 @@ func esSSE(c *gin.Context) { log.Panic("server not support") //浏览器不兼容 } + for { + // Simulate sending events every second + fmt.Fprintf(w, "data: %s\n\n", time.Now().Format(time.Stamp)) + w.(http.Flusher).Flush() + time.Sleep(1 * time.Second) + } + _, err := fmt.Fprintf(w, "id: aaa\ndata: %s\n\n", "dsdf") _, er1r := fmt.Fprintf(w, "event: connecttime\ndata: %s\n\n", "connecttime") if err != nil || er1r != nil { diff --git a/templates/index.tmpl b/templates/index.tmpl index a3e6e98..7ab57e6 100644 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -44,10 +44,39 @@ .header {} + +
- +