test manager
This commit is contained in:
9
main.go
9
main.go
@@ -41,9 +41,14 @@ func main() {
|
|||||||
|
|
||||||
// 3. 初始化数据库
|
// 3. 初始化数据库
|
||||||
models.InitDatabase(conf)
|
models.InitDatabase(conf)
|
||||||
|
sqlDB, _ := models.DB.DB()
|
||||||
|
defer func() {
|
||||||
|
if err := sqlDB.Close(); err != nil {
|
||||||
|
slog.Error("数据库关闭异常", "error", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
// 4. 初始化主题系统
|
// 4. 初始化主题系统
|
||||||
themeManager := themes.NewManager(conf.Theme.Current)
|
themeManager := themes.NewManager("web\\themes")
|
||||||
if err := themeManager.LoadTheme(conf.Theme.Current); err != nil {
|
if err := themeManager.LoadTheme(conf.Theme.Current); err != nil {
|
||||||
slog.Error("主题系统初始化失败", "error", err)
|
slog.Error("主题系统初始化失败", "error", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
@@ -41,12 +41,7 @@ func InitDatabase(conf *config.Config) {
|
|||||||
slog.Error("数据库连接失败", "error", err)
|
slog.Error("数据库连接失败", "error", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
sqlDB, _ := db.DB()
|
|
||||||
defer func() {
|
|
||||||
if err := sqlDB.Close(); err != nil {
|
|
||||||
slog.Error("数据库关闭异常", "error", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
DB = db
|
DB = db
|
||||||
// 3. 自动迁移数据模型
|
// 3. 自动迁移数据模型
|
||||||
db.AutoMigrate(&Account{})
|
db.AutoMigrate(&Account{})
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"go_blog/serializers"
|
"go_blog/serializers"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -91,6 +92,13 @@ func esSSE(c *gin.Context) {
|
|||||||
log.Panic("server not support") //浏览器不兼容
|
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")
|
_, err := fmt.Fprintf(w, "id: aaa\ndata: %s\n\n", "dsdf")
|
||||||
_, er1r := fmt.Fprintf(w, "event: connecttime\ndata: %s\n\n", "connecttime")
|
_, er1r := fmt.Fprintf(w, "event: connecttime\ndata: %s\n\n", "connecttime")
|
||||||
if err != nil || er1r != nil {
|
if err != nil || er1r != nil {
|
||||||
|
|||||||
@@ -44,10 +44,39 @@
|
|||||||
|
|
||||||
.header {}
|
.header {}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
|
||||||
|
window.addEventListener('load', (event) => {
|
||||||
|
|
||||||
|
var evsrc = new EventSource("http://localhost:8090/events");
|
||||||
|
evsrc.onmessage = function (ev) {
|
||||||
|
console.log("readyStateOnmessage = " + ev.currentTarget.readyState);
|
||||||
|
console.log("getdata = " + ev.data + "lastEventId = " + ev.lastEventId);
|
||||||
|
//document.getElementById("log")
|
||||||
|
//.insertAdjacentHTML("beforeend", "<li>" + ev.data + "</li>");
|
||||||
|
}
|
||||||
|
evsrc.onopen = function (event) {
|
||||||
|
console.log("Connection open ...");
|
||||||
|
};
|
||||||
|
evsrc.onerror = function (ev) {
|
||||||
|
console.log("readyStateOnError = " + ev.currentTarget.readyState);
|
||||||
|
//evsrc.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
evsrc.addEventListener('connecttime', function (event) {
|
||||||
|
console.log("Start time: " + event.data );
|
||||||
|
}, false);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
<h1>SSE test</h1>
|
||||||
|
<div>
|
||||||
|
<ul id="log">
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
<div class="header">
|
<div class="header">
|
||||||
<div class="layui-main">
|
<div class="layui-main">
|
||||||
<h1><a class="logo" href="https://www.hanxiaonuan.cn/">韩小暖的博客</a></h1>
|
<h1><a class="logo" href="https://www.hanxiaonuan.cn/">韩小暖的博客</a></h1>
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package themes
|
package themes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
@@ -10,154 +9,228 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"golang.org/x/exp/slog"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 主题元数据结构
|
const (
|
||||||
|
defaultLayout = "base.html"
|
||||||
|
themeConfigFile = "theme.yaml"
|
||||||
|
requiredTemplates = "home.html,post.html"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ThemeMeta 主题元数据
|
||||||
type ThemeMeta struct {
|
type ThemeMeta struct {
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
Author string `yaml:"author"`
|
|
||||||
Version string `yaml:"version"`
|
Version string `yaml:"version"`
|
||||||
|
Author string `yaml:"author"`
|
||||||
Description string `yaml:"description"`
|
Description string `yaml:"description"`
|
||||||
|
License string `yaml:"license"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// 主题管理器
|
// ThemeManager 主题管理器
|
||||||
type ThemeManager struct {
|
type ThemeManager struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
currentTheme string
|
themesDir string
|
||||||
templates map[string]*template.Template
|
currentTheme string
|
||||||
baseTemplates embed.FS // 可选:嵌入基础模板
|
templates *template.Template
|
||||||
themesDir string
|
staticFS gin.StaticFS
|
||||||
|
meta ThemeMeta
|
||||||
|
funcMap template.FuncMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建新主题管理器
|
// NewManager 创建主题管理器实例
|
||||||
func NewManager(themesDir string) *ThemeManager {
|
func NewManager(themesDir, defaultTheme string) (*ThemeManager, error) {
|
||||||
return &ThemeManager{
|
m := &ThemeManager{
|
||||||
templates: make(map[string]*template.Template),
|
|
||||||
themesDir: themesDir,
|
themesDir: themesDir,
|
||||||
|
funcMap: defaultFuncMap(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := m.LoadTheme(defaultTheme); err != nil {
|
||||||
|
return nil, fmt.Errorf("初始化默认主题失败: %w", err)
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 默认模板函数
|
||||||
|
func defaultFuncMap() template.FuncMap {
|
||||||
|
return template.FuncMap{
|
||||||
|
"safeHTML": func(s string) template.HTML { return template.HTML(s) },
|
||||||
|
"dateFmt": func(t time.Time) string { return t.Format("2006-01-02") },
|
||||||
|
"assetPath": func(name string) string {
|
||||||
|
return fmt.Sprintf("/theme/static/%s?t=%d", name, time.Now().Unix())
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 核心方法:加载主题
|
// LoadTheme 加载指定主题
|
||||||
func (m *ThemeManager) LoadTheme(themeName string) error {
|
func (m *ThemeManager) LoadTheme(name string) error {
|
||||||
|
start := time.Now()
|
||||||
|
themePath := filepath.Join(m.themesDir, name)
|
||||||
|
|
||||||
|
// 验证主题完整性
|
||||||
|
if err := validateTheme(themePath); err != nil {
|
||||||
|
return fmt.Errorf("主题验证失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加载元数据
|
||||||
|
meta, err := loadThemeMeta(themePath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("元数据加载失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析模板
|
||||||
|
tpls, err := parseTemplates(themePath, m.funcMap)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("模板解析失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 准备静态文件系统
|
||||||
|
staticPath := filepath.Join(themePath, "static")
|
||||||
|
staticFS := gin.Dir(staticPath, false)
|
||||||
|
|
||||||
|
// 原子化更新状态
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
// 1. 验证主题目录结构
|
m.currentTheme = name
|
||||||
themePath := filepath.Join(m.themesDir, themeName)
|
|
||||||
if !isValidTheme(themePath) {
|
|
||||||
return fmt.Errorf("invalid theme structure: %s", themeName)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. 加载模板文件
|
|
||||||
tpls, err := m.parseTemplates(themePath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("template parsing failed: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. 更新当前主题
|
|
||||||
m.currentTheme = themeName
|
|
||||||
m.templates = tpls
|
m.templates = tpls
|
||||||
|
m.staticFS = staticFS
|
||||||
|
m.meta = meta
|
||||||
|
|
||||||
|
slog.Info("主题加载成功",
|
||||||
|
"theme", name,
|
||||||
|
"templates", len(tpls.Templates()),
|
||||||
|
"duration", time.Since(start),
|
||||||
|
)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取当前主题名称
|
// 验证主题完整性
|
||||||
func (m *ThemeManager) CurrentTheme() string {
|
func validateTheme(themePath string) error {
|
||||||
m.mu.RLock()
|
// 检查必须文件
|
||||||
defer m.mu.RUnlock()
|
|
||||||
return m.currentTheme
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取编译后的模板
|
|
||||||
func (m *ThemeManager) GetTemplate(name string) *template.Template {
|
|
||||||
m.mu.RLock()
|
|
||||||
defer m.mu.RUnlock()
|
|
||||||
return m.templates[name]
|
|
||||||
}
|
|
||||||
|
|
||||||
// 注册静态文件路由(Gin框架)
|
|
||||||
func (m *ThemeManager) RegisterStaticRoutes(router *gin.Engine) {
|
|
||||||
staticPath := filepath.Join(m.themesDir, m.currentTheme, "static")
|
|
||||||
router.StaticFS("/theme/static", gin.Dir(staticPath, false))
|
|
||||||
}
|
|
||||||
|
|
||||||
// 校验主题完整性
|
|
||||||
func isValidTheme(themePath string) bool {
|
|
||||||
requiredFiles := []string{
|
requiredFiles := []string{
|
||||||
"theme.yaml",
|
themeConfigFile,
|
||||||
"templates/home.html",
|
filepath.Join("templates", "base.html"),
|
||||||
"templates/post.html",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, f := range strings.Split(requiredTemplates, ",") {
|
||||||
|
requiredFiles = append(requiredFiles,
|
||||||
|
filepath.Join("templates", f))
|
||||||
|
}
|
||||||
|
|
||||||
|
var missing []string
|
||||||
for _, f := range requiredFiles {
|
for _, f := range requiredFiles {
|
||||||
if _, err := os.Stat(filepath.Join(themePath, f)); os.IsNotExist(err) {
|
if _, err := os.Stat(filepath.Join(themePath, f)); err != nil {
|
||||||
return false
|
if os.IsNotExist(err) {
|
||||||
|
missing = append(missing, f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
|
||||||
|
if len(missing) > 0 {
|
||||||
|
return fmt.Errorf("缺失必要文件: %v", missing)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析模板文件(支持布局继承)
|
// 加载主题元数据
|
||||||
func (m *ThemeManager) parseTemplates(themePath string) (map[string]*template.Template, error) {
|
func loadThemeMeta(themePath string) (ThemeMeta, error) {
|
||||||
templates := make(map[string]*template.Template)
|
configPath := filepath.Join(themePath, themeConfigFile)
|
||||||
|
data, err := os.ReadFile(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return ThemeMeta{}, err
|
||||||
|
}
|
||||||
|
|
||||||
// 加载公共基础模板(可选)
|
var meta ThemeMeta
|
||||||
baseTpl := template.New("base").Funcs(template.FuncMap{
|
if err := yaml.Unmarshal(data, &meta); err != nil {
|
||||||
"safeHTML": func(s string) template.HTML { return template.HTML(s) },
|
return ThemeMeta{}, err
|
||||||
})
|
}
|
||||||
|
|
||||||
// 从主题目录加载模板
|
if meta.Name == "" {
|
||||||
|
return ThemeMeta{}, errors.New("主题名称不能为空")
|
||||||
|
}
|
||||||
|
return meta, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析模板文件
|
||||||
|
func parseTemplates(themePath string, funcMap template.FuncMap) (*template.Template, error) {
|
||||||
tplDir := filepath.Join(themePath, "templates")
|
tplDir := filepath.Join(themePath, "templates")
|
||||||
err := filepath.WalkDir(tplDir, func(path string, d fs.DirEntry, err error) error {
|
baseFile := filepath.Join(tplDir, defaultLayout)
|
||||||
if err != nil || d.IsDir() || !strings.HasSuffix(path, ".html") {
|
|
||||||
|
// 先解析基础模板
|
||||||
|
baseTpl, err := template.New("base").
|
||||||
|
Funcs(funcMap).
|
||||||
|
ParseFiles(baseFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("基础模板解析失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 遍历模板目录
|
||||||
|
return filepath.WalkDir(tplDir, func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil || d.IsDir() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取模板内容
|
if path == baseFile { // 已处理基础模板
|
||||||
content, err := os.ReadFile(path)
|
return nil
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成模板名称(相对路径)
|
if filepath.Ext(path) != ".html" {
|
||||||
name := strings.TrimPrefix(path, tplDir+string(filepath.Separator))
|
return nil
|
||||||
name = strings.TrimSuffix(name, filepath.Ext(name))
|
}
|
||||||
|
|
||||||
// 克隆基础模板并解析
|
// 克隆基础模板并解析
|
||||||
tpl := template.Must(baseTpl.Clone())
|
_, err = baseTpl.Clone().
|
||||||
tpl, err = tpl.Parse(string(content))
|
ParseFiles(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parse error in %s: %w", name, err)
|
return fmt.Errorf("解析 %s 失败: %w", filepath.Base(path), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
templates[name] = tpl
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(templates) == 0 {
|
|
||||||
return nil, errors.New("no valid templates found")
|
|
||||||
}
|
|
||||||
|
|
||||||
return templates, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取可用主题列表
|
// GetTemplate 获取指定模板
|
||||||
|
func (m *ThemeManager) GetTemplate(name string) *template.Template {
|
||||||
|
m.mu.RLock()
|
||||||
|
defer m.mu.RUnlock()
|
||||||
|
|
||||||
|
return m.templates.Lookup(name + ".html")
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterStatic 注册静态资源路由
|
||||||
|
func (m *ThemeManager) RegisterStatic(r *gin.Engine) {
|
||||||
|
m.mu.RLock()
|
||||||
|
defer m.mu.RUnlock()
|
||||||
|
|
||||||
|
r.StaticFS("/theme/static", m.staticFS)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CurrentThemeInfo 返回当前主题信息
|
||||||
|
func (m *ThemeManager) CurrentThemeInfo() ThemeMeta {
|
||||||
|
m.mu.RLock()
|
||||||
|
defer m.mu.RUnlock()
|
||||||
|
|
||||||
|
return m.meta
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListThemes 获取可用主题列表
|
||||||
func (m *ThemeManager) ListThemes() ([]string, error) {
|
func (m *ThemeManager) ListThemes() ([]string, error) {
|
||||||
dirs, err := os.ReadDir(m.themesDir)
|
entries, err := os.ReadDir(m.themesDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var themes []string
|
var themes []string
|
||||||
for _, d := range dirs {
|
for _, entry := range entries {
|
||||||
if d.IsDir() && isValidTheme(filepath.Join(m.themesDir, d.Name())) {
|
if entry.IsDir() {
|
||||||
themes = append(themes, d.Name())
|
if validateTheme(filepath.Join(m.themesDir, entry.Name())) == nil {
|
||||||
|
themes = append(themes, entry.Name())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return themes, nil
|
return themes, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user