2021-10-23 04:56:24 +00:00
|
|
|
package configui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ActiveFile struct {
|
|
|
|
RO bool
|
|
|
|
Path string
|
|
|
|
Name string
|
2021-11-11 02:21:10 +00:00
|
|
|
Cmd string
|
2021-10-23 04:56:24 +00:00
|
|
|
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
|
2021-10-23 04:56:24 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
// })
|
|
|
|
// }
|
2021-10-23 04:56:24 +00:00
|
|
|
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,
|
2021-10-23 04:56:24 +00:00
|
|
|
File: ActiveFile{},
|
|
|
|
Files: Files,
|
|
|
|
Editor: Editor{
|
|
|
|
Platform: plat,
|
|
|
|
},
|
2021-11-09 03:27:58 +00:00
|
|
|
Actions: cui.Actions,
|
|
|
|
Integrations: cui.Integrations,
|
2021-10-23 04:56:24 +00:00
|
|
|
Static: cui.NoReconfig,
|
|
|
|
HideConfig: cui.HideConfig,
|
|
|
|
ResultBellow: cui.ResultBellow,
|
2021-10-23 16:49:44 +00:00
|
|
|
Version: version,
|
2021-10-23 04:56:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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:"
|
2021-10-23 04:56:24 +00:00
|
|
|
data.Editor.Lang = "json"
|
|
|
|
if name != "" {
|
|
|
|
data.Error = name + " not found\n"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tmp, err = file.Read()
|
2021-11-11 02:21:10 +00:00
|
|
|
data.File.Cmd = file.Cmd
|
2021-10-23 04:56:24 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-10-23 04:56:24 +00:00
|
|
|
}
|