From 57b7c7ab260bd7d24a700c9468f1a92a5777e388 Mon Sep 17 00:00:00 2001 From: Burkay Durdu Date: Sun, 6 Mar 2022 02:30:07 +0300 Subject: [PATCH] Created reading env logic --- config/config.go | 28 ++++++++++++++++++++++++++-- net.http | 2 +- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 0b7b270..208ebd5 100644 --- a/config/config.go +++ b/config/config.go @@ -2,6 +2,8 @@ package config import ( "fmt" + "os" + "strconv" "time" ) @@ -34,14 +36,14 @@ type Config struct { func New() (*Config, error) { config := &Config{} - config.AppName = AppName + config.AppName = shortlyViberString("NAME", AppName) config.IsDebug = IsDebug config.LengthOfCode = 6 config.DurationOfWriteToDisk = time.Second * 2 config.MemoryPath = ".mem" config.MemoryFileName = "shortly" config.Server = ServerConfig{ - Port: Port, + Port: shortlyViberInt("PORT", Port), } return config, nil @@ -50,3 +52,25 @@ func New() (*Config, error) { func (c *Config) Print() { fmt.Printf("%+v\n", c) // [TODO] should improve in here } + +func shortlyViberString(envKey, defaultKey string) string { + envValue := os.Getenv(envKey) + + if len(envValue) == 0 { + return defaultKey + } + + return envValue +} + +func shortlyViberInt(envKey string, defaultValue int) int { + envValue := os.Getenv(envKey) + + envIntegerValue, err := strconv.ParseInt(envValue, 10, 64) + + if err != nil { + return defaultValue + } + + return int(envIntegerValue) +} diff --git a/net.http b/net.http index 2d9a64f..a4ddac7 100644 --- a/net.http +++ b/net.http @@ -1,2 +1,2 @@ # Health Request -GET http://localhost:6161/api/health \ No newline at end of file +GET http://localhost:6161/api/v1/health \ No newline at end of file