Skip to content

Commit

Permalink
Abandon YAML and switch to TOML
Browse files Browse the repository at this point in the history
Mainflux has very simple configuration. No need for all the YAML
structures. YAML is complex and overkill. Keep it lean.

Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
  • Loading branch information
drasko committed Oct 2, 2016
1 parent 04710ad commit 551172e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 59 deletions.
23 changes: 10 additions & 13 deletions config/config-docker.yml → config/config-docker.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
# See the included LICENSE file for more details.
###

###
# HTPP Server
###
http:
host: "0.0.0.0"
port: 7070
# HTTP
httpHost = "0.0.0.0"
httpPort = 7070

###
# MongoDB
###
mongo:
host: "mongo"
port: 27017
db: "mainflux"
# Mongo
mongoHost = "mongo"
mongoPort = 27017
mongoDatabase = "mainflux"

# MQTT
mqttHost = "emqttd"
mqttPort = 1833
54 changes: 22 additions & 32 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,49 @@ package config

import (
"fmt"
"github.com/spf13/viper"
"github.com/BurntSushi/toml"
"os"
)

type Config struct {
// HTTP
HttpHost string
HttpPort int
HttpHost string
HttpPort int

// Mongo
MongoHost string
MongoPort int
MongoDatabase string
MongoHost string
MongoPort int
MongoDatabase string

// MQTT
MqttHost string
MqttPort int

// Influx
InfluxHost string
InfluxPort int
InfluxDatabase string
InfluxHost string
InfluxPort int
InfluxDatabase string
}


func (this *Config) Parse() {
/**
* Config
*/
/** Viper setup */
viper.SetConfigType("yaml") // or viper.SetConfigType("YAML")
func (cfg *Config) Parse() {

var confFile string

testEnv := os.Getenv("TEST_ENV")
if testEnv == "" && len(os.Args) > 1 {
// We are not in the TEST_ENV (where different args are provided)
// and provided config file as an argument
viper.SetConfigFile(os.Args[1])
confFile = os.Args[1]
} else {
// default cfg path to source dir, as we keep cfg.yml there
cfgDir := os.Getenv("GOPATH") + "/src/github.com/mainflux/mainflux/config"
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath(cfgDir) // path to look for the config file in
confFile = os.Getenv("GOPATH") + "/src/github.com/mainflux/mainflux/config/config.toml"
}

err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
if _, err := toml.DecodeFile(confFile, &cfg); err != nil {
// handle error
fmt.Println("Error parsing Toml")
}

this.MongoHost = viper.GetString("mongo.host")
this.MongoPort = viper.GetInt("mongo.port")
this.MongoDatabase = viper.GetString("mongo.db")

this.InfluxHost = viper.GetString("influx.host")
this.InfluxPort = viper.GetInt("influx.port")
this.InfluxDatabase = viper.GetString("influx.db")

this.HttpHost = viper.GetString("http.host")
this.HttpPort = viper.GetInt("http.port")
fmt.Println(cfg)
}
24 changes: 10 additions & 14 deletions config/config.yml → config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@
# See the included LICENSE file for more details.
###

###
# HTPP Server
###
http:
host: "0.0.0.0"
port: 7070

###
# MongoDB
###
mongo:
host: "localhost"
port: 27017
db: "mainflux"
# HTTP
httpHost = "0.0.0.0"
httpPort = 7070

# Mongo
mongoHost = "localhost"
mongoPort = 27017
mongoDatabase = "mainflux"

# MQTT
mqttHost = "localhost"
mqttPort = 1833

0 comments on commit 551172e

Please sign in to comment.