configui/app.go

113 lines
2.0 KiB
Go
Raw Normal View History

2021-10-21 13:49:15 +00:00
package configui
import (
"net/http"
"path/filepath"
"runtime"
"sort"
"strings"
)
type ActiveFile struct {
RO bool
Path string
Name string
Action string
Content string
2021-10-22 05:20:59 +00:00
Order int
2021-10-21 13:49:15 +00:00
}
type Editor struct {
Lang string
Platform string
}
type Page struct {
AppName string
Files []ActiveFile
Error string
File ActiveFile
Editor Editor
2021-10-23 04:55:05 +00:00
Static bool
HideConfig bool
ResultBellow bool
2021-10-21 13:49:15 +00:00
}
func (cui *ConfigUI) App(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
Files := []ActiveFile{}
for _, i := range cui.fileIndex {
Files = append(Files, ActiveFile{
2021-10-22 05:20:59 +00:00
Name: cui.Files[i].Name,
Path: cui.Files[i].Path,
Order: cui.Files[i].Order,
2021-10-21 13:49:15 +00:00
})
}
2021-10-22 05:20:59 +00:00
sort.Slice(Files, func(i, j int) bool {
if Files[i].Order == Files[j].Order {
return Files[i].Name < Files[j].Name
}
return Files[i].Order < Files[j].Order
})
2021-10-21 13:49:15 +00:00
plat := "unix"
if runtime.GOOS == "windows" {
plat = "windows"
}
data := Page{
AppName: cui.AppName,
File: ActiveFile{},
Files: Files,
Editor: Editor{
Platform: plat,
},
2021-10-23 04:55:05 +00:00
Static: cui.NoReconfig,
HideConfig: cui.HideConfig,
ResultBellow: cui.ResultBellow,
2021-10-21 13:49:15 +00:00
}
content := ""
var tmp []byte
var err error
name := r.URL.Query().Get("name")
file, err := cui.File(name)
if name == "" || err != nil {
tmp, err = cui.Config()
2021-10-22 05:20:59 +00:00
data.File.Name = cui.AppName
2021-10-21 13:49:15 +00:00
data.File.Path = ":mem:"
data.Editor.Lang = "json"
if name != "" {
data.Error = name + " not found\n"
}
} else {
tmp, err = file.Read()
data.File.Action = file.Action
data.File.Name = file.Name
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
}
if err != nil {
data.Error = err.Error()
data.Editor.Lang = ""
} else {
content = string(tmp)
}
data.File.Content = content
cui.Parse(w, "home", data)
}