Skip to content

Commit

Permalink
Created reading env logic
Browse files Browse the repository at this point in the history
  • Loading branch information
burkaydurdu committed Mar 5, 2022
1 parent 0dd998b commit 57b7c7a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
28 changes: 26 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"fmt"
"os"
"strconv"
"time"
)

Expand Down Expand Up @@ -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
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion net.http
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Health Request
GET http://localhost:6161/api/health
GET http://localhost:6161/api/v1/health

0 comments on commit 57b7c7a

Please sign in to comment.