configui/server.go

158 lines
3.7 KiB
Go

package configui
import (
"net/http"
"strings"
"kumoly.io/lib/ksrv"
"kumoly.io/lib/stat"
)
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 {
k := ksrv.New()
cui.ksrv_log = k.GetLogger()
cui.setLog()
k.SetNoLogCondition(func(r *http.Request) bool {
if strings.HasPrefix(r.URL.Path, "/public") || r.URL.Query().Get("f") == "true" {
return true
}
return false
})
return k.Middleware(
http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if cui.AllowIP != "" {
ip := ksrv.GetIP(r)
if !ksrv.MatchIPGlob(ip, cui.AllowIP) {
rw.WriteHeader(403)
panic("permission denied")
}
}
if MutexLocked(&cui.configLock) {
panic(ErrorServerReloading)
}
next.ServeHTTP(rw, r)
}),
)
}
func (cui *ConfigUI) mux() *http.ServeMux {
cuiR := http.NewServeMux()
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)
}
})
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)
}
})
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)
}
})
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/oni", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
cui.GetOni(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/oni/start", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
cui.OniStart(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/oni/stop", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
cui.OniStop(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/oni/kill", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
cui.OniKill(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/oni/log", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
cui.OniLog(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.HandleFunc("/api/oni/attach", func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
cui.OniAttach(w, r)
} else {
w.WriteHeader(404)
}
})
cuiR.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.FS(cui.PublicFS))))
cuiR.HandleFunc("/", cui.App)
return cuiR
}