base creation

This commit is contained in:
m
2026-09-03 20:38:47 +02:00
parent 32a61e6a64
commit 80dd164fde
12 changed files with 187 additions and 18 deletions
+55
View File
@@ -0,0 +1,55 @@
package config
import (
"fmt"
"log"
"os"
"strconv"
"github.com/joho/godotenv"
)
type ApplicationConfig struct {
Port int64
}
func LoadApplicationConfig() (error, *ApplicationConfig) {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
err, portString := getVar("PORT", "8080", true)
if err != nil {
return err, nil
}
port, err := strconv.ParseInt(portString, 10, 64)
if err != nil {
return err, nil
}
return nil, &ApplicationConfig{
Port: port,
}
}
func getVar(varName string, defaultValue string, isRequired bool) (error, string) {
varValue := os.Getenv(varName)
if varValue == "" {
if isRequired {
return fmt.Errorf("%s is missing and required : ", varName), ""
}
return nil, defaultValue
}
return nil, varValue
}