From 7c968b1ae2a951e6b228f0315c5beb838547dc13 Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Tue, 11 Sep 2018 16:37:14 +0200 Subject: [PATCH] add(config): import global config pkg --- config/database.go | 34 ++++++++++++++++++++++++++++++++++ config/http.go | 40 ++++++++++++++++++++++++++++++++++++++++ config/jwt.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 config/database.go create mode 100644 config/http.go create mode 100644 config/jwt.go diff --git a/config/database.go b/config/database.go new file mode 100644 index 000000000..7655e7878 --- /dev/null +++ b/config/database.go @@ -0,0 +1,34 @@ +package config + +import ( + "github.com/namsral/flag" + "github.com/pkg/errors" +) + +type ( + Database struct { + DSN string + Profiler string + } +) + +func (c *Database) Validate() error { + if c == nil { + return nil + } + if c.DSN == "" { + return errors.New("No DB DSN is set, can't connect to database") + } + return nil +} + +func (c *Database) Init(prefix ...string) *Database { + p := func(s string) string { + return prefix[0] + "-" + s + } + + flag.StringVar(&c.DSN, p("db-dsn"), "crust:crust@tcp(db1:3306)/crust?collation=utf8mb4_general_ci", "DSN for database connection") + flag.StringVar(&c.Profiler, p("db-profiler"), "", "Profiler for DB queries (none, stdout)") + + return c +} diff --git a/config/http.go b/config/http.go new file mode 100644 index 000000000..ed18947e6 --- /dev/null +++ b/config/http.go @@ -0,0 +1,40 @@ +package config + +import ( + "github.com/namsral/flag" + "github.com/pkg/errors" +) + +type ( + HTTP struct { + Addr string + Logging bool + Pretty bool + Tracing bool + Metrics bool + } +) + +func (c *HTTP) Validate() error { + if c == nil { + return nil + } + if c.Addr == "" { + return errors.New("No HTTP Addr is set, can't listen for HTTP") + } + return nil +} + +func (c *HTTP) Init(prefix ...string) *HTTP { + p := func(s string) string { + return prefix[0] + "-" + s + } + + flag.StringVar(&c.Addr, p("http-addr"), ":3000", "Listen address for HTTP server") + flag.BoolVar(&c.Logging, p("http-log"), true, "Enable/disable HTTP request log") + flag.BoolVar(&c.Pretty, p("http-pretty-json"), false, "Prettify returned JSON output") + flag.BoolVar(&c.Tracing, p("http-error-tracing"), false, "Return error stack frame") + flag.BoolVar(&c.Metrics, p("http-metrics"), false, "Provide metrics export for prometheus") + + return c +} diff --git a/config/jwt.go b/config/jwt.go new file mode 100644 index 000000000..d6cebe103 --- /dev/null +++ b/config/jwt.go @@ -0,0 +1,31 @@ +package config + +import ( + "github.com/namsral/flag" + "github.com/pkg/errors" +) + +type ( + JWT struct { + Secret string + Expiry int64 + DebugToken bool + } +) + +func (c *JWT) Validate() error { + if c == nil { + return nil + } + if c.Secret == "" { + return errors.New("JWT Secret not set for AUTH") + } + return nil +} + +func (c *JWT) Init(prefix ...string) *JWT { + flag.StringVar(&c.Secret, "auth-jwt-secret", "", "JWT Secret") + flag.Int64Var(&c.Expiry, "auth-jwt-expiry", 3600, "JWT Expiration in minutes") + flag.BoolVar(&c.DebugToken, "auth-jwt-debug", false, "Generate debug JWT key") + return c +}