78 lines
2.3 KiB
Go
78 lines
2.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"git.coopcloud.tech/wiki-cafe/member-console/internal/auth"
|
|
"git.coopcloud.tech/wiki-cafe/member-console/internal/middleware"
|
|
"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) {
|
|
// Retrieve the port value from Viper
|
|
port := viper.GetString("port")
|
|
|
|
// Create a new HTTP request router
|
|
httpRequestRouter := http.NewServeMux()
|
|
|
|
// Set up authentication
|
|
authConfig, err := auth.Setup()
|
|
if err != nil {
|
|
log.Fatalf("Failed to set up authentication: %v", err)
|
|
}
|
|
|
|
// Register auth handlers
|
|
authConfig.RegisterHandlers(httpRequestRouter)
|
|
|
|
// Create middleware stack
|
|
stack := middleware.CreateStack(
|
|
middleware.SecureHeaders,
|
|
middleware.Logging,
|
|
middleware.MaxBodySize(1024*1024), // 1MB size limit
|
|
authConfig.Middleware(),
|
|
)
|
|
|
|
// Create HTTP server
|
|
server := http.Server{
|
|
Addr: ":" + port,
|
|
Handler: stack(httpRequestRouter),
|
|
}
|
|
|
|
// Serve the components directory
|
|
httpRequestRouter.Handle("/", http.FileServer(http.Dir("./components")))
|
|
log.Println("Starting server on port", port)
|
|
log.Fatal(server.ListenAndServe())
|
|
},
|
|
}
|
|
|
|
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("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)
|
|
}
|