arnaucube
arnaucube/gogame-oldPublic Self-hosted repository
gogame-old/config/config.go
config/config.goRaw
 1package config
 2
 3import (
 4	"strings"
 5
 6	"github.com/spf13/viper"
 7	"github.com/urfave/cli"
 8)
 9
10type Config struct {
11	Server struct {
12		ServiceApi         string
13		AdminApi           string
14		CorsOriginsAllowed string
15	}
16	Mongodb struct {
17		Url      string
18		Database string
19	}
20}
21
22var C Config
23
24func MustRead(c *cli.Context) error {
25
26	viper.SetConfigType("yaml")
27	viper.SetConfigName("config")
28	viper.AddConfigPath(".")     // adding home directory as first search path
29	viper.SetEnvPrefix("gogame") // so viper.AutomaticEnv will get matching envvars starting with O2M_
30	viper.AutomaticEnv()         // read in environment variables that match
31	viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
32
33	if c.GlobalString("config") != "" {
34		viper.SetConfigFile(c.GlobalString("config"))
35	}
36
37	if err := viper.ReadInConfig(); err != nil {
38		return err
39	}
40	if err := viper.Unmarshal(&C); err != nil {
41		return err
42	}
43	return nil
44}