Initial commit

This commit is contained in:
Fabio Scotto di Santolo
2025-05-31 22:04:55 +02:00
parent 15acea9120
commit 1a5d437311
17 changed files with 392 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
{
"server": {
"host": "localhost",
"port": 8080
}
}

6
config/application.json Normal file
View File

@@ -0,0 +1,6 @@
{
"server": {
"host": "localhost",
"port": 8089
}
}

49
config/config.go Normal file
View File

@@ -0,0 +1,49 @@
package config
import (
"encoding/json"
"fmt"
"log"
"os"
)
type Application struct {
Server Server `json:"server"`
}
type Server struct {
Host string `json:"host"`
Port int `json:"port"`
}
var App Application
const configDir = "config"
func Initialize(profile string) error {
filename := checkProfileAndGetFilePath(profile)
content, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("error reading file: %v", err)
}
if err = json.Unmarshal(content, &App); err != nil {
return fmt.Errorf("error unmarshaling JSON: %v", err)
}
return nil
}
func checkProfileAndGetFilePath(profile string) string {
var filename string
switch {
case profile == "dev":
filename = fmt.Sprintf("%s/application-dev.json", configDir)
case profile == "test":
filename = fmt.Sprintf("%s/application-test.json", configDir)
case profile == "prod":
filename = fmt.Sprintf("%s/application.json", configDir)
default:
log.Fatalf("Profile %s is not valid value", profile)
}
return filename
}