Skip to content

Instantly share code, notes, and snippets.

@eamonnmcevoy
Created March 13, 2017 10:18
Show Gist options
  • Save eamonnmcevoy/a3c377142676a64236492f7dc820822c to your computer and use it in GitHub Desktop.
Save eamonnmcevoy/a3c377142676a64236492f7dc820822c to your computer and use it in GitHub Desktop.
package config
import (
"os"
"fmt"
"encoding/json"
"path/filepath"
"go_rest_api/pkg"
)
func GetConfig() *root.Config {
configuration, err := loadConfig()
if(err != nil) {
fmt.Println("Unable to load config file, using defaults")
configuration = getDefaultConfig()
return configuration
}
return configuration
}
func loadConfig() (*root.Config,error) {
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
file, err := os.Open(dir+"/config.json")
if(err != nil) {
return nil, err
}
decoder := json.NewDecoder(file)
configuration := root.Config{}
err = decoder.Decode(&configuration)
return &configuration, err
}
func getDefaultConfig() *root.Config {
return &root.Config {
Mongo: &root.MongoConfig {
Ip: "127.0.0.1:27017",
DbName: "myDb" },
Server: &root.ServerConfig { Port: ":1337"},
Auth: &root.AuthConfig { Secret: "mysecret"} }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment