configui/app.go

130 lines
2.5 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
Cmd string
Content string
Order int
}
type Editor struct {
Lang string
Platform string
}
type Page struct {
2021-11-09 03:27:58 +00:00
AppName string
BaseUrl string
Actions []Action
Integrations []*Integration
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{}
2021-11-09 03:27:58 +00:00
// for i := range cui.Files {
// Files = append(Files, ActiveFile{
// Name: cui.Files[i].Name,
// Path: cui.Files[i].Path,
// })
// }
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,
2021-10-24 07:06:02 +00:00
BaseUrl: cui.BaseUrl,
File: ActiveFile{},
Files: Files,
Editor: Editor{
Platform: plat,
},
2021-11-09 03:27:58 +00:00
Actions: cui.Actions,
Integrations: cui.Integrations,
Static: cui.NoReconfig,
HideConfig: cui.HideConfig,
ResultBellow: cui.ResultBellow,
2021-10-23 16:49:44 +00:00
Version: version,
}
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
2021-11-09 03:27:58 +00:00
// data.File.Path = ":mem:"
data.Editor.Lang = "json"
if name != "" {
data.Error = name + " not found\n"
}
} else {
tmp, err = file.Read()
data.File.Cmd = file.Cmd
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
2021-11-04 05:55:45 +00:00
err = cui.tmpl.ExecuteTemplate(w, "home", data)
if err != nil {
panic(err)
}
}