#StackBounty: #go Golang configuration management with viper package

Bounty: 50

I’m new to GO,

I created 2 different configuration resources. Also I created a manager responsible for initializing and reading the configurations and creating default ones if needed.

Is this a clean apporoach for handling configuration through the application? Would love to get some opinions and improvement suggestions. Thanks!

config_manager.go

package conf

import (
    config "github.com/spf13/viper"
    "log"
    "os"
    "sync"
)

var(
    usersConfig = config.New()
    dbConfig = config.New()
)

func InitAllConfigurations(){

    if _, err := os.Stat("data"); os.IsNotExist(err){
        err = os.MkdirAll("data", 0755)
        if err != nil {
            log.Fatal(err)
        }
    }

    var wg = sync.WaitGroup{}
    wg.Add(2)

    go func() {
        defer wg.Done()
        InitUsersConfiguration()
    }()

    go func() {
        defer wg.Done()
        InitDbConfiguration()
    }()

    wg.Wait()
}

db_config.go

package conf

import (
    "log"
    "os"
)

var DatabaseConfig databaseConfig

type databaseConfig struct {
    Name string `mapstructure:name`
    Type string    `mapstructure:type`
    Username string `mapstructure:username`
    Password string `mapstructure:password`
}

func InitDbConfiguration() {
    dbConfig.AddConfigPath("data/")
    dbConfig.SetConfigType("json")
    dbConfig.SetConfigName("database")

    if dbConfig.ReadInConfig() != nil {
        log.Print("Failed reading users config, setting default values")

        _, err := os.OpenFile("data/database.json", os.O_RDONLY|os.O_CREATE, 0666)

        if err != nil {
            log.Println(err)
        }

        dbConfig.SetDefault("db.name", "OutlookDB")
        dbConfig.SetDefault("db.type", "mongo")
        dbConfig.SetDefault("db.username", "username")
        dbConfig.SetDefault("db.password", "password")

        err = dbConfig.WriteConfig()

        if err != nil {
            log.Println(err)
        }
    }

    dbSection := dbConfig.Sub("db")
    err := dbSection.Unmarshal(&DatabaseConfig)

    if err != nil {
        log.Print(err)
    }
}

users_config.go

package conf

import (
    "log"
    "os"
)

var UserConfiguration userConfig


type User struct {
    Id int `mapstructure:id`
    Name string `mapstructure:name`
}

type userConfig struct {
    MyId     int    `mapstructure:my_id`
    AllUsers map[int]*User `mapstructure:all_users`
}

func InitUsersConfiguration() {
    usersConfig.AddConfigPath("data/")
    usersConfig.SetConfigType("json")
    usersConfig.SetConfigName("users")

    if usersConfig.ReadInConfig() != nil {
        log.Print("Failed reading users config, setting default values")

        os.OpenFile("data/users.json", os.O_RDONLY|os.O_CREATE, 0666)

        usersConfig.SetDefault("user.my_id", 1)
        usersConfig.SetDefault("user.all_users", map[int]User{
            1: {Id:1, Name:"First"},
            2: {Id:2, Name:"Second"},
            3: {Id:3, Name:"Third"},
        })

        err := usersConfig.WriteConfig()

        if err != nil {
            log.Print(err)
        }
    }

    userSection := usersConfig.Sub("user")
    err := userSection.Unmarshal(&UserConfiguration)

    if err != nil {
        log.Print(err)
    }
}

main.go

package main

import (
    "log"
    "outlook/conf"
)

func main(){
    conf.InitAllConfigurations()
    myUserName := conf.DatabaseConfig.Username
    log.Println(myUserName)

}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.