Skip to content

Commit

Permalink
feat: allow config of timeout from configuration
Browse files Browse the repository at this point in the history
This PR also creates a config if one does not exist in ./config/ dir and
enables the user to specifiy a config file via command line.
  • Loading branch information
rsdmike committed Nov 5, 2024
1 parent 2a95938 commit e9d5135
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 24 deletions.
92 changes: 74 additions & 18 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package config

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

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

var ConsoleConfig *Config
Expand All @@ -18,14 +25,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 +69,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 +98,55 @@ func NewConfig() (*Config, error) {
},
}

_ = cleanenv.ReadConfig("./config/config.yml", ConsoleConfig)
// its ok to ignore the error here, as we have default values set if the config file is not found
// Define a command line flag for the config path
var configPathFlag string
if flag.Lookup("config") == nil {
flag.StringVar(&configPathFlag, "config", "", "path to config file")
}

flag.Parse()

// Determine the config path
var configPath string
if configPathFlag != "" {
configPath = configPathFlag
} else {
ex, err := os.Executable()
if err != nil {
panic(err)
}

exPath := filepath.Dir(ex)

configPath = filepath.Join(exPath, "config", "config.yml")
}

err := cleanenv.ReadConfig(configPath, ConsoleConfig)

var pathErr *os.PathError

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

file, err := os.Create(configPath)
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
}
}

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/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ type deviceRoutes struct {

var ErrValidationDevices = dto.NotValidError{Console: consoleerrors.CreateConsoleError("ProfileAPI")}

const fiveMinutes = 5

func NewDeviceRoutes(handler *gin.RouterGroup, t devices.Feature, l logger.Interface) {
r := &deviceRoutes{t, l}

Expand Down Expand Up @@ -99,7 +97,7 @@ func (dr *deviceRoutes) LoginRedirection(c *gin.Context) {
return
}
// Create JWT token
expirationTime := time.Now().Add(fiveMinutes * time.Minute)
expirationTime := time.Now().Add(config.ConsoleConfig.JWTExpiration)
claims := jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expirationTime),
}
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

0 comments on commit e9d5135

Please sign in to comment.