From dfd9985552bc20a146f2208d87b08fa3f4af8e89 Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Tue, 11 Sep 2018 17:49:56 +0200 Subject: [PATCH] upd(config): add pubsub and websocket, singletons --- config/database.go | 16 +++++++---- config/http.go | 22 +++++++++------ config/jwt.go | 17 ++++++++---- config/pubsub.go | 51 ++++++++++++++++++++++++++++++++++ config/websocket.go | 27 ++++++++++++++++++ sam/websocket/configuration.go | 43 ---------------------------- 6 files changed, 115 insertions(+), 61 deletions(-) create mode 100644 config/pubsub.go create mode 100644 config/websocket.go delete mode 100644 sam/websocket/configuration.go diff --git a/config/database.go b/config/database.go index 7655e7878..746ed599c 100644 --- a/config/database.go +++ b/config/database.go @@ -12,6 +12,8 @@ type ( } ) +var db *Database + func (c *Database) Validate() error { if c == nil { return nil @@ -22,13 +24,17 @@ func (c *Database) Validate() error { return nil } -func (c *Database) Init(prefix ...string) *Database { +func (*Database) Init(prefix ...string) *Database { + if db != nil { + return db + } + 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 + db := new(Database) + flag.StringVar(&db.DSN, p("db-dsn"), "crust:crust@tcp(db1:3306)/crust?collation=utf8mb4_general_ci", "DSN for database connection") + flag.StringVar(&db.Profiler, p("db-profiler"), "", "Profiler for DB queries (none, stdout)") + return db } diff --git a/config/http.go b/config/http.go index ed18947e6..ed25e5893 100644 --- a/config/http.go +++ b/config/http.go @@ -15,6 +15,8 @@ type ( } ) +var http *HTTP + func (c *HTTP) Validate() error { if c == nil { return nil @@ -25,16 +27,20 @@ func (c *HTTP) Validate() error { return nil } -func (c *HTTP) Init(prefix ...string) *HTTP { +func (*HTTP) Init(prefix ...string) *HTTP { + if http != nil { + return 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 + http := new(HTTP) + flag.StringVar(&http.Addr, p("http-addr"), ":3000", "Listen address for HTTP server") + flag.BoolVar(&http.Logging, p("http-log"), true, "Enable/disable HTTP request log") + flag.BoolVar(&http.Pretty, p("http-pretty-json"), false, "Prettify returned JSON output") + flag.BoolVar(&http.Tracing, p("http-error-tracing"), false, "Return error stack frame") + flag.BoolVar(&http.Metrics, p("http-metrics"), false, "Provide metrics export for prometheus") + return http } diff --git a/config/jwt.go b/config/jwt.go index d6cebe103..074f6e1d7 100644 --- a/config/jwt.go +++ b/config/jwt.go @@ -13,6 +13,8 @@ type ( } ) +var jwt *JWT + func (c *JWT) Validate() error { if c == nil { return nil @@ -23,9 +25,14 @@ func (c *JWT) Validate() error { 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 +func (*JWT) Init(prefix ...string) *JWT { + if jwt != nil { + return jwt + } + + jwt := new(JWT) + flag.StringVar(&jwt.Secret, "auth-jwt-secret", "", "JWT Secret") + flag.Int64Var(&jwt.Expiry, "auth-jwt-expiry", 3600, "JWT Expiration in minutes") + flag.BoolVar(&jwt.DebugToken, "auth-jwt-debug", false, "Generate debug JWT key") + return jwt } diff --git a/config/pubsub.go b/config/pubsub.go new file mode 100644 index 000000000..0f311ca8e --- /dev/null +++ b/config/pubsub.go @@ -0,0 +1,51 @@ +package config + +import ( + "github.com/namsral/flag" + "github.com/pkg/errors" + "time" +) + +type ( + PubSub struct { + Mode string + RedisAddr string + PollingInterval time.Duration + + Timeout time.Duration + PingTimeout time.Duration + PingPeriod time.Duration + } +) + +var pubsub *PubSub + +func (c *PubSub) Validate() error { + switch c.Mode { + case "redis": + if c.Mode == "redis" && c.RedisAddr == "" { + return errors.New("No host defined for mode=redis, PubSub.Redis is empty") + } + case "poll": + default: + return errors.Errorf("Unknown PubSub.Mode: %s", c.Mode) + } + return nil +} + +func (*PubSub) Init(prefix ...string) *PubSub { + if pubsub != nil { + return pubsub + } + + pubsub = new(PubSub) + pubsub.Timeout = 15 * time.Second + pubsub.PingTimeout = 60 * time.Second + pubsub.PingPeriod = (pubsub.PingTimeout * 10) / 9 + + flag.StringVar(&pubsub.Mode, "pubsub", "poll", "Pubsub mode (poll, redis)") + flag.StringVar(&pubsub.RedisAddr, "pubsub-redis", "", "Redis Pub/Sub hostname") + flag.DurationVar(&pubsub.PollingInterval, "pubsub-poll-interval", 3*time.Second, "Pub/Sub polling interval (3s, 12m, 3h...)") + + return pubsub +} diff --git a/config/websocket.go b/config/websocket.go new file mode 100644 index 000000000..15a269b6a --- /dev/null +++ b/config/websocket.go @@ -0,0 +1,27 @@ +package config + +import ( + "time" +) + +type ( + Websocket struct { + Timeout time.Duration + PingTimeout time.Duration + PingPeriod time.Duration + } +) + +var websocket *Websocket + +func (c *Websocket) Validate() error { + return nil +} + +func (*Websocket) Init(prefix ...string) *Websocket { + websocket = new(Websocket) + websocket.Timeout = 15 * time.Second + websocket.PingTimeout = 120 * time.Second + websocket.PingPeriod = (websocket.PingTimeout * 10) / 9 + return websocket +} diff --git a/sam/websocket/configuration.go b/sam/websocket/configuration.go deleted file mode 100644 index 4ffb76f50..000000000 --- a/sam/websocket/configuration.go +++ /dev/null @@ -1,43 +0,0 @@ -package websocket - -import ( - "github.com/namsral/flag" - "github.com/pkg/errors" - "time" -) - -type ( - Configuration struct { - writeTimeout time.Duration - pingTimeout time.Duration - pingPeriod time.Duration - - pubSubMode string - pubSubRedis string - pubSubInterval time.Duration - } -) - -// Validate returns error if there is an issue with the config -func (c *Configuration) Validate() error { - switch c.pubSubMode { - case "redis", "poll": - default: - return errors.Errorf("Unknown pubSubMode: %s", c.pubSubMode) - } - if c.pubSubMode == "redis" && c.pubSubRedis == "" { - return errors.New("No host defined for mode=redis, pubSubRedis is empty") - } - return nil -} - -// Init binds flags to websocket configuration structure -func (c *Configuration) Init() { - c.writeTimeout = 15 * time.Second - c.pingTimeout = 120 * time.Second - c.pingPeriod = (c.pingTimeout * 10) / 9 - - flag.StringVar(&c.pubSubMode, "pubsub", "poll", "Pubsub mode (poll, redis)") - flag.StringVar(&c.pubSubRedis, "pubsub-redis", "", "Redis Pub/Sub hostname") - flag.DurationVar(&c.pubSubInterval, "pubsub-poll-interval", 3*time.Second, "Pub/Sub polling interval (3s, 12m, 3h...)") -}