configui/app.go

117 lines
2.1 KiB
Go
Raw Normal View History

package configui
import (
"net/http"
"path/filepath"
"runtime"
"sort"
"strings"
)
type ActiveFile struct {
RO bool
Path string
Name string
Action string
Content string
Order int
}
type Editor struct {
Lang string
Platform string
}
type Page struct {
AppName string
2021-10-23 16:49:44 +00:00
Version string
Build string
Files []ActiveFile
Error string
File ActiveFile
Editor Editor
Static bool
HideConfig bool
ResultBellow bool
}
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{
Name: cui.Files[i].Name,
Path: cui.Files[i].Path,
Order: cui.Files[i].Order,
})
}
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
})
plat := "unix"
if runtime.GOOS == "windows" {
plat = "windows"
}
data := Page{
AppName: cui.AppName,
File: ActiveFile{},
Files: Files,
Editor: Editor{
Platform: plat,
},
Static: cui.NoReconfig,
HideConfig: cui.HideConfig,
ResultBellow: cui.ResultBellow,
2021-10-23 16:49:44 +00:00
Version: version,
Build: build,
}
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()
data.File.Name = cui.AppName
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)
}