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

View File

@ -18,6 +18,7 @@ type File struct {
Alias string `json:"name"` Alias string `json:"name"`
Action string `json:"action"` Action string `json:"action"`
RO bool `json:"ro"` RO bool `json:"ro"`
Lang string `json:"lang"`
// used for parsing post data // used for parsing post data
Data string `json:"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) { func Catch(rw *ResponseWriter, r *http.Request) {
ex := recover() ex := recover()
if ex != nil { 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")) 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{} buf := &bytes.Buffer{}
err := tmpl.ExecuteTemplate(buf, "home", data) err := tmpl.ExecuteTemplate(buf, "home", data)
if err != nil { if err != nil {
AbortError(w, err) panic(err)
return err
} }
_, err = w.Write(buf.Bytes()) _, err = w.Write(buf.Bytes())
return err return err

View File

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