From 827b8231a69f76f41202e7e87e3e792edaa4791d Mon Sep 17 00:00:00 2001 From: kandrusyak Date: Fri, 30 Jun 2023 17:14:01 +0300 Subject: [PATCH] add config --- config/config.go | 31 +++++++++++++++++++++++++++++++ main.go | 3 ++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 config/config.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..3e94962 --- /dev/null +++ b/config/config.go @@ -0,0 +1,31 @@ +package config + +import ( + "os" +) + +type ServerConfig struct { + Port string +} + +type Config struct { + Server ServerConfig +} + +func New() *Config { + return &Config{ + Server: ServerConfig{ + Port: getEnv("PORT", ""), + }, + } +} + +func getEnv(key string, defaultVal string) string { + if value, exists := os.LookupEnv(key); exists { + return value + } + + return defaultVal +} + +var Env = New() diff --git a/main.go b/main.go index bdb1625..c05f0e8 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "astra-users/config" "astra-users/handler" "astra-users/model" "github.com/labstack/echo/v4" @@ -48,5 +49,5 @@ func main() { e.DELETE("/:id", h.DeleteUser) e.GET("/health", h.Health) - e.Logger.Fatal(e.Start(":8081")) + e.Logger.Fatal(e.Start(":" + config.Env.Server.Port)) }