v2.6.7
This commit is contained in:
parent
f04677c810
commit
c0ac4c4b7d
49
web/web.go
49
web/web.go
@ -128,28 +128,39 @@ func (s *Server) getHtmlFiles() ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) getHtmlTemplate(funcMap template.FuncMap) (*template.Template, error) {
|
func (s *Server) getHtmlTemplate(funcMap template.FuncMap) (*template.Template, error) {
|
||||||
t := template.New("").Funcs(funcMap)
|
// 这里用 htmlFS(//go:embed html/*)而不是“templates”
|
||||||
err := fs.WalkDir(htmlFS, "html", func(path string, d fs.DirEntry, err error) error {
|
t := template.New("").Funcs(funcMap)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if d.IsDir() {
|
// 递归遍历 embed 的 html 目录,解析所有 .html 模板
|
||||||
newT, err := t.ParseFS(htmlFS, path+"/*.html")
|
err := fs.WalkDir(htmlFS, "html", func(path string, d fs.DirEntry, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// ignore
|
return err
|
||||||
return nil
|
}
|
||||||
}
|
if d.IsDir() {
|
||||||
t = newT
|
return nil
|
||||||
}
|
}
|
||||||
return nil
|
if !strings.HasSuffix(path, ".html") {
|
||||||
})
|
return nil
|
||||||
if err != nil {
|
}
|
||||||
return nil, err
|
|
||||||
}
|
// 读出模板内容
|
||||||
return t, nil
|
b, err := htmlFS.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 去掉前缀“html/”,让 {{template "form/inbound"}} 这种名字能被正确找到
|
||||||
|
name := strings.TrimPrefix(path, "html/")
|
||||||
|
_, err = t.New(name).Parse(string(b))
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *Server) initRouter() (*gin.Engine, error) {
|
func (s *Server) initRouter() (*gin.Engine, error) {
|
||||||
if config.IsDebug() {
|
if config.IsDebug() {
|
||||||
gin.SetMode(gin.DebugMode)
|
gin.SetMode(gin.DebugMode)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user