socialnetworknews-api

git clone git://archive.git.mtrnord.blog/SocialNetworkNews/socialnetworknews-api.git
Log | Files | Refs | LICENSE

config.go (951B)


      1 package config
      2 
      3 import (
      4 	"gopkg.in/yaml.v2"
      5 	"io/ioutil"
      6 	"path/filepath"
      7 	"sync"
      8 )
      9 
     10 var gConfig *Config
     11 var configOnce sync.Once
     12 
     13 type Config struct {
     14 	TwitterConfig `yaml:"twitter"`
     15 }
     16 
     17 type TwitterConfig struct {
     18 	ConsumerKey    string   `yaml:"consumerKey"`
     19 	ConsumerSecret string   `yaml:"consumerSecret"`
     20 	Lists          []string `yaml:"lists,flow"`
     21 	Hashtags       []string `yaml:"hashtags,flow"`
     22 }
     23 
     24 func ConfigPath() string {
     25 	filePath := filepath.ToSlash("/etc/SocialNetworksNews")
     26 	return filePath
     27 }
     28 
     29 func GetConfig() (*Config, error) {
     30 	var gFerr error
     31 	configOnce.Do(func() {
     32 		config := &Config{}
     33 		filePath := ConfigPath()
     34 		b, err := ioutil.ReadFile(filepath.Join(filePath, "config.yaml")) // just pass the file name
     35 		if err != nil {
     36 			gFerr = err
     37 			return
     38 		}
     39 
     40 		err = yaml.Unmarshal(b, config)
     41 		if err != nil {
     42 			gFerr = err
     43 			return
     44 		}
     45 
     46 		gConfig = config
     47 	})
     48 	if gFerr != nil {
     49 		return nil, gFerr
     50 	}
     51 
     52 	return gConfig, nil
     53 }