configui/main.go

164 lines
3.4 KiB
Go
Raw Normal View History

2021-10-18 08:49:16 +00:00
package main
import (
"bytes"
"embed"
"flag"
"fmt"
"html/template"
2021-10-18 10:59:09 +00:00
"io"
2021-10-18 08:49:16 +00:00
"log"
"net/http"
"os"
"time"
"kumoly.io/tools/configui/configui"
)
//go:embed templates
var tmplFS embed.FS
var tmpl *template.Template
var (
2021-10-18 10:59:09 +00:00
flagPath string
flagAction string
flagAlias string
flagConfigPath string
2021-10-18 08:49:16 +00:00
2021-10-18 10:59:09 +00:00
flagBind string
flagLogFile string
2021-10-18 15:53:49 +00:00
flagVer bool
2021-10-18 08:49:16 +00:00
)
2021-10-18 15:53:49 +00:00
var Version = "0.0.0"
var Build = "alpha"
2021-10-18 08:49:16 +00:00
var files = map[string]*configui.File{}
func init() {
2021-10-18 10:59:09 +00:00
// log.SetFlags(0)
flag.StringVar(&flagConfigPath, "f", "", "path to config file")
flag.StringVar(&flagPath, "p", "", "path to file, precedence over config")
flag.StringVar(&flagAlias, "n", "", "alias of file")
flag.StringVar(&flagAction, "c", "", "cmd to apply")
flag.StringVar(&flagLogFile, "log", "", "log to file")
flag.StringVar(&flagBind, "bind", "localhost:8000", "address to bind")
2021-10-18 15:53:49 +00:00
flag.BoolVar(&flagVer, "v", false, "show version")
2021-10-18 08:49:16 +00:00
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: configui [options]\n")
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
2021-10-18 15:53:49 +00:00
if flagVer {
fmt.Printf("%v - %v\n", Version, Build)
}
2021-10-18 10:59:09 +00:00
// setup logging
if flagLogFile != "" {
f, err := os.OpenFile(flagLogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Error opening file: %v", err)
}
defer f.Close()
mwriter := io.MultiWriter(f, os.Stderr)
log.SetOutput(mwriter)
}
2021-10-18 08:49:16 +00:00
// setup values
2021-10-18 10:59:09 +00:00
if flagPath != "" {
if flagAlias == "" {
flagAlias = flagPath
2021-10-18 08:49:16 +00:00
}
2021-10-18 10:59:09 +00:00
files[flagAlias] = &configui.File{
Path: flagPath,
Alias: flagAlias,
Action: flagAction,
2021-10-18 08:49:16 +00:00
}
2021-10-18 10:59:09 +00:00
} else if flagConfigPath == "" {
2021-10-18 15:53:49 +00:00
log.Println("no config found")
2021-10-18 08:49:16 +00:00
} else {
2021-10-18 10:59:09 +00:00
conf, err := os.ReadFile(flagConfigPath)
2021-10-18 08:49:16 +00:00
if err != nil {
log.Fatalln(err)
}
ftmp, err := configui.ReadConfig(string(conf))
if err != nil {
log.Fatalln(err)
}
files = configui.GetFileMap(ftmp)
}
// setup routes
mux := http.NewServeMux()
2021-10-18 15:53:49 +00:00
mux.HandleFunc("/api/conf", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
GetConfig(w, r)
} else if r.Method == "POST" {
LoadConfig(w, r)
} else {
w.WriteHeader(404)
}
})
2021-10-18 10:59:09 +00:00
mux.HandleFunc("/api/files", func(w http.ResponseWriter, r *http.Request) {
2021-10-18 08:49:16 +00:00
if r.Method == "GET" {
2021-10-18 10:59:09 +00:00
ListFiles(w, r)
2021-10-18 08:49:16 +00:00
} else {
2021-10-18 10:59:09 +00:00
w.WriteHeader(404)
2021-10-18 08:49:16 +00:00
}
})
2021-10-18 10:59:09 +00:00
mux.HandleFunc("/api/file", func(w http.ResponseWriter, r *http.Request) {
2021-10-18 08:49:16 +00:00
if r.Method == "GET" {
2021-10-18 10:59:09 +00:00
GetFile(w, r)
2021-10-18 08:49:16 +00:00
} else if r.Method == "POST" {
2021-10-18 10:59:09 +00:00
PostFile(w, r)
2021-10-18 08:49:16 +00:00
} else {
2021-10-18 10:59:09 +00:00
w.WriteHeader(404)
2021-10-18 08:49:16 +00:00
}
})
2021-10-18 10:59:09 +00:00
mux.HandleFunc("/api/apply", func(w http.ResponseWriter, r *http.Request) {
2021-10-18 08:49:16 +00:00
if r.Method == "POST" {
2021-10-18 10:59:09 +00:00
Apply(w, r)
2021-10-18 08:49:16 +00:00
} else {
2021-10-18 10:59:09 +00:00
w.WriteHeader(404)
2021-10-18 08:49:16 +00:00
}
})
2021-10-18 10:59:09 +00:00
mux.HandleFunc("/api/export", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
Download(w, r)
} else {
w.WriteHeader(404)
}
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
2021-10-18 08:49:16 +00:00
buf := &bytes.Buffer{}
err := tmpl.ExecuteTemplate(buf, "home", nil)
if err != nil {
2021-10-18 10:59:09 +00:00
w.WriteHeader(500)
w.Write([]byte(err.Error()))
2021-10-18 08:49:16 +00:00
} else {
2021-10-18 10:59:09 +00:00
w.Write(buf.Bytes())
2021-10-18 08:49:16 +00:00
}
})
tmpl = template.Must(template.New("").ParseFS(tmplFS, "templates/*.tmpl", "templates/**/*.tmpl"))
server := &http.Server{
2021-10-18 10:59:09 +00:00
Addr: flagBind,
2021-10-18 08:49:16 +00:00
WriteTimeout: time.Second * 3,
ReadTimeout: time.Second * 30,
2021-10-18 10:59:09 +00:00
Handler: LogMiddleware(mux),
2021-10-18 08:49:16 +00:00
}
// start server
2021-10-18 10:59:09 +00:00
log.Println("Listening on", flagBind)
2021-10-18 08:49:16 +00:00
log.Fatal(server.ListenAndServe())
}