configui/route.go

96 lines
1.7 KiB
Go

package main
import (
"net/http"
"path/filepath"
"runtime"
"sort"
"strings"
)
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
}
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{}
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,
},
}
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 !ok {
data.Error = name + " not found\n"
}
} else {
tmp, err = file.Read()
data.File.Action = file.Action
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
data.File.Path = file.Path
}
if err != nil {
data.Error = err.Error()
data.Editor.Lang = ""
} else {
content = string(tmp)
}
data.File.Content = content
Parse(w, "home", data)
})
}