Initial commit

This commit is contained in:
2021-09-14 19:25:37 +02:00
parent caadfddb6f
commit b12c5e579e
7 changed files with 319 additions and 0 deletions

23
config/config.go Normal file
View File

@@ -0,0 +1,23 @@
package config
// AppConfig contains application configuration
var AppConfig Config
// Load application configuration
func Load() {
AppConfig = Config{
Service: Service{
Port: getEnv("OPENFIRE_PORT", "5222"),
Address: mustGetEnv("OPENFIRE_ADRESS"),
Domain: mustGetEnv("OPENFIRE_DOMAIN"),
},
Credentials: Credentials{
CredentialsFileLocation: getEnv("CREDENTIALS_FILE_LOCATION", "input.json"),
},
GeneralOptions: GeneralOptions{
DelayBetweenMassages: int64(getEnvInt("MASSAGE_DELAY", 120000000000)),
},
}
}

56
config/helpers.go Normal file
View File

@@ -0,0 +1,56 @@
package config
import (
"log"
"os"
"strconv"
)
// getEnv returns value for give key from environment
// if key is not present in environment it returns defaultValue
func getEnv(key, defaultValue string) string {
v := os.Getenv(key)
if len(v) > 0 {
return v
}
return defaultValue
}
// getEnvInt returns integer value for give key from environment
// if key is not present in environment it returns defaultValue
// if key cannot be parsed to integer function will panic
func getEnvInt(key string, defaultValue int) int {
v := os.Getenv(key)
if len(v) == 0 {
return defaultValue
}
valInteger, err := strconv.Atoi(v)
if err != nil {
log.Fatalf("variable `%s` cannot be parsed to INTEGER", key)
}
return valInteger
}
// getEnv extracts the bool value from environment variable
func getEnvBool(env string) bool {
envVal := mustGetEnv(env)
value, err := strconv.ParseBool(envVal)
if err != nil {
value = false
}
return value
}
// mustGetEnv returns value for give key from environment
// if key is not present in environment function will panic
func mustGetEnv(key string) string {
v := os.Getenv(key)
if len(v) == 0 {
log.Fatalf(" variable `%s` is not present in ENVIRONMENT", key)
}
return v
}

25
config/models.go Normal file
View File

@@ -0,0 +1,25 @@
package config
// Config stores application configuration
type Config struct {
Service Service
Credentials Credentials
GeneralOptions GeneralOptions
}
// Service contains configuration for service
type Service struct {
Port string
Address string
Domain string
}
// Credentials contains information about client credentials
type Credentials struct {
CredentialsFileLocation string
}
// GeneralOptions contains information for general configuration options
type GeneralOptions struct {
DelayBetweenMassages int64
}