member-console/cmd/start.go

72 lines
2.2 KiB
Go

package cmd
import (
"context"
"log/slog"
"git.coopcloud.tech/wiki-cafe/member-console/internal/logging"
"git.coopcloud.tech/wiki-cafe/member-console/internal/server"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var startCmd = &cobra.Command{
Use: "start",
Short: "Start serving the member-console web application",
Long: `The start command starts an HTTP server that serves the member-console web
application from the components directory in the current directory.
The server listens on port 8080 by default, unless a different port is specified using the --port flag.`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
// Create base context for the application
ctx := context.Background()
// Retrieve the configuration values from Viper
port := viper.GetString("port")
env := viper.GetString("env")
csrfSecret := viper.GetString("csrf-secret")
// Set up structured logging
logger := logging.SetupLogger(env)
// Store logger in context
ctx = logging.WithContext(ctx, logger)
// Create server config
serverConfig := server.Config{
Port: port,
Env: env,
CSRFSecret: csrfSecret,
Logger: logger,
}
// Start the server
if err := server.Start(ctx, serverConfig); err != nil {
logger.Error("server failed to start", slog.Any("error", err))
}
},
}
func init() {
// Register flags with Cobra
startCmd.Flags().StringP("port", "p", "", "Port to listen on")
startCmd.Flags().String("client-id", "", "OIDC Client ID")
startCmd.Flags().String("client-secret", "", "OIDC Client Secret")
startCmd.Flags().String("issuer-url", "", "Identity Provider Issuer URL")
startCmd.Flags().String("hostname", "", "Address at which the server is exposed")
startCmd.Flags().String("session-secret", "", "Session encryption secret")
startCmd.Flags().String("csrf-secret", "", "Secret key for CSRF protection (must be exactly 32 bytes)")
startCmd.Flags().String("env", "", "Environment (development/production)")
// Bind all flags to Viper
viper.BindPFlags(startCmd.Flags())
// Set default values
viper.SetDefault("port", "8080")
viper.SetDefault("env", "development")
// Add the command to the root command
rootCmd.AddCommand(startCmd)
}