build: use ksrv engine

feat/muzan
Evan Chen 2021-11-04 13:55:45 +08:00
parent 0aa278efa0
commit f76cc2f75e
4 changed files with 13 additions and 18 deletions

6
app.go
View File

@ -112,6 +112,8 @@ func (cui *ConfigUI) App(w http.ResponseWriter, r *http.Request) {
} }
data.File.Content = content data.File.Content = content
err = cui.tmpl.ExecuteTemplate(w, "home", data)
cui.Parse(w, "home", data) if err != nil {
panic(err)
}
} }

View File

@ -1,17 +1,16 @@
package configui package configui
import ( import (
"bytes"
"embed" "embed"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
"net/http"
"os" "os"
"time" "time"
"kumoly.io/lib/klog" "kumoly.io/lib/klog"
"kumoly.io/lib/ksrv/engine"
"kumoly.io/tools/configui/public" "kumoly.io/tools/configui/public"
) )
@ -54,7 +53,7 @@ type ConfigUI struct {
LogLevel int `json:"log_level"` LogLevel int `json:"log_level"`
TmplFS embed.FS `json:"-"` TmplFS embed.FS `json:"-"`
tmpl *template.Template tmpl *engine.Engine
PublicFS embed.FS `json:"-"` PublicFS embed.FS `json:"-"`
log *klog.Logger `json:"-"` log *klog.Logger `json:"-"`
ksrv_log *klog.Logger `json:"-"` ksrv_log *klog.Logger `json:"-"`
@ -62,7 +61,7 @@ type ConfigUI struct {
} }
func New() *ConfigUI { 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 { "step": func(from, to, step uint) []uint {
items := []uint{} items := []uint{}
for i := from; i <= to; i += step { for i := from; i <= to; i += step {
@ -211,13 +210,3 @@ func (cui *ConfigUI) AppendFile(file *File) error {
cui.Files = append(cui.Files, file) cui.Files = append(cui.Files, file)
return nil 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
}

4
go.mod
View File

@ -3,8 +3,8 @@ module kumoly.io/tools/configui
go 1.17 go 1.17
require ( require (
kumoly.io/lib/klog v0.1.9 kumoly.io/lib/klog v0.1.10
kumoly.io/lib/ksrv v0.1.4 kumoly.io/lib/ksrv v0.1.5-0.20211104040031-77be1d6ae358
) )
require ( require (

4
go.sum
View File

@ -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= 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 h1:rS9PPqfyBIIfeQlPSuMv+7StGPiFVuAdp04HDwwDY3E=
kumoly.io/lib/klog v0.1.9/go.mod h1:Snm+c1xRrh/RbXsxQf7UGYbAJGPcIa6bEEN+CmzJh7M= 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 h1:8zbslRwdNWHw5Wm1PiyDLr6Mu4xxjz0FTW4u8dZ6ZeI=
kumoly.io/lib/ksrv v0.1.4/go.mod h1:eKfhJR5mOqlQZAy5EUI+Avfxirx2/nNW79r+Ki2C18k= 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=