From d9c2fcd61acfe5df438a15c5c570310b14967ab9 Mon Sep 17 00:00:00 2001 From: chapeau Date: Sat, 21 Oct 2023 23:50:53 +0200 Subject: [PATCH] feat #4: add storage config in config file --- polyculeconnect/config/config.go | 30 ++++++++++++++++++++++++++---- polyculeconnect/main.go | 19 ++++++++++++++++++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/polyculeconnect/config/config.go b/polyculeconnect/config/config.go index a44f379..126f041 100644 --- a/polyculeconnect/config/config.go +++ b/polyculeconnect/config/config.go @@ -54,6 +54,19 @@ type OpenConnectConfig struct { Issuer string `json:"issuer"` } +type StorageConfig struct { + File string `json:"file"` + Host string `json:"host"` + Port int `json:"port"` + Database string `json:"database"` + User string `json:"user"` + Password string `json:"password"` + Ssl struct { + Mode string `json:"mode"` + CaFile string `json:"caFile"` + } `json:"ssl"` +} + type jsonConf struct { Log struct { Level string `json:"level"` @@ -64,6 +77,10 @@ type jsonConf struct { Mode string `json:"mode"` SockPath string `json:"sock"` } `json:"server"` + Storage struct { + StorageType string `json:"type"` + Config *StorageConfig `json:"config"` + } `json:"storage"` OpenConnectConfig *OpenConnectConfig `json:"openconnect"` } @@ -73,6 +90,8 @@ type AppConfig struct { Host string Port int SockPath string + StorageType string + StorageConfig *StorageConfig OpenConnectConfig *OpenConnectConfig } @@ -102,14 +121,17 @@ func (ac *AppConfig) UnmarshalJSON(data []byte) error { ac.Host = jsonConf.Server.Host ac.Port = jsonConf.Server.Port ac.OpenConnectConfig = jsonConf.OpenConnectConfig + ac.StorageType = jsonConf.Storage.StorageType + ac.StorageConfig = jsonConf.Storage.Config return nil } var defaultConfig AppConfig = AppConfig{ - LogLevel: logrus.InfoLevel, - ServerMode: ModeNet, - Host: "0.0.0.0", - Port: 5000, + LogLevel: logrus.InfoLevel, + ServerMode: ModeNet, + Host: "0.0.0.0", + Port: 5000, + StorageType: "memory", } func New(filepath string) (*AppConfig, error) { diff --git a/polyculeconnect/main.go b/polyculeconnect/main.go index 20953ad..87e3271 100644 --- a/polyculeconnect/main.go +++ b/polyculeconnect/main.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "flag" + "fmt" "os" "os/signal" "time" @@ -34,6 +35,17 @@ func parseArgs() *cliArgs { } } +func initStorage(conf *config.AppConfig) (storage.Storage, error) { + var storageType storage.Storage + switch conf.StorageType { + case "memory": + storageType = memory.New(logger.L) + default: + return storageType, fmt.Errorf("Unsupported storage backend type: %s", conf.StorageType) + } + return storageType, nil +} + func main() { args := parseArgs() @@ -47,12 +59,17 @@ func main() { logger.Init(conf.LogLevel) logger.L.Infof("Initialized logger with level %v", conf.LogLevel) + storageType, err := initStorage(conf) + if err != nil { + logger.L.Errorf("Failed to initialize storage backend: %s", err.Error()) + } + logger.L.Infof("Initialized storage backend \"%s\"", conf.StorageType) dexConf := dex_server.Config{ Web: dex_server.WebConfig{ Dir: "./", Theme: "default", }, - Storage: memory.New(logger.L), + Storage: storageType, Issuer: conf.OpenConnectConfig.Issuer, SupportedResponseTypes: []string{"code"}, SkipApprovalScreen: false,