From d0b59cb4116077890768ff8b693ef99cf45f28a8 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 2 May 2021 22:49:51 +0200 Subject: [PATCH] Add some config through flag/env --- app.go | 13 +++++++++---- config/cli.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ config/config.go | 28 ++++++++++++++++++++++++++++ config/env.go | 21 +++++++++++++++++++++ main.go | 9 ++++++++- ui/routes.go | 10 +++++++++- 6 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 config/cli.go create mode 100644 config/config.go create mode 100644 config/env.go diff --git a/app.go b/app.go index 7f95e8f..bdab283 100644 --- a/app.go +++ b/app.go @@ -13,6 +13,7 @@ import ( "github.com/gin-gonic/gin" "nhooyr.io/websocket" + "github.com/nemunaire/minifaas/config" "github.com/nemunaire/minifaas/engine/docker" "github.com/nemunaire/minifaas/ui" ) @@ -20,17 +21,20 @@ import ( type App struct { router *gin.Engine srv *http.Server + cfg *config.Config } -func NewApp() App { +func NewApp(cfg *config.Config) App { //Check rights on artifacts directory os.MkdirAll("./artifacts", os.ModePerm) - //gin.SetMode(gin.ReleaseMode) + if !cfg.Dev { + gin.SetMode(gin.ReleaseMode) + } gin.ForceConsoleColor() router := gin.Default() - ui.DeclareRoutes(router) + ui.DeclareRoutes(router, cfg) artifacts := http.Dir("./artifacts") router.GET("/artifacts/*path", func(c *gin.Context) { @@ -123,6 +127,7 @@ func NewApp() App { app := App{ router: router, + cfg: cfg, } return app @@ -130,7 +135,7 @@ func NewApp() App { func (app *App) Start() { app.srv = &http.Server{ - Addr: ":8082", + Addr: app.cfg.Bind, Handler: app.router, } diff --git a/config/cli.go b/config/cli.go new file mode 100644 index 0000000..2bacfb1 --- /dev/null +++ b/config/cli.go @@ -0,0 +1,45 @@ +package config + +import ( + "flag" +) + +// declareFlags registers flags for the structure Options. +func (c *Config) declareFlags() { + flag.BoolVar(&c.Dev, "dev", c.Dev, "Use ui directory instead of embedded assets") + flag.StringVar(&c.Bind, "bind", c.Bind, "Bind port/socket") + flag.StringVar(&c.ExternalURL, "externalurl", c.ExternalURL, "Begining of the URL, before the base, that should be used eg. in mails") + + // Others flags are declared in some other files when they need specials configurations +} + +func Consolidated() (cfg *Config, err error) { + // Define defaults options + cfg = &Config{ + Bind: ":8082", + ExternalURL: "http://localhost:8082", + } + + cfg.declareFlags() + + // Then, overwrite that by what is present in the environment + err = cfg.FromEnv() + if err != nil { + return + } + + // Finaly, command line takes precedence + err = cfg.parseCLI() + if err != nil { + return + } + + return +} + +// parseCLI parse the flags and treats extra args as configuration filename. +func (c *Config) parseCLI() error { + flag.Parse() + + return nil +} diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..579e835 --- /dev/null +++ b/config/config.go @@ -0,0 +1,28 @@ +package config + +import ( + "flag" + "strings" +) + +type Config struct { + Dev bool + Bind string + ExternalURL string +} + +// parseLine treats a config line and place the read value in the variable +// declared to the corresponding flag. +func (c *Config) parseLine(line string) (err error) { + fields := strings.SplitN(line, "=", 2) + orig_key := strings.TrimSpace(fields[0]) + value := strings.TrimSpace(fields[1]) + + key := strings.TrimPrefix(orig_key, "MINIFAAS_") + key = strings.Replace(key, "_", "-", -1) + key = strings.ToLower(key) + + err = flag.Set(key, value) + + return +} diff --git a/config/env.go b/config/env.go new file mode 100644 index 0000000..d54bc3d --- /dev/null +++ b/config/env.go @@ -0,0 +1,21 @@ +package config + +import ( + "fmt" + "os" + "strings" +) + +// FromEnv analyzes all the environment variables to find each one +// starting by MINIFAAS_ +func (c *Config) FromEnv() error { + for _, line := range os.Environ() { + if strings.HasPrefix(line, "MINIFAAS_") { + err := c.parseLine(line) + if err != nil { + return fmt.Errorf("error in environment (%q): %w", line, err) + } + } + } + return nil +} diff --git a/main.go b/main.go index fe4ef9c..6e114fb 100644 --- a/main.go +++ b/main.go @@ -5,10 +5,17 @@ import ( "os" "os/signal" "syscall" + + "github.com/nemunaire/minifaas/config" ) func main() { - a := NewApp() + cfg, err := config.Consolidated() + if err != nil { + log.Fatal("Unable to read configuration:", err) + } + + a := NewApp(cfg) go a.Start() quit := make(chan os.Signal) diff --git a/ui/routes.go b/ui/routes.go index 4bf01b8..8646934 100644 --- a/ui/routes.go +++ b/ui/routes.go @@ -1,10 +1,18 @@ package ui import ( + "net/http" + "github.com/gin-gonic/gin" + + "github.com/nemunaire/minifaas/config" ) -func DeclareRoutes(router *gin.Engine) { +func DeclareRoutes(router *gin.Engine, cfg *config.Config) { + if cfg.Dev { + Assets = http.Dir("./ui") + } + router.GET("/", serveOrReverse("/")) router.GET("/favicon.ico", serveOrReverse("/favicon.ico")) router.GET("/manifest.json", serveOrReverse("/manifest.json"))