1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

Introduce config ini.

This commit is contained in:
Ferdinand Mütsch 2019-05-16 22:51:11 +02:00
parent ecf69795ba
commit c7a8372516
3 changed files with 33 additions and 17 deletions

5
config.ini Normal file
View File

@ -0,0 +1,5 @@
[server]
port = 3000
[data]
aggregation_interval = 24h

30
main.go
View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"log"
"net/http"
"os"
@ -11,6 +12,7 @@ import (
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
"github.com/joho/godotenv"
ini "gopkg.in/ini.v1"
"github.com/n1try/wakapi/middlewares"
"github.com/n1try/wakapi/models"
@ -26,26 +28,32 @@ func readConfig() models.Config {
log.Fatal(err)
}
port, err := strconv.Atoi(os.Getenv("WAKAPI_PORT"))
dbUser, valid := os.LookupEnv("WAKAPI_DB_USER")
dbPassword, valid := os.LookupEnv("WAKAPI_DB_PASSWORD")
dbHost, valid := os.LookupEnv("WAKAPI_DB_HOST")
dbName, valid := os.LookupEnv("WAKAPI_DB_NAME")
if err != nil {
log.Fatal(err)
}
if !valid {
log.Fatal("Config parameters missing.")
log.Fatal("Environment variables missing.")
}
cfg, err := ini.Load("config.ini")
if err != nil {
log.Fatal(fmt.Sprintf("Fail to read file: %v", err))
}
port := cfg.Section("server").Key("port").MustInt()
intervalStr := cfg.Section("data").Key("aggregation_interval").String()
interval, _ := time.ParseDuration(intervalStr)
return models.Config{
Port: port,
DbHost: dbHost,
DbUser: dbUser,
DbPassword: dbPassword,
DbName: dbName,
DbDialect: "mysql",
Port: port,
DbHost: dbHost,
DbUser: dbUser,
DbPassword: dbPassword,
DbName: dbName,
DbDialect: "mysql",
AggregationInterval: interval,
}
}

View File

@ -1,10 +1,13 @@
package models
import "time"
type Config struct {
Port int
DbHost string
DbUser string
DbPassword string
DbName string
DbDialect string
Port int
DbHost string
DbUser string
DbPassword string
DbName string
DbDialect string
AggregationInterval time.Duration
}