Skip to content

Commit

Permalink
feat: Add config loading from env with default option
Browse files Browse the repository at this point in the history
* refactor: Move filepath creation to yaml redirect

* feat: Add config helper
  • Loading branch information
mehmetumit committed Nov 7, 2023
1 parent ec9cffe commit 44355c7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 11 deletions.
30 changes: 20 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/mehmetumit/dexus/helper"
"github.com/mehmetumit/dexus/internal/adapters/fileredirect"
"github.com/mehmetumit/dexus/internal/adapters/memcache"
"github.com/mehmetumit/dexus/internal/adapters/rest"
Expand All @@ -20,13 +20,23 @@ var (

// Composition root
func main() {
//TODO env
debugLevel := true
logger := stdlog.NewStdLog(debugLevel)
// Load with these defaults if envs are not set
envCfg := helper.LoadConfigWithDefaults(helper.Config{
DebugLevel: true,
Host: "",
Port: "8080",
ReadTimeoutMS: 1000 * time.Millisecond,
WriteTimeoutMS: 1000 * time.Millisecond,
CacheTTLSec: 60 * time.Second,
YamlRedirectPath: "configs/redirection.yaml",
})

logger := stdlog.NewStdLog(envCfg.DebugLevel)
logger.Info(fmt.Sprintf("Version: %s | Commit: %s", Version, Commit))

cacher := memcache.NewMemCache(logger)
fPath := filepath.FromSlash("configs/redirection.yaml")
redirectRepo, err := fileredirect.NewYamlRedirect(logger, fPath)

redirectRepo, err := fileredirect.NewYamlRedirect(logger, envCfg.YamlRedirectPath)
if err != nil {
os.Exit(1)
}
Expand All @@ -35,9 +45,9 @@ func main() {
RedirectionRepo: redirectRepo,
})
rest.NewServer(logger, &rest.ServerConfig{
Addr: ":8080",
Addr: envCfg.Host + ":" + envCfg.Port,
//IdleTimeout: 90 * time.Second,
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
}, appCore, rest.WithCacher(cacher, logger, 5*time.Second))
ReadTimeout: envCfg.ReadTimeoutMS,
WriteTimeout: envCfg.WriteTimeoutMS,
}, appCore, rest.WithCacher(cacher, logger, envCfg.CacheTTLSec))
}
62 changes: 62 additions & 0 deletions helper/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package helper

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

type Config struct {
DebugLevel bool
Host string
Port string
ReadTimeoutMS time.Duration
WriteTimeoutMS time.Duration
CacheTTLSec time.Duration
YamlRedirectPath string
}
type EnvConfig struct {
Config
}

func LoadConfigWithDefaults(cfg Config) *EnvConfig {
c := Config{}
dl, err := strconv.ParseBool(os.Getenv("DEBUG_LEVEL"))
c.DebugLevel = dl
if err != nil {
c.DebugLevel = cfg.DebugLevel
}
h := os.Getenv("HOST")
c.Host = h
if h == "" {
c.Host = cfg.Host
}
p := os.Getenv("PORT")
c.Port = p
if p == "" {
c.Port = cfg.Port
}
rt, err := strconv.ParseInt(os.Getenv("READ_TIMEOUT_MS"), 10, 64)
c.ReadTimeoutMS = time.Duration(rt) * time.Millisecond
if err != nil {
c.ReadTimeoutMS = cfg.ReadTimeoutMS
}
wt, err := strconv.ParseInt(os.Getenv("WRITE_TIMEOUT_MS"), 10, 64)
c.WriteTimeoutMS = time.Duration(wt) * time.Millisecond
if err != nil {
c.WriteTimeoutMS = cfg.WriteTimeoutMS
}
ttl, err := strconv.ParseInt(os.Getenv("CACHE_TTL_SEC"), 10, 64)
c.CacheTTLSec = time.Duration(ttl) * time.Second
if err != nil {
c.CacheTTLSec = cfg.CacheTTLSec
}
fp := os.Getenv("YAML_REDIRECT_PATH")
if fp == "" {
c.YamlRedirectPath = cfg.YamlRedirectPath
}
return &EnvConfig{
c,
}

}
3 changes: 2 additions & 1 deletion internal/adapters/fileredirect/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"os"
"path/filepath"
"sync"

"github.com/mehmetumit/dexus/internal/core/ports"
Expand All @@ -26,7 +27,7 @@ type YamlRedirect struct {
}

func initStore(logger ports.Logger, filePath string) (*RedirectionStore, error) {
f, err := os.Open(filePath)
f, err := os.Open(filepath.FromSlash(filePath))
defer func() {
err := f.Close()
if err != nil{
Expand Down

0 comments on commit 44355c7

Please sign in to comment.