kconfig/kconfig.go

130 lines
2.6 KiB
Go
Raw Normal View History

2021-11-19 03:58:55 +00:00
package kconfig
import (
_ "embed"
2021-11-24 09:08:11 +00:00
"html/template"
"io/ioutil"
2021-11-19 03:58:55 +00:00
"net/http"
"strings"
2021-11-24 09:08:11 +00:00
"github.com/rs/zerolog/log"
2021-11-19 03:58:55 +00:00
"kumoly.io/lib/guard/engine"
"kumoly.io/tools/kconfig/public"
)
2021-11-24 09:39:04 +00:00
//go:embed assets/default.jcs
var defaultSchema []byte
2021-11-24 09:08:11 +00:00
2021-11-19 03:58:55 +00:00
//go:embed public/index.html
var index string
var servePublic = http.FileServer(http.FS(public.FS))
var tmpl *engine.Engine
2021-11-24 15:13:00 +00:00
var l = log.With().Str("mod", "kconfig").Logger()
2021-11-19 03:58:55 +00:00
func init() {
tmpl = engine.Must(engine.New("").Parse(index))
}
type Kconfig struct {
2021-11-24 10:49:30 +00:00
// AppName displays appname in title and the download file will be {{.AppName}}.json
2021-11-19 03:58:55 +00:00
AppName string
2021-11-24 10:49:30 +00:00
// Schema json for schema, more information see [json-editor](https://github.com/json-editor/json-editor)
Schema []byte
// Mux the underlying Mux
Mux *http.ServeMux
2021-11-19 03:58:55 +00:00
2021-11-24 10:49:30 +00:00
// Apply function callback for intergrating submit
2021-11-24 09:08:11 +00:00
Apply func([]byte) error
2021-11-24 10:49:30 +00:00
// Load function callback for intergrating load
Load func() []byte
2021-11-19 03:58:55 +00:00
}
func New() *Kconfig {
2021-11-24 09:08:11 +00:00
k := &Kconfig{
AppName: "kconfig",
2021-11-24 10:49:30 +00:00
Schema: defaultSchema,
2021-11-24 09:08:11 +00:00
Apply: func(b []byte) error {
2021-11-24 15:13:00 +00:00
l.Debug().Msgf("%s", b)
2021-11-24 09:08:11 +00:00
return nil
},
Load: func() []byte {
return []byte(`{
"name": "test",
"age": 21,
"gender": "male",
"location": {
"city": "",
"state": "",
"citystate": ", "
},
"pets": [],
"cars": []
}`)
},
}
2021-11-19 03:58:55 +00:00
mux := http.NewServeMux()
mux.HandleFunc("/", k.App)
2021-11-24 09:08:11 +00:00
mux.HandleFunc("/api/schema", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.NotFound(w, r)
return
}
2021-11-24 10:49:30 +00:00
w.Write([]byte(k.Schema))
2021-11-24 09:08:11 +00:00
})
mux.HandleFunc("/api/load", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.NotFound(w, r)
return
}
w.Write([]byte(k.Load()))
})
mux.HandleFunc("/api/apply", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.NotFound(w, r)
return
}
data, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
panic(err)
}
err = k.Apply(data)
if err != nil {
panic(err)
}
w.Write([]byte("ok"))
})
2021-11-19 03:58:55 +00:00
k.Mux = mux
return k
}
func (k *Kconfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
k.Mux.ServeHTTP(w, r)
}
func (k *Kconfig) App(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
2021-11-24 09:08:11 +00:00
tmpl.Execute(w, struct {
AppName string
ACE_JS template.HTML
}{
k.AppName,
template.HTML(`<script src="ace/ace.js"></script>`),
})
2021-11-19 03:58:55 +00:00
return
}
file, err := public.FS.Open(strings.TrimPrefix(r.URL.String(), "/"))
if err != nil {
http.NotFound(w, r)
return
}
stat, err := file.Stat()
if err != nil || stat.IsDir() {
http.NotFound(w, r)
return
}
servePublic.ServeHTTP(w, r)
}