chore: refactor serve command
All checks were successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/deploy Pipeline was successful

This commit is contained in:
Melora Hugues 2023-10-29 13:32:09 +01:00
parent 69a07ce076
commit 9d2d49425d
3 changed files with 18 additions and 22 deletions

View file

@ -1,4 +1,4 @@
package cmd
package serve
import (
"context"
@ -6,12 +6,12 @@ import (
"os/signal"
"time"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/config"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/connector"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/server"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/services"
"github.com/dexidp/dex/connector/oidc"
dex_server "github.com/dexidp/dex/server"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cobra"
@ -35,18 +35,11 @@ variables`,
func serve() {
mainCtx, cancel := context.WithCancel(context.Background())
conf, err := config.New(configPath)
if err != nil {
panic(err)
}
conf := utils.InitConfig(configPath)
logger.Init(conf.LogLevel)
logger.L.Infof("Initialized logger with level %v", conf.LogLevel)
storageType, err := services.InitStorage(conf)
if err != nil {
logger.L.Fatalf("Failed to initialize storage backend: %s", err.Error())
}
storageType := utils.InitStorage(conf)
logger.L.Infof("Initialized storage backend %q", conf.StorageType)
dexConf := dex_server.Config{
Web: dex_server.WebConfig{
@ -64,7 +57,7 @@ func serve() {
logger.L.Info("Initializing authentication backends")
dex_server.ConnectorsConfig["refuseAll"] = func() dex_server.ConnectorConfig { return new(connector.RefuseAllConfig) }
dex_server.ConnectorsConfig[connector.TypeRefuseAll] = func() dex_server.ConnectorConfig { return new(connector.RefuseAllConfig) }
connectors, err := dexConf.Storage.ListConnectors()
if err != nil {
logger.L.Fatalf("Failed to get existing connectors: %s", err.Error())
@ -74,14 +67,7 @@ func serve() {
connectorIDs = append(connectorIDs, conn.ID)
}
backend := config.BackendConfig{
Config: &oidc.Config{},
Name: "RefuseAll",
ID: "null",
Type: "refuseAll",
}
if err := services.CreateConnector(&backend, &dexConf, connectorIDs); err != nil {
if err := services.AddDefaultBackend(storageType); err != nil {
logger.L.Errorf("Failed to add connector for backend RefuseAll to stage: %s", err.Error())
}
@ -140,7 +126,7 @@ func serve() {
}
func init() {
rootCmd.AddCommand(serveCmd)
cmd.RootCmd.AddCommand(serveCmd)
// Here you will define your flags and configuration settings.

View file

@ -3,6 +3,7 @@ package main
import (
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd"
_ "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/backend"
_ "git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/serve"
)
func main() {

View file

@ -1,9 +1,11 @@
package services
import (
"errors"
"fmt"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/config"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/connector"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
"github.com/dexidp/dex/storage"
"github.com/dexidp/dex/storage/memory"
@ -29,3 +31,10 @@ func InitStorage(conf *config.AppConfig) (storage.Storage, error) {
}
return storageType, nil
}
func AddDefaultBackend(s storage.Storage) error {
if err := s.CreateConnector(connector.RefuseAllConnectorConfig); err != nil && !errors.Is(err, storage.ErrAlreadyExists) {
return fmt.Errorf("failed to add default backend: %w", err)
}
return nil
}