112 lines
2.3 KiB
Go
112 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"kumoly.io/tools/configui"
|
|
)
|
|
|
|
var (
|
|
flagPath string
|
|
flagAction string
|
|
flagName string
|
|
flagConfigPath string
|
|
|
|
flagBind string
|
|
flagNoReconfig bool
|
|
flagLogFile string
|
|
flagAllow string
|
|
flagVer bool
|
|
)
|
|
|
|
var Version = "0.0.0"
|
|
var Build = "alpha"
|
|
|
|
func init() {
|
|
// log.SetFlags(0)
|
|
|
|
flag.StringVar(&flagConfigPath, "f", "", "path to config file")
|
|
flag.StringVar(&flagPath, "p", "", "path to file, precedence over config")
|
|
flag.StringVar(&flagName, "n", "", "Name of file")
|
|
flag.StringVar(&flagAction, "c", "", "cmd to apply")
|
|
flag.StringVar(&flagLogFile, "log", "", "log to file")
|
|
flag.StringVar(&flagAllow, "allow", "", "IPs to allow, blank to allow all")
|
|
flag.StringVar(&flagBind, "bind", "0.0.0.0:8000", "address to bind")
|
|
flag.BoolVar(&flagNoReconfig, "static", false, "disable config api")
|
|
flag.BoolVar(&flagVer, "v", false, "show version")
|
|
flag.Usage = func() {
|
|
fmt.Fprintf(os.Stderr, "Usage: configui [options]\n")
|
|
flag.PrintDefaults()
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
if flagVer {
|
|
fmt.Printf("%v - %v\n", Version, Build)
|
|
return
|
|
}
|
|
|
|
cui := configui.New()
|
|
|
|
// 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)
|
|
}
|
|
|
|
// setup values
|
|
if flagPath != "" {
|
|
if flagName == "" {
|
|
flagName = flagPath
|
|
}
|
|
file := &configui.File{
|
|
Path: flagPath,
|
|
Name: flagName,
|
|
Action: flagAction,
|
|
}
|
|
if err := cui.AppendFile(file); err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
} else if flagConfigPath == "" {
|
|
log.Println("no config specified")
|
|
} else {
|
|
conf, err := os.ReadFile(flagConfigPath)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
cui.LoadConfig(string(conf))
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
}
|
|
|
|
cui.LogPath = flagLogFile
|
|
cui.ConfigPath = flagConfigPath
|
|
cui.NoReconfig = flagNoReconfig
|
|
|
|
// setup routes
|
|
server := &http.Server{
|
|
Addr: flagBind,
|
|
WriteTimeout: time.Second * 30,
|
|
ReadTimeout: time.Second * 30,
|
|
Handler: cui,
|
|
}
|
|
|
|
// start server
|
|
log.Println("Listening on", flagBind)
|
|
log.Fatal(server.ListenAndServe())
|
|
}
|