Refactor how config is used.

This commit is contained in:
eccentric 2023-11-01 21:51:14 +00:00
parent 226241588c
commit 4607e42476
7 changed files with 85 additions and 60 deletions

View File

@ -14,7 +14,7 @@ tmp_dir = "tmp"
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_ext = ["go", "tpl", "tmpl", "html", "ini"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"

53
aid/config.go Normal file
View File

@ -0,0 +1,53 @@
package aid
import (
"os"
"gopkg.in/ini.v1"
)
type CS struct {
Database struct {
URI string
Type string
}
Output struct {
Level string
}
}
var (
Config *CS
)
func LoadConfig() {
Config = &CS{}
configPath := "config.ini"
if _, err := os.Stat(configPath); err != nil {
panic("config.ini not found! please rename default.config.ini to config.ini and complete")
}
cfg, err := ini.Load("config.ini")
if err != nil {
panic(err)
}
Config.Database.URI = cfg.Section("database").Key("uri").String()
if Config.Database.URI == "" {
panic("Database URI is empty")
}
Config.Database.Type = cfg.Section("database").Key("type").String()
if Config.Database.Type == "" {
panic("Database Type is empty")
}
Config.Output.Level = cfg.Section("output").Key("level").String()
if Config.Output.Level == "" {
panic("Output Level is empty")
}
if Config.Output.Level != "dev" && Config.Output.Level != "prod" && Config.Output.Level != "time" && Config.Output.Level != "info" {
panic("Output Level must be either dev or prod")
}
}

View File

@ -3,12 +3,29 @@ package aid
import (
"encoding/json"
"fmt"
"time"
)
func PrintJSON(v interface{}) {
if Config.Output.Level == "prod" || Config.Output.Level == "time" {
return
}
json1, err := json.MarshalIndent(v, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(json1))
}
func PrintTime(label string, functions ...func()) {
if Config.Output.Level == "prod" {
return
}
current := time.Now()
for _, f := range functions {
f()
}
fmt.Println(label + ":", time.Since(current))
}

View File

@ -1,52 +0,0 @@
package config
import (
"os"
"gopkg.in/ini.v1"
)
var (
local *Config
)
type Config struct {
Database struct {
URI string
Type string
}
}
func NewConfig() *Config {
return &Config{}
}
func (c *Config) Load() {
configPath := "config.ini"
if _, err := os.Stat(configPath); err != nil {
panic("config.ini not found! please rename default.config.ini to config.ini and complete")
}
cfg, err := ini.Load("config.ini")
if err != nil {
panic(err)
}
c.Database.URI = cfg.Section("database").Key("uri").String()
if c.Database.URI == "" {
panic("Database URI is empty")
}
c.Database.Type = cfg.Section("database").Key("type").String()
if c.Database.Type == "" {
panic("Database Type is empty")
}
}
func Get() *Config {
if local == nil {
local = NewConfig()
local.Load()
}
return local
}

View File

@ -3,3 +3,7 @@
uri="host=localhost user=postgres password=pass dbname=snow port=5432 sslmode=disable"
; postgres
type="postgres"
[output]
; level of logging
level="dev" # dev, prod

11
main.go
View File

@ -2,7 +2,6 @@ package main
import (
"github.com/ectrc/snow/aid"
"github.com/ectrc/snow/config"
"github.com/ectrc/snow/person"
"github.com/ectrc/snow/storage"
)
@ -12,11 +11,11 @@ const (
)
func init() {
config := config.Get()
aid.LoadConfig()
var device storage.Storage
switch config.Database.Type {
switch aid.Config.Database.Type {
case "postgres":
postgresStorage := storage.NewPostgresStorage()
@ -90,7 +89,11 @@ func init() {
}
func main() {
users := person.AllFromDatabase()
var users []*person.Person
aid.PrintTime("Fetching Persons", func() {
users = person.AllFromDatabase()
})
for _, user := range users {
aid.PrintJSON(user.Snapshot())

View File

@ -1,7 +1,7 @@
package storage
import (
"github.com/ectrc/snow/config"
"github.com/ectrc/snow/aid"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
@ -11,7 +11,7 @@ type PostgresStorage struct {
}
func NewPostgresStorage() *PostgresStorage {
db, err := gorm.Open(postgres.Open(config.Get().Database.URI), &gorm.Config{})
db, err := gorm.Open(postgres.Open(aid.Config.Database.URI), &gorm.Config{})
if err != nil {
panic(err)
}