diff --git a/config.go b/config.go index ba39b4f..cfef09e 100644 --- a/config.go +++ b/config.go @@ -2,14 +2,13 @@ package main import ( "flag" - "fmt" ) const ( DefaultProductionStorage = "burlesque.kch#opts=c#zcomp=gz#msiz=524288000" ) -type ( +var ( Config struct { Storage string Env string @@ -18,22 +17,14 @@ type ( } ) -var ( - cfg = Config{} -) - func SetupConfig() { - cfg.Storage = *flag.String("storage", "-", "Kyoto Cabinet storage path (e.g. "+DefaultProductionStorage+")") - cfg.Env = *flag.String("environment", "development", "Process environment: development or production") - cfg.Port = *flag.Int("port", 4401, "Server HTTP port") - cfg.Rollbar = *flag.String("rollbar", "", "Rollbar token") + Config.Storage = *flag.String("storage", "-", "Kyoto Cabinet storage path (e.g. "+DefaultProductionStorage+")") + Config.Env = *flag.String("environment", "development", "Process environment: development or production") + Config.Port = *flag.Int("port", 4401, "Server HTTP port") + Config.Rollbar = *flag.String("rollbar", "", "Rollbar token") flag.Parse() - if cfg.Env == "production" && cfg.Storage == "-" { - cfg.Storage = DefaultProductionStorage + if Config.Env == "production" && Config.Storage == "-" { + Config.Storage = DefaultProductionStorage } } - -func (c Config) PortString() string { - return fmt.Sprintf(":%d", cfg.Port) -} diff --git a/logging.go b/logging.go index ec8afed..cdefcdd 100644 --- a/logging.go +++ b/logging.go @@ -12,8 +12,8 @@ var ( func SetupLogging() { logger = log.New(os.Stdout, "", log.Ldate|log.Lmicroseconds) - rollbar.Token = cfg.Rollbar - rollbar.Environment = cfg.Env + rollbar.Token = Config.Rollbar + rollbar.Environment = Config.Env } func Log(format string, args ...interface{}) { @@ -21,7 +21,7 @@ func Log(format string, args ...interface{}) { } func Debug(format string, args ...interface{}) { - if cfg.Env == "development" { + if Config.Env == "development" { logger.Printf("[DEBUG] "+format, args...) } } @@ -29,10 +29,10 @@ func Debug(format string, args ...interface{}) { func Error(err error, format string, args ...interface{}) { logger.Printf("[ERROR] "+format, args...) - if cfg.Env == "development" { + if Config.Env == "development" { panic(err) } - if cfg.Rollbar != "" { + if Config.Rollbar != "" { rollbar.Error("error", err) } } diff --git a/main.go b/main.go index 158a726..d3b8c70 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "github.com/stvp/rollbar" "net/http" "os" @@ -41,7 +42,8 @@ func main() { go PersistMessages() Log("GOMAXPROCS = %d", runtime.GOMAXPROCS(-1)) - Log("Starting HTTP server on port %d", cfg.Port) + Log("Starting HTTP server on port %d", Config.Port) - http.ListenAndServe(cfg.PortString(), nil) + port := fmt.Sprintf(":%d", Config.Port) + http.ListenAndServe(port, nil) } diff --git a/storage.go b/storage.go index 444e08c..bf556f9 100644 --- a/storage.go +++ b/storage.go @@ -27,9 +27,9 @@ func NewKey(queue string, index uint) Key { } func SetupStorage() { - err := storage.Open(cfg.Storage, cabinet.KCOWRITER|cabinet.KCOCREATE) + err := storage.Open(Config.Storage, cabinet.KCOWRITER|cabinet.KCOCREATE) if err != nil { - Error(err, "Failed to open database '%s'", cfg.Storage) + Error(err, "Failed to open database '%s'", Config.Storage) } }