configui/route.go

83 lines
1.4 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"
"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 {
Lang string
}
type Page struct {
Files []string
Error string
File OnPageFile
Editor Editor
}
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 := []string{}
for file := range files {
Files = append(Files, files[file].Alias)
}
2021-10-19 18:32:37 +00:00
sort.Slice(Files, func(i, j int) bool { return Files[i] < Files[j] })
2021-10-18 18:38:28 +00:00
2021-10-19 18:32:37 +00:00
data := Page{
File: OnPageFile{},
2021-10-19 15:41:09 +00:00
Files: Files,
2021-10-19 18:32:37 +00:00
Editor: Editor{},
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"
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()
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
}
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
})
}