pull/36/head
Evan Chen 2021-10-21 01:19:13 +08:00
parent 539bdbf25e
commit 0a750b700f
5 changed files with 35 additions and 30 deletions

View File

@ -1 +1,5 @@
# 0.1.0
# 0.1.2
## Feature
* Save config back to file

41
api.go
View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"kumoly.io/tools/configui/configui"
)
@ -11,8 +12,7 @@ import (
func ListFiles(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(files)
if err != nil {
AbortError(w, err)
return
panic(err)
}
w.Write(data)
}
@ -26,8 +26,7 @@ func GetFile(w http.ResponseWriter, r *http.Request) {
}
data, err := file.Read()
if err != nil {
AbortError(w, err)
return
panic(err)
}
response, err := json.Marshal(map[string]string{
"path": file.Path,
@ -36,8 +35,7 @@ func GetFile(w http.ResponseWriter, r *http.Request) {
"data": string(data),
})
if err != nil {
AbortError(w, err)
return
panic(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
@ -47,13 +45,11 @@ func PostFile(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
AbortError(w, err)
return
panic(err)
}
f := configui.File{}
if err := json.Unmarshal(data, &f); err != nil {
AbortError(w, err)
return
panic(err)
}
file, ok := files[f.Alias]
if !ok {
@ -61,8 +57,7 @@ func PostFile(w http.ResponseWriter, r *http.Request) {
return
}
if err := file.Write([]byte(f.Data)); err != nil {
AbortError(w, err)
return
panic(err)
}
w.Write([]byte("ok"))
}
@ -76,8 +71,7 @@ func Apply(w http.ResponseWriter, r *http.Request) {
}
result, err := file.Do()
if err != nil {
AbortError(w, err)
return
panic(err)
}
w.Write([]byte(result))
}
@ -93,19 +87,23 @@ func Download(w http.ResponseWriter, r *http.Request) {
func LoadConfig(w http.ResponseWriter, r *http.Request) {
if flagNoReconfig {
AbortError(w, "system reconfig is disabled")
return
panic("system reconfig is disabled")
}
data, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
AbortError(w, err)
return
panic(err)
}
ftmp, err := configui.ReadConfig(string(data))
if err != nil {
AbortError(w, err)
return
panic(err)
}
if flagConfigPath != "" {
info, err := os.Stat(flagConfigPath)
if err != nil {
panic(err)
}
os.WriteFile(flagConfigPath, data, info.Mode())
}
files = configui.GetFileMap(ftmp)
w.Write([]byte("ok"))
@ -114,8 +112,7 @@ func LoadConfig(w http.ResponseWriter, r *http.Request) {
func getConfigHandler(w http.ResponseWriter, r *http.Request) {
data, err := GetConfig()
if err != nil {
AbortError(w, err)
return
panic(err)
}
w.Write(data)
}

View File

@ -18,6 +18,7 @@ type File struct {
Alias string `json:"name"`
Action string `json:"action"`
RO bool `json:"ro"`
Lang string `json:"lang"`
// used for parsing post data
Data string `json:"data"`

View File

@ -54,7 +54,7 @@ func AbortError(w http.ResponseWriter, err interface{}) (int, error) {
func Catch(rw *ResponseWriter, r *http.Request) {
ex := recover()
if ex != nil {
AbortError(rw, r)
AbortError(rw, ex)
log.Printf("%s %s %d %s %s\n", GetIP(r), r.Method, rw.StatueCode, r.URL, r.Header.Get("User-Agent"))
}
}
@ -102,8 +102,7 @@ func Parse(w http.ResponseWriter, name string, data interface{}) error {
buf := &bytes.Buffer{}
err := tmpl.ExecuteTemplate(buf, "home", data)
if err != nil {
AbortError(w, err)
return err
panic(err)
}
_, err = w.Write(buf.Bytes())
return err

View File

@ -75,11 +75,15 @@ func setRoutes(mux *http.ServeMux) {
tmp, err = file.Read()
data.File.Action = file.Action
data.File.Alias = file.Alias
if file.Lang != "" {
data.Editor.Lang = file.Lang
} else {
ext := strings.TrimPrefix(filepath.Ext(file.Path), ".")
if ext2mode[ext] != "" {
ext = ext2mode[ext]
}
data.Editor.Lang = ext
}
data.File.RO = file.RO
data.File.Path = file.Path
}