polycule-connect/polyculeconnect/cmd/serve/serve.go
Melora Hugues 8d805cefe6
Some checks failed
/ docker-build-only (push) Failing after 27s
/ go-test (push) Failing after 1m18s
Cleanup DB a bit, and start correctly handling users (#42)
2024-10-18 22:06:05 +02:00

125 lines
3.4 KiB
Go

package serve
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"log/slog"
"os"
"os/signal"
"time"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/client"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/middlewares"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/model"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/storage"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/logger"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/server"
"github.com/go-jose/go-jose/v4"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/zitadel/oidc/v3/pkg/op"
"go.uber.org/zap/exp/zapslog"
)
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())
conf := utils.InitConfig(configPath)
logger.Init(conf.LogLevel)
logger.L.Infof("Initialized logger with level %v", conf.LogLevel)
userDB, err := db.New(*conf)
if err != nil {
utils.Failf("failed to init user DB: %s", err.Error())
}
backends := map[uuid.UUID]*client.OIDCClient{}
key := sha256.Sum256([]byte("test"))
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
utils.Failf("Failed to generate private key: %s", err)
}
signingKey := model.Key{
PrivateKey: privateKey,
KeyID: uuid.New(),
SigningAlg: jose.RS256,
}
st := storage.Storage{LocalStorage: userDB, InitializedBackends: backends, Key: &signingKey}
opConf := op.Config{
CryptoKey: key,
CodeMethodS256: false,
}
slogger := slog.New(zapslog.NewHandler(logger.L.Desugar().Core(), nil))
// slogger :=
options := []op.Option{
op.WithAllowInsecure(),
op.WithLogger(slogger),
op.WithHttpInterceptors(middlewares.WithBackendFromRequestMiddleware),
}
provider, err := op.NewProvider(&opConf, &st, op.StaticIssuer(conf.Issuer), options...)
if err != nil {
utils.Failf("failed to init OIDC provider: %s", err.Error())
}
logger.L.Info("Initializing server")
s, err := server.New(conf, provider, &st, 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() {
cmd.RootCmd.AddCommand(serveCmd)
serveCmd.Flags().StringVarP(&configPath, "config", "c", "config.json", "Path to the JSON configuration file")
}