103 lines
2.0 KiB
Go
103 lines
2.0 KiB
Go
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 {
|
|
w.WriteHeader(500)
|
|
w.Write([]byte(err.Error()))
|
|
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 {
|
|
w.WriteHeader(404)
|
|
w.Write([]byte("file not found"))
|
|
return
|
|
}
|
|
data, err := file.Read()
|
|
if err != nil {
|
|
w.WriteHeader(500)
|
|
w.Write([]byte(err.Error()))
|
|
return
|
|
}
|
|
response, err := json.Marshal(map[string]string{
|
|
"path": file.Path,
|
|
"name": file.Alias,
|
|
"action": file.Action,
|
|
"data": string(data),
|
|
})
|
|
if err != nil {
|
|
w.WriteHeader(500)
|
|
w.Write([]byte(err.Error()))
|
|
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 {
|
|
w.WriteHeader(500)
|
|
w.Write([]byte(err.Error()))
|
|
return
|
|
}
|
|
f := configui.File{}
|
|
if err := json.Unmarshal(data, &f); err != nil {
|
|
w.WriteHeader(500)
|
|
w.Write([]byte(err.Error()))
|
|
return
|
|
}
|
|
file, ok := files[f.Alias]
|
|
if !ok {
|
|
w.WriteHeader(404)
|
|
w.Write([]byte("file not found"))
|
|
return
|
|
}
|
|
if err := file.Write([]byte(f.Data)); err != nil {
|
|
w.WriteHeader(500)
|
|
w.Write([]byte(err.Error()))
|
|
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 {
|
|
w.WriteHeader(404)
|
|
w.Write([]byte("file not found"))
|
|
return
|
|
}
|
|
result, err := file.Do()
|
|
if err != nil {
|
|
w.WriteHeader(500)
|
|
w.Write([]byte(err.Error()))
|
|
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)
|
|
}
|