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 (
|
|
|
|
conf Config
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is immutable and is always returned by value
|
|
|
|
func C() Config {
|
|
|
|
return conf
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2015-03-06 13:23:01 +00:00
|
|
|
var err error
|
|
|
|
var path string
|
2015-03-05 07:44:42 +00:00
|
|
|
flag.StringVar(&path, "config", "config.json", "Path to configuration file")
|
|
|
|
flag.Parse()
|
|
|
|
|
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
|
|
|
}
|