configui/main.go

155 lines
3.4 KiB
Go
Raw Normal View History

2021-10-18 08:49:16 +00:00
package main
import (
"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"
2021-10-18 18:38:28 +00:00
"kumoly.io/tools/configui/public"
2021-10-18 08:49:16 +00:00
)
//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
flagBind string
flagNoReconfig bool
flagLogFile string
flagAllow string
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")
2021-10-18 18:38:28 +00:00
flag.StringVar(&flagAllow, "allow", "", "IPs to allow, blank to allow all")
2021-10-20 16:12:12 +00:00
flag.StringVar(&flagBind, "bind", "0.0.0.0:8000", "address to bind")
flag.BoolVar(&flagNoReconfig, "static", false, "disable config api")
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" {
2021-10-18 18:38:28 +00:00
getConfigHandler(w, r)
2021-10-18 15:53:49 +00:00
} 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)
}
})
2021-10-18 08:49:16 +00:00
2021-10-18 18:38:28 +00:00
mux.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.FS(public.FS))))
2021-10-18 08:49:16 +00:00
tmpl = template.Must(template.New("").ParseFS(tmplFS, "templates/*.tmpl", "templates/**/*.tmpl"))
2021-10-18 18:38:28 +00:00
setRoutes(mux)
2021-10-18 08:49:16 +00:00
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 18:38:28 +00:00
Handler: Middleware(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())
}