configui/server.go

143 lines
3.1 KiB
Go
Raw Normal View History

package configui
import (
"net/http"
2021-11-30 08:34:36 +00:00
"path/filepath"
2021-11-03 19:35:51 +00:00
"kumoly.io/lib/ksrv"
2021-11-15 08:54:55 +00:00
"kumoly.io/lib/stat"
2021-11-30 08:34:36 +00:00
"kumoly.io/tools/kconfig"
)
func (cui *ConfigUI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
cui.middleware(cui.mux()).ServeHTTP(w, r)
}
func (cui *ConfigUI) middleware(next http.Handler) http.Handler {
2021-11-03 19:35:51 +00:00
k := ksrv.New()
cui.ksrv_log = k.GetLogger()
cui.setLog()
k.SetNoLogCondition(func(r *http.Request) bool {
2021-11-30 08:34:36 +00:00
ext := filepath.Ext(r.URL.Path)
switch ext {
case ".js":
2021-11-03 19:35:51 +00:00
return true
2021-11-30 08:34:36 +00:00
case ".css":
return true
case ".ttf":
return true
case ".ico":
return true
2021-11-03 19:35:51 +00:00
}
2021-11-30 08:34:36 +00:00
return r.URL.Query().Get("f") == "true"
2021-11-03 19:35:51 +00:00
})
return k.Middleware(
http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if cui.AllowIP != "" {
2021-11-03 19:35:51 +00:00
ip := ksrv.GetIP(r)
if !ksrv.MatchIPGlob(ip, cui.AllowIP) {
rw.WriteHeader(403)
panic("permission denied")
}
}
next.ServeHTTP(rw, r)
2021-11-03 19:35:51 +00:00
}),
)
2021-11-03 19:35:51 +00:00
}
func (cui *ConfigUI) mux() *http.ServeMux {
cuiR := http.NewServeMux()
2021-11-15 08:54:55 +00:00
cuiR.HandleFunc("/api/profile", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
ksrv.JSON(w, stat.GetProfile())
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/conf", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
cui.GetConfig(w, r)
} else if r.Method == "POST" {
cui.PostConfig(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/files", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
cui.ListFiles(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/file", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
cui.GetFile(w, r)
} else if r.Method == "POST" {
cui.PostFile(w, r)
} else {
w.WriteHeader(404)
}
})
2021-11-03 08:21:49 +00:00
cuiR.HandleFunc("/api/delta", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
cui.GetDelta(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/apply", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
cui.Apply(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/export", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
cui.Download(w, r)
} else {
w.WriteHeader(404)
}
})
2021-11-09 03:27:58 +00:00
cuiR.HandleFunc("/api/action", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
cui.DoAction(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/integration", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
cui.DoIntegration(w, r)
} else {
w.WriteHeader(404)
}
})
2021-11-30 08:34:36 +00:00
k := kconfig.New()
k.Schema = SCHEMA
k.Load = func() []byte {
b, err := cui.Config()
if err != nil {
cui.log.Error(err)
return []byte{}
}
return b
}
k.Apply = func(b []byte) error {
err := cui.LoadConfig(string(b))
if err == nil {
k.AppName = cui.AppName
}
return err
}
cuiR.Handle("/kconfig/", http.StripPrefix("/kconfig", k))
cuiR.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.FS(cui.PublicFS))))
cuiR.HandleFunc("/", cui.App)
return cuiR
}