Skip to content

Commit

Permalink
feat: allow config of timeout from configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rsdmike committed Oct 30, 2024
1 parent ad5aa5b commit e08af2a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 22 deletions.
67 changes: 50 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package config

import (
"errors"
"os"
"path/filepath"
"time"

"github.com/ilyakaznacheev/cleanenv"
"gopkg.in/yaml.v2"
)

var ConsoleConfig *Config
Expand All @@ -18,14 +24,16 @@ type (

// App -.
App struct {
Name string `env-required:"true" yaml:"name" env:"APP_NAME"`
Repo string `env-required:"true" yaml:"repo" env:"APP_REPO"`
Version string `env-required:"true"`
EncryptionKey string `yaml:"encryption_key" env:"APP_ENCRYPTION_KEY"`
JWTKey string `env-required:"true" yaml:"jwtKey" env:"APP_JWT_KEY"`
AuthDisabled bool `yaml:"authDisabled" env:"APP_AUTH_DISABLED"`
AdminUsername string `yaml:"adminUsername" env:"APP_ADMIN_USERNAME"`
AdminPassword string `yaml:"adminPassword" env:"APP_ADMIN_PASSWORD"`
Name string `env-required:"true" yaml:"name" env:"APP_NAME"`
Repo string `env-required:"true" yaml:"repo" env:"APP_REPO"`
Version string `env-required:"true"`
EncryptionKey string `yaml:"encryption_key" env:"APP_ENCRYPTION_KEY"`
JWTKey string `env-required:"true" yaml:"jwtKey" env:"APP_JWT_KEY"`
AuthDisabled bool `yaml:"authDisabled" env:"APP_AUTH_DISABLED"`
AdminUsername string `yaml:"adminUsername" env:"APP_ADMIN_USERNAME"`
AdminPassword string `yaml:"adminPassword" env:"APP_ADMIN_PASSWORD"`
JWTExpiration time.Duration `yaml:"jwtExpiration" env:"APP_JWT_EXPIRATION"`
RedirectionJWTExpiration time.Duration `yaml:"redirectionJWTExpiration" env:"APP_REDIRECTION_JWT_EXPIRATION"`
}

// HTTP -.
Expand Down Expand Up @@ -60,13 +68,15 @@ func NewConfig() (*Config, error) {
// set defaults
ConsoleConfig = &Config{
App: App{
Name: "console",
Repo: "open-amt-cloud-toolkit/console",
Version: "DEVELOPMENT",
EncryptionKey: "",
JWTKey: "your_secret_jwt_key",
AdminUsername: "standalone",
AdminPassword: "G@ppm0ym",
Name: "console",
Repo: "open-amt-cloud-toolkit/console",
Version: "DEVELOPMENT",
EncryptionKey: "",
JWTKey: "your_secret_jwt_key",
AdminUsername: "standalone",
AdminPassword: "G@ppm0ym",
JWTExpiration: 24 * time.Hour,
RedirectionJWTExpiration: 5 * time.Minute,
},
HTTP: HTTP{
Host: "localhost",
Expand All @@ -87,10 +97,33 @@ func NewConfig() (*Config, error) {
},
}

_ = cleanenv.ReadConfig("./config/config.yml", ConsoleConfig)
err := cleanenv.ReadConfig("./config/config.yml", ConsoleConfig)

var pathErr *os.PathError
if errors.As(err, &pathErr) {
// Write config file out to disk
configDir := filepath.Dir("./config/config.yml")
if err := os.MkdirAll(configDir, os.ModePerm); err != nil {
return nil, err
}

file, err := os.Create("./config/config.yml")
if err != nil {
return nil, err
}
defer file.Close()

encoder := yaml.NewEncoder(file)
defer encoder.Close()

if err := encoder.Encode(ConsoleConfig); err != nil {
return nil, err
}
}

// its ok to ignore the error here, as we have default values set if the config file is not found

err := cleanenv.ReadEnv(ConsoleConfig)
err = cleanenv.ReadEnv(ConsoleConfig)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ app:
jwtKey: "your_secret_jwt_key"
adminUsername: "standalone"
adminPassword: "G@ppm0ym"
jwtExpiration: 24h
redirectionJWTExpiration: 5m
http:
host: "localhost"
port: "8181"
Expand Down
4 changes: 1 addition & 3 deletions internal/controller/http/v1/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (

var ErrLogin = consoleerrors.CreateConsoleError("LoginHandler")

const hoursInADay = 24

type LoginRoute struct {
Config *config.Config
}
Expand Down Expand Up @@ -45,7 +43,7 @@ func (lr LoginRoute) Login(c *gin.Context) {
}

// Create JWT token
expirationTime := time.Now().Add(hoursInADay * time.Hour)
expirationTime := time.Now().Add(config.ConsoleConfig.JWTExpiration)
claims := jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expirationTime),
}
Expand Down
4 changes: 2 additions & 2 deletions internal/usecase/profiles/usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,9 +822,9 @@ func TestBuildConfigurationObject(t *testing.T) {
t.Parallel()

originalConfig := local.ConsoleConfig
defer func() {
t.Cleanup(func() {
local.ConsoleConfig = originalConfig
}()
})

local.ConsoleConfig = &local.Config{
EA: local.EA{
Expand Down

0 comments on commit e08af2a

Please sign in to comment.