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 package main
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -11,6 +12,7 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/joho/godotenv" "github.com/joho/godotenv"
ini "gopkg.in/ini.v1"
"github.com/n1try/wakapi/middlewares" "github.com/n1try/wakapi/middlewares"
"github.com/n1try/wakapi/models" "github.com/n1try/wakapi/models"
@ -26,26 +28,32 @@ func readConfig() models.Config {
log.Fatal(err) log.Fatal(err)
} }
port, err := strconv.Atoi(os.Getenv("WAKAPI_PORT"))
dbUser, valid := os.LookupEnv("WAKAPI_DB_USER") dbUser, valid := os.LookupEnv("WAKAPI_DB_USER")
dbPassword, valid := os.LookupEnv("WAKAPI_DB_PASSWORD") dbPassword, valid := os.LookupEnv("WAKAPI_DB_PASSWORD")
dbHost, valid := os.LookupEnv("WAKAPI_DB_HOST") dbHost, valid := os.LookupEnv("WAKAPI_DB_HOST")
dbName, valid := os.LookupEnv("WAKAPI_DB_NAME") dbName, valid := os.LookupEnv("WAKAPI_DB_NAME")
if err != nil {
log.Fatal(err)
}
if !valid { 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{ return models.Config{
Port: port, Port: port,
DbHost: dbHost, DbHost: dbHost,
DbUser: dbUser, DbUser: dbUser,
DbPassword: dbPassword, DbPassword: dbPassword,
DbName: dbName, DbName: dbName,
DbDialect: "mysql", DbDialect: "mysql",
AggregationInterval: interval,
} }
} }

View File

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