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
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
tmpl = engine.Must(engine.New("").Parse(index))
|
|
|
|
}
|
|
|
|
|
|
|
|
type Kconfig struct {
|
|
|
|
AppName string
|
2021-11-24 09:39:04 +00:00
|
|
|
KFJstr []byte
|
2021-11-19 03:58:55 +00:00
|
|
|
Mux *http.ServeMux
|
|
|
|
|
2021-11-24 09:08:11 +00:00
|
|
|
Apply func([]byte) error
|
|
|
|
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",
|
|
|
|
KFJstr: defaultSchema,
|
|
|
|
Apply: func(b []byte) error {
|
|
|
|
log.Debug().Msgf("%s", b)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
w.Write([]byte(k.KFJstr))
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
}
|