first commit
This commit is contained in:
52
config/config.go
Normal file
52
config/config.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"gopkg.in/yaml.v3"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Configuration struct {
|
||||
Database DatabaseConfiguration `yaml:"database"`
|
||||
Features FeaturesConfiguration `yaml:"features"`
|
||||
Cache string `yaml:"cache"`
|
||||
}
|
||||
|
||||
type DatabaseConfiguration struct {
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
Username string `yaml:"username"`
|
||||
Password string `yaml:"password"`
|
||||
}
|
||||
|
||||
type FeaturesConfiguration struct {
|
||||
AllowRegister bool `yaml:"allow_register"`
|
||||
}
|
||||
|
||||
var currentConfig *Configuration
|
||||
|
||||
func init() {
|
||||
path := flag.String("config", "./config.yml", "Set the configuration file path")
|
||||
flag.Parse()
|
||||
configYamlContent, err := os.ReadFile(*path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = yaml.Unmarshal(configYamlContent, ¤tConfig)
|
||||
if err != nil {
|
||||
log.Fatalf("error: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func Database() *DatabaseConfiguration {
|
||||
return ¤tConfig.Database
|
||||
}
|
||||
|
||||
func Features() *FeaturesConfiguration {
|
||||
return ¤tConfig.Features
|
||||
}
|
||||
|
||||
func Cache() string {
|
||||
return currentConfig.Cache
|
||||
}
|
||||
Reference in New Issue
Block a user