1
0
Fork 0
empact/config/config.go

55 lines
1.0 KiB
Go
Raw Permalink Normal View History

2015-03-05 07:44:42 +00:00
package config
import (
"encoding/json"
"flag"
"io/ioutil"
2015-03-06 13:23:01 +00:00
"log"
2015-03-05 07:44:42 +00:00
"os"
)
type (
Config struct {
2015-03-18 17:29:20 +00:00
ServerPort int `json:"app_server_port"`
RedisPort int `json:"app_redis_port"`
2015-03-06 17:30:47 +00:00
DatabaseURI string `json:"app_database_uri"`
2015-03-05 12:53:38 +00:00
AuthURL string `json:"github_auth_url"`
AccessTokenURL string `json:"github_access_token_url"`
ClientID string `json:"github_client_id"`
ClientSecret string `json:"github_client_secret"`
RedirectURI string `json:"github_redirect_uri"`
2015-03-05 07:44:42 +00:00
}
)
var (
2015-03-28 17:06:01 +00:00
path string
2015-03-05 07:44:42 +00:00
conf Config
)
func init() {
flag.StringVar(&path, "config", "config.json", "Path to configuration file")
2015-03-28 17:06:01 +00:00
}
2015-03-05 07:44:42 +00:00
2015-03-28 17:06:01 +00:00
func Load() {
var err error
2015-03-06 13:23:01 +00:00
var fd *os.File
2015-03-05 07:44:42 +00:00
if fd, err = os.Open(path); err != nil {
panic(err)
}
2015-03-06 13:23:01 +00:00
var contents []byte
2015-03-05 07:44:42 +00:00
if contents, err = ioutil.ReadAll(fd); err != nil {
panic(err)
}
if err = json.Unmarshal(contents, &conf); err != nil {
panic(err)
}
2015-03-06 13:23:01 +00:00
log.SetOutput(os.Stderr)
2015-03-06 17:16:56 +00:00
log.SetFlags(log.Ldate | log.Ltime)
2015-03-05 07:44:42 +00:00
}
2015-03-28 17:06:01 +00:00
// Config is immutable and is always returned by value
func C() Config {
return conf
}