1
0
Fork 0

Config is a public variable

This commit is contained in:
Gregory Eremin 2014-07-16 02:46:22 +07:00
parent 1c57066091
commit dadbcfd0f1
4 changed files with 18 additions and 25 deletions

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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)
}
}