package main import ( "net/http" "path/filepath" "sort" "strings" ) 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 } 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) } sort.Slice(Files, func(i, j int) bool { return Files[i] < Files[j] }) data := Page{ File: OnPageFile{}, Files: Files, Editor: Editor{}, } 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.Editor.Lang = "json" if !ok { data.Error = name + " not found\n" } } 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) } data.File.Content = content Parse(w, "home", data) }) }