configui/route.go

98 lines
1.7 KiB
Go
Raw Normal View History

2021-10-18 18:38:28 +00:00
package main
import (
"net/http"
2021-10-19 18:32:37 +00:00
"path/filepath"
2021-10-20 10:20:14 +00:00
"runtime"
2021-10-19 18:32:37 +00:00
"sort"
"strings"
2021-10-18 18:38:28 +00:00
)
2021-10-19 18:32:37 +00:00
type OnPageFile struct {
RO bool
Path string
Alias string
Action string
Content string
}
type Editor struct {
2021-10-20 10:20:14 +00:00
Lang string
Platform string
2021-10-19 18:32:37 +00:00
}
type Page struct {
2021-10-20 10:20:14 +00:00
Files []OnPageFile
2021-10-19 18:32:37 +00:00
Error string
File OnPageFile
Editor Editor
2021-10-20 14:02:10 +00:00
Static bool
2021-10-19 18:32:37 +00:00
}
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
}
2021-10-20 10:20:14 +00:00
Files := []OnPageFile{}
2021-10-18 18:38:28 +00:00
for file := range files {
2021-10-20 10:20:14 +00:00
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"
2021-10-18 18:38:28 +00:00
}
2021-10-19 18:32:37 +00:00
data := Page{
2021-10-20 10:20:14 +00:00
File: OnPageFile{},
Files: Files,
Editor: Editor{
Platform: plat,
},
2021-10-20 14:02:10 +00:00
Static: flagNoReconfig,
2021-10-18 18:38:28 +00:00
}
2021-10-19 15:41:09 +00:00
content := ""
2021-10-19 18:32:37 +00:00
var tmp []byte
var err error
2021-10-19 15:41:09 +00:00
name := r.URL.Query().Get("name")
file, ok := files[name]
if name == "" || !ok {
2021-10-19 18:32:37 +00:00
tmp, err = GetConfig()
data.File.Alias = "ConfigUI"
2021-10-20 10:20:14 +00:00
data.File.Path = ":mem:"
2021-10-19 18:32:37 +00:00
data.Editor.Lang = "json"
2021-10-19 15:41:09 +00:00
if !ok {
2021-10-19 18:32:37 +00:00
data.Error = name + " not found\n"
2021-10-19 15:41:09 +00:00
}
2021-10-19 18:32:37 +00:00
} else {
tmp, err = file.Read()
2021-10-20 10:20:14 +00:00
data.File.Action = file.Action
2021-10-19 18:32:37 +00:00
data.File.Alias = file.Alias
ext := strings.TrimPrefix(filepath.Ext(file.Path), ".")
if ext2mode[ext] != "" {
ext = ext2mode[ext]
}
data.Editor.Lang = ext
data.File.RO = file.RO
2021-10-20 10:20:14 +00:00
data.File.Path = file.Path
2021-10-19 18:32:37 +00:00
}
if err != nil {
data.Error = err.Error()
data.Editor.Lang = ""
} else {
content = string(tmp)
2021-10-19 15:41:09 +00:00
}
2021-10-19 18:32:37 +00:00
data.File.Content = content
2021-10-19 15:41:09 +00:00
2021-10-19 04:34:01 +00:00
Parse(w, "home", data)
2021-10-18 18:38:28 +00:00
})
}