configui/api.go

130 lines
2.5 KiB
Go
Raw Normal View History

2021-10-18 10:59:09 +00:00
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"kumoly.io/tools/configui/configui"
)
func ListFiles(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(files)
if err != nil {
2021-10-19 04:34:01 +00:00
AbortError(w, err)
2021-10-18 10:59:09 +00:00
return
}
w.Write(data)
}
func GetFile(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
file, ok := files[name]
if name == "" || !ok {
2021-10-19 04:34:01 +00:00
MakeResponse(w, 404, []byte("file not found"))
2021-10-18 10:59:09 +00:00
return
}
data, err := file.Read()
if err != nil {
2021-10-19 04:34:01 +00:00
AbortError(w, err)
2021-10-18 10:59:09 +00:00
return
}
response, err := json.Marshal(map[string]string{
"path": file.Path,
"name": file.Alias,
"action": file.Action,
"data": string(data),
})
if err != nil {
2021-10-19 04:34:01 +00:00
AbortError(w, err)
2021-10-18 10:59:09 +00:00
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}
func PostFile(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
2021-10-19 04:34:01 +00:00
AbortError(w, err)
2021-10-18 10:59:09 +00:00
return
}
f := configui.File{}
if err := json.Unmarshal(data, &f); err != nil {
2021-10-19 04:34:01 +00:00
AbortError(w, err)
2021-10-18 10:59:09 +00:00
return
}
file, ok := files[f.Alias]
if !ok {
2021-10-19 04:34:01 +00:00
MakeResponse(w, 404, []byte("file not found"))
2021-10-18 10:59:09 +00:00
return
}
if err := file.Write([]byte(f.Data)); err != nil {
2021-10-19 04:34:01 +00:00
AbortError(w, err)
2021-10-18 10:59:09 +00:00
return
}
w.Write([]byte("ok"))
}
func Apply(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
file, ok := files[name]
if name == "" || !ok {
2021-10-19 04:34:01 +00:00
MakeResponse(w, 404, []byte("file not found"))
2021-10-18 10:59:09 +00:00
return
}
result, err := file.Do()
if err != nil {
2021-10-19 04:34:01 +00:00
AbortError(w, err)
2021-10-18 10:59:09 +00:00
return
}
w.Write([]byte(result))
}
func Download(w http.ResponseWriter, r *http.Request) {
fs := []string{}
for _, v := range files {
fs = append(fs, v.Path)
}
w.Header().Set("Content-Disposition", `attachment; filename="export.tar.gz"`)
bundle(w, fs, false)
}
2021-10-18 15:53:49 +00:00
func LoadConfig(w http.ResponseWriter, r *http.Request) {
if flagNoReconfig {
AbortError(w, "system reconfig is disabled")
return
}
2021-10-18 15:53:49 +00:00
data, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
2021-10-19 04:34:01 +00:00
AbortError(w, err)
2021-10-18 15:53:49 +00:00
return
}
ftmp, err := configui.ReadConfig(string(data))
if err != nil {
2021-10-19 04:34:01 +00:00
AbortError(w, err)
2021-10-18 15:53:49 +00:00
return
}
files = configui.GetFileMap(ftmp)
w.Write([]byte("ok"))
}
2021-10-18 18:38:28 +00:00
func getConfigHandler(w http.ResponseWriter, r *http.Request) {
data, err := GetConfig()
2021-10-18 15:53:49 +00:00
if err != nil {
2021-10-19 04:34:01 +00:00
AbortError(w, err)
2021-10-18 15:53:49 +00:00
return
}
w.Write(data)
}
2021-10-18 18:38:28 +00:00
func GetConfig() ([]byte, error) {
config := []configui.File{}
for _, f := range files {
config = append(config, *f)
}
return json.MarshalIndent(config, "", " ")
2021-10-18 18:38:28 +00:00
}