Implement database integration with SQLite and refactor user handling for OIDC authentication

This commit is contained in:
2025-06-02 19:35:11 -05:00
parent 77b2e6c24e
commit a7d3822f94
11 changed files with 206 additions and 76 deletions

View File

@ -6,6 +6,7 @@ import (
"os"
"strings"
"git.coopcloud.tech/wiki-cafe/member-console/internal/db"
"git.coopcloud.tech/wiki-cafe/member-console/internal/logging"
"git.coopcloud.tech/wiki-cafe/member-console/internal/server"
"github.com/spf13/cobra"
@ -31,6 +32,17 @@ var startCmd = &cobra.Command{
// Store logger in context
ctx = logging.WithContext(ctx, logger)
// Database Setup
dbDSN := viper.GetString("db-dsn")
database, err := db.NewDB(ctx, logger, dbDSN)
if err != nil {
logger.Error("failed to initialize database", slog.Any("error", err))
os.Exit(1)
}
defer database.Close()
// You'll pass 'database' (or your sqlc Querier) to your server/handlers.
// For example, by adding it to the server.Config
// Validate and load configurations from files if specified
configPairs := []struct {
value string
@ -76,6 +88,7 @@ var startCmd = &cobra.Command{
Env: env,
CSRFSecret: csrfSecret,
Logger: logger,
DB: db.New(database), // Pass the sqlc Querier
}
// Start the server
@ -100,6 +113,7 @@ func init() {
startCmd.Flags().String("session-secret-file", "", "Path to file containing session secret key")
startCmd.Flags().String("csrf-secret", "", "Secret key for CSRF protection (must be exactly 32 bytes)")
startCmd.Flags().String("csrf-secret-file", "", "Path to file containing CSRF secret key")
startCmd.Flags().String("db-dsn", "", "Database DSN (e.g., ./member_console.db or file:/path/to/data.db?_foreign_keys=on)")
// Bind all flags to Viper
viper.BindPFlags(startCmd.Flags())
@ -107,6 +121,7 @@ func init() {
// Set default values
viper.SetDefault("port", "8080")
viper.SetDefault("env", "development")
viper.SetDefault("db-dsn", "./member_console.db")
// Add the command to the root command
rootCmd.AddCommand(startCmd)