configui/route.go

102 lines
1.8 KiB
Go
Raw Normal View History

2021-10-18 18:38:28 +00:00
package main
import (
"net/http"
"path/filepath"
"runtime"
"sort"
"strings"
2021-10-18 18:38:28 +00:00
)
type OnPageFile struct {
RO bool
Path string
Alias string
Action string
Content string
}
type Editor struct {
Lang string
Platform string
}
type Page struct {
Files []OnPageFile
Error string
File OnPageFile
Editor Editor
Static bool
}
2021-10-18 18:38:28 +00:00
func setRoutes(mux *http.ServeMux) {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
Files := []OnPageFile{}
2021-10-18 18:38:28 +00:00
for file := range files {
Files = append(Files, OnPageFile{
Alias: files[file].Alias,
Path: files[file].Path,
})
}
sort.Slice(Files, func(i, j int) bool { return Files[i].Alias < Files[j].Alias })
plat := "unix"
if runtime.GOOS == "windows" {
plat = "windows"
}
data := Page{
File: OnPageFile{},
Files: Files,
Editor: Editor{
Platform: plat,
},
Static: flagNoReconfig,
2021-10-18 18:38:28 +00:00
}
content := ""
var tmp []byte
var err error
name := r.URL.Query().Get("name")
file, ok := files[name]
if name == "" || !ok {
tmp, err = GetConfig()
data.File.Alias = "ConfigUI"
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.Alias = file.Alias
2021-10-20 17:19:13 +00:00
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
}
2021-10-18 18:38:28 +00:00
if err != nil {
data.Error = err.Error()
data.Editor.Lang = ""
2021-10-18 18:38:28 +00:00
} else {
content = string(tmp)
2021-10-18 18:38:28 +00:00
}
data.File.Content = content
2021-10-18 18:38:28 +00:00
2021-10-19 04:34:01 +00:00
Parse(w, "home", data)
2021-10-18 18:38:28 +00:00
})
}