Config is a public variable
This commit is contained in:
parent
1c57066091
commit
dadbcfd0f1
23
config.go
23
config.go
@ -2,14 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DefaultProductionStorage = "burlesque.kch#opts=c#zcomp=gz#msiz=524288000"
|
DefaultProductionStorage = "burlesque.kch#opts=c#zcomp=gz#msiz=524288000"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
var (
|
||||||
Config struct {
|
Config struct {
|
||||||
Storage string
|
Storage string
|
||||||
Env string
|
Env string
|
||||||
@ -18,22 +17,14 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
cfg = Config{}
|
|
||||||
)
|
|
||||||
|
|
||||||
func SetupConfig() {
|
func SetupConfig() {
|
||||||
cfg.Storage = *flag.String("storage", "-", "Kyoto Cabinet storage path (e.g. "+DefaultProductionStorage+")")
|
Config.Storage = *flag.String("storage", "-", "Kyoto Cabinet storage path (e.g. "+DefaultProductionStorage+")")
|
||||||
cfg.Env = *flag.String("environment", "development", "Process environment: development or production")
|
Config.Env = *flag.String("environment", "development", "Process environment: development or production")
|
||||||
cfg.Port = *flag.Int("port", 4401, "Server HTTP port")
|
Config.Port = *flag.Int("port", 4401, "Server HTTP port")
|
||||||
cfg.Rollbar = *flag.String("rollbar", "", "Rollbar token")
|
Config.Rollbar = *flag.String("rollbar", "", "Rollbar token")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if cfg.Env == "production" && cfg.Storage == "-" {
|
if Config.Env == "production" && Config.Storage == "-" {
|
||||||
cfg.Storage = DefaultProductionStorage
|
Config.Storage = DefaultProductionStorage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Config) PortString() string {
|
|
||||||
return fmt.Sprintf(":%d", cfg.Port)
|
|
||||||
}
|
|
||||||
|
10
logging.go
10
logging.go
@ -12,8 +12,8 @@ var (
|
|||||||
|
|
||||||
func SetupLogging() {
|
func SetupLogging() {
|
||||||
logger = log.New(os.Stdout, "", log.Ldate|log.Lmicroseconds)
|
logger = log.New(os.Stdout, "", log.Ldate|log.Lmicroseconds)
|
||||||
rollbar.Token = cfg.Rollbar
|
rollbar.Token = Config.Rollbar
|
||||||
rollbar.Environment = cfg.Env
|
rollbar.Environment = Config.Env
|
||||||
}
|
}
|
||||||
|
|
||||||
func Log(format string, args ...interface{}) {
|
func Log(format string, args ...interface{}) {
|
||||||
@ -21,7 +21,7 @@ func Log(format string, args ...interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Debug(format string, args ...interface{}) {
|
func Debug(format string, args ...interface{}) {
|
||||||
if cfg.Env == "development" {
|
if Config.Env == "development" {
|
||||||
logger.Printf("[DEBUG] "+format, args...)
|
logger.Printf("[DEBUG] "+format, args...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -29,10 +29,10 @@ func Debug(format string, args ...interface{}) {
|
|||||||
func Error(err error, format string, args ...interface{}) {
|
func Error(err error, format string, args ...interface{}) {
|
||||||
logger.Printf("[ERROR] "+format, args...)
|
logger.Printf("[ERROR] "+format, args...)
|
||||||
|
|
||||||
if cfg.Env == "development" {
|
if Config.Env == "development" {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if cfg.Rollbar != "" {
|
if Config.Rollbar != "" {
|
||||||
rollbar.Error("error", err)
|
rollbar.Error("error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
6
main.go
6
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/stvp/rollbar"
|
"github.com/stvp/rollbar"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -41,7 +42,8 @@ func main() {
|
|||||||
go PersistMessages()
|
go PersistMessages()
|
||||||
|
|
||||||
Log("GOMAXPROCS = %d", runtime.GOMAXPROCS(-1))
|
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)
|
||||||
}
|
}
|
||||||
|
@ -27,9 +27,9 @@ func NewKey(queue string, index uint) Key {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SetupStorage() {
|
func SetupStorage() {
|
||||||
err := storage.Open(cfg.Storage, cabinet.KCOWRITER|cabinet.KCOCREATE)
|
err := storage.Open(Config.Storage, cabinet.KCOWRITER|cabinet.KCOCREATE)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error(err, "Failed to open database '%s'", cfg.Storage)
|
Error(err, "Failed to open database '%s'", Config.Storage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user