2023-10-29 12:32:09 +00:00
|
|
|
package serve
|
2023-10-28 12:58:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"time"
|
|
|
|
|
2023-10-29 12:32:09 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd"
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
|
2023-10-28 12:58:06 +00:00
|
|
|
"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"
|
|
|
|
dex_server "github.com/dexidp/dex/server"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var configPath string
|
|
|
|
|
|
|
|
const stopTimeout = 10 * time.Second
|
|
|
|
|
|
|
|
// serveCmd represents the serve command
|
|
|
|
var serveCmd = &cobra.Command{
|
|
|
|
Use: "serve",
|
|
|
|
Short: "Start the web server",
|
|
|
|
Long: `Start the PolyculeConnect web server using the configuration defined through environment
|
|
|
|
variables`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
serve()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func serve() {
|
|
|
|
mainCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
2023-10-29 12:32:09 +00:00
|
|
|
conf := utils.InitConfig(configPath)
|
2023-10-28 12:58:06 +00:00
|
|
|
logger.Init(conf.LogLevel)
|
|
|
|
logger.L.Infof("Initialized logger with level %v", conf.LogLevel)
|
|
|
|
|
2023-10-29 12:32:09 +00:00
|
|
|
storageType := utils.InitStorage(conf)
|
2023-10-28 12:58:06 +00:00
|
|
|
logger.L.Infof("Initialized storage backend %q", conf.StorageType)
|
|
|
|
dexConf := dex_server.Config{
|
|
|
|
Web: dex_server.WebConfig{
|
2024-05-08 14:07:40 +00:00
|
|
|
Dir: conf.StaticDir,
|
2023-10-28 12:58:06 +00:00
|
|
|
Theme: "default",
|
|
|
|
},
|
|
|
|
Storage: storageType,
|
2024-05-28 19:47:51 +00:00
|
|
|
Issuer: conf.Issuer,
|
2023-10-28 12:58:06 +00:00
|
|
|
SupportedResponseTypes: []string{"code"},
|
|
|
|
SkipApprovalScreen: false,
|
|
|
|
AllowedOrigins: []string{"*"},
|
|
|
|
Logger: logger.L,
|
|
|
|
PrometheusRegistry: prometheus.NewRegistry(),
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.L.Info("Initializing authentication backends")
|
|
|
|
|
2023-10-29 12:32:09 +00:00
|
|
|
dex_server.ConnectorsConfig[connector.TypeRefuseAll] = func() dex_server.ConnectorConfig { return new(connector.RefuseAllConfig) }
|
2023-10-28 12:58:06 +00:00
|
|
|
connectors, err := dexConf.Storage.ListConnectors()
|
|
|
|
if err != nil {
|
|
|
|
logger.L.Fatalf("Failed to get existing connectors: %s", err.Error())
|
|
|
|
}
|
|
|
|
var connectorIDs []string
|
|
|
|
for _, conn := range connectors {
|
|
|
|
connectorIDs = append(connectorIDs, conn.ID)
|
|
|
|
}
|
|
|
|
|
2023-10-29 12:32:09 +00:00
|
|
|
if err := services.AddDefaultBackend(storageType); err != nil {
|
2023-10-28 12:58:06 +00:00
|
|
|
logger.L.Errorf("Failed to add connector for backend RefuseAll to stage: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
dexSrv, err := dex_server.NewServer(mainCtx, dexConf)
|
|
|
|
if err != nil {
|
|
|
|
logger.L.Fatalf("Failed to init dex server: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.L.Info("Initializing server")
|
|
|
|
s, err := server.New(conf, dexSrv, logger.L)
|
|
|
|
if err != nil {
|
|
|
|
logger.L.Fatalf("Failed to initialize server: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
go s.Run(mainCtx)
|
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
|
|
|
|
logger.L.Info("Application successfully started")
|
|
|
|
|
|
|
|
logger.L.Debug("Waiting for stop signal")
|
|
|
|
select {
|
|
|
|
case <-s.Done():
|
|
|
|
logger.L.Fatal("Unexpected exit from server")
|
|
|
|
case <-c:
|
|
|
|
logger.L.Info("Stopping main application")
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.L.Debugf("Waiting %v for all daemons to stop", stopTimeout)
|
|
|
|
select {
|
|
|
|
case <-time.After(stopTimeout):
|
|
|
|
logger.L.Fatalf("Failed to stop all daemons in the expected time")
|
|
|
|
case <-s.Done():
|
|
|
|
logger.L.Info("web server successfully stopped")
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.L.Info("Application successfully stopped")
|
|
|
|
os.Exit(0)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-10-29 12:32:09 +00:00
|
|
|
cmd.RootCmd.AddCommand(serveCmd)
|
2023-10-28 12:58:06 +00:00
|
|
|
serveCmd.Flags().StringVarP(&configPath, "config", "c", "config.json", "Path to the JSON configuration file")
|
|
|
|
}
|