upd(config): add pubsub and websocket, singletons
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
51
config/pubsub.go
Normal file
51
config/pubsub.go
Normal file
@@ -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
|
||||
}
|
||||
27
config/websocket.go
Normal file
27
config/websocket.go
Normal file
@@ -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
|
||||
}
|
||||
@@ -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...)")
|
||||
}
|
||||
Reference in New Issue
Block a user