From f76cc2f75e6860912fdd275dbfedc42a9a142641 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Thu, 4 Nov 2021 13:55:45 +0800 Subject: [PATCH] build: use ksrv engine --- app.go | 6 ++++-- configui.go | 17 +++-------------- go.mod | 4 ++-- go.sum | 4 ++++ 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app.go b/app.go index fcad36d..5e33b2c 100644 --- a/app.go +++ b/app.go @@ -112,6 +112,8 @@ func (cui *ConfigUI) App(w http.ResponseWriter, r *http.Request) { } data.File.Content = content - - cui.Parse(w, "home", data) + err = cui.tmpl.ExecuteTemplate(w, "home", data) + if err != nil { + panic(err) + } } diff --git a/configui.go b/configui.go index 83bcb7c..f5d7176 100644 --- a/configui.go +++ b/configui.go @@ -1,17 +1,16 @@ package configui import ( - "bytes" "embed" "encoding/json" "errors" "fmt" "html/template" - "net/http" "os" "time" "kumoly.io/lib/klog" + "kumoly.io/lib/ksrv/engine" "kumoly.io/tools/configui/public" ) @@ -54,7 +53,7 @@ type ConfigUI struct { LogLevel int `json:"log_level"` TmplFS embed.FS `json:"-"` - tmpl *template.Template + tmpl *engine.Engine PublicFS embed.FS `json:"-"` log *klog.Logger `json:"-"` ksrv_log *klog.Logger `json:"-"` @@ -62,7 +61,7 @@ type ConfigUI struct { } func New() *ConfigUI { - tmpl := template.Must(template.New("").Funcs(template.FuncMap{ + tmpl := engine.Must(engine.New("").Funcs(template.FuncMap{ "step": func(from, to, step uint) []uint { items := []uint{} for i := from; i <= to; i += step { @@ -211,13 +210,3 @@ func (cui *ConfigUI) AppendFile(file *File) error { cui.Files = append(cui.Files, file) return nil } - -func (cui *ConfigUI) Parse(w http.ResponseWriter, name string, data interface{}) error { - buf := &bytes.Buffer{} - err := cui.tmpl.ExecuteTemplate(buf, "home", data) - if err != nil { - panic(err) - } - _, err = w.Write(buf.Bytes()) - return err -} diff --git a/go.mod b/go.mod index d068adb..3cc0e64 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module kumoly.io/tools/configui go 1.17 require ( - kumoly.io/lib/klog v0.1.9 - kumoly.io/lib/ksrv v0.1.4 + kumoly.io/lib/klog v0.1.10 + kumoly.io/lib/ksrv v0.1.5-0.20211104040031-77be1d6ae358 ) require ( diff --git a/go.sum b/go.sum index 1ef1c21..137a032 100644 --- a/go.sum +++ b/go.sum @@ -4,5 +4,9 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= kumoly.io/lib/klog v0.1.9 h1:rS9PPqfyBIIfeQlPSuMv+7StGPiFVuAdp04HDwwDY3E= kumoly.io/lib/klog v0.1.9/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M= +kumoly.io/lib/klog v0.1.10 h1:GJxwcsUct8nF3oHtsJPTIlPKTUeB/+7jsbgh3bMvKMc= +kumoly.io/lib/klog v0.1.10/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M= kumoly.io/lib/ksrv v0.1.4 h1:8zbslRwdNWHw5Wm1PiyDLr6Mu4xxjz0FTW4u8dZ6ZeI= kumoly.io/lib/ksrv v0.1.4/go.mod h1:eKfhJR5mOqlQZAy5EUI+Avfxirx2/nNW79r+Ki2C18k= +kumoly.io/lib/ksrv v0.1.5-0.20211104040031-77be1d6ae358 h1:cVm0fUXzLYZBmhTJf0MRLBt35jLhjodadI8uUh4RNEg= +kumoly.io/lib/ksrv v0.1.5-0.20211104040031-77be1d6ae358/go.mod h1:Tux6CApi3sYGRM5prDPN2+aw3gKz1Tdf5CU6XFSFxbQ=