Initial commit: Open sourcing all of the Maple Open Technologies code.

This commit is contained in:
Bartlomiej Mika 2025-12-02 14:33:08 -05:00
commit 755d54a99d
2010 changed files with 448675 additions and 0 deletions

View file

@ -0,0 +1,118 @@
package daemon
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/app"
"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/config"
"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/pkg/storage/database"
)
// createBootstrapLogger creates a simple console logger for use during application bootstrap
// This is used before the main application logger is initialized
func createBootstrapLogger() *zap.Logger {
encoderConfig := zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "",
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderConfig),
zapcore.AddSync(os.Stdout),
zapcore.InfoLevel,
)
return zap.New(core)
}
// DaemonCmd returns the daemon command
func DaemonCmd() *cobra.Command {
var noAutoMigrate bool
cmd := &cobra.Command{
Use: "daemon",
Short: "Start the MaplePress backend server",
Long: `Start the MaplePress backend server.
By default, the server will automatically run database migrations on startup.
This ensures the database schema is always up-to-date with the application code.
For cloud-native deployments (Kubernetes, Docker, etc.), this is the recommended approach.
To disable auto-migration, use the --no-auto-migrate flag.`,
RunE: func(cmd *cobra.Command, args []string) error {
// Create bootstrap logger for startup messages
logger := createBootstrapLogger()
defer logger.Sync()
// Load configuration
cfg, err := config.Load()
if err != nil {
return err
}
// Run migrations automatically (unless disabled)
if !noAutoMigrate {
logger.Info("⏳ Running database migrations...")
migrator := database.NewMigrator(cfg, logger)
if err := migrator.Up(); err != nil {
return fmt.Errorf("failed to run migrations: %w", err)
}
logger.Info("✓ Database migrations completed successfully")
} else {
logger.Warn("⚠️ Auto-migration disabled, skipping database migrations")
}
// Initialize application via Wire
logger.Info("⏳ Initializing application dependencies...")
application, err := app.InitializeApplication(cfg)
if err != nil {
return err
}
logger.Info("✓ Application dependencies initialized")
logger.Info("")
// Start server
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Handle graceful shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
errChan := make(chan error, 1)
go func() {
errChan <- application.Run(ctx)
}()
select {
case err := <-errChan:
return err
case <-sigChan:
return application.Shutdown(ctx)
}
},
}
// Add flags
cmd.Flags().BoolVar(&noAutoMigrate, "no-auto-migrate", false, "Disable automatic database migrations on startup")
return cmd
}

View file

@ -0,0 +1,138 @@
package migrate
import (
"fmt"
"github.com/spf13/cobra"
"go.uber.org/zap"
"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/config"
"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/pkg/storage/database"
)
// MigrateCmd returns the migrate command with subcommands
func MigrateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "migrate",
Short: "Database migration commands",
}
cmd.AddCommand(upCmd())
cmd.AddCommand(downCmd())
cmd.AddCommand(versionCmd())
cmd.AddCommand(forceCmd())
return cmd
}
// upCmd runs pending migrations
func upCmd() *cobra.Command {
return &cobra.Command{
Use: "up",
Short: "Run all pending migrations",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
// Create simple logger for CLI
logger := zap.NewNop() // Silent logger for migrate commands
migrator := database.NewMigrator(cfg, logger)
if err := migrator.Up(); err != nil {
return fmt.Errorf("failed to run migrations: %w", err)
}
return nil
},
}
}
// downCmd rolls back the last migration
func downCmd() *cobra.Command {
return &cobra.Command{
Use: "down",
Short: "Rollback the last migration",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
// Create console logger for CLI output
logger, _ := zap.NewDevelopment()
defer logger.Sync()
migrator := database.NewMigrator(cfg, logger)
if err := migrator.Down(); err != nil {
return fmt.Errorf("failed to rollback migration: %w", err)
}
logger.Info("Successfully rolled back last migration")
return nil
},
}
}
// versionCmd shows the current migration version
func versionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Show current migration version",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
// Create console logger for CLI output
logger, _ := zap.NewDevelopment()
defer logger.Sync()
migrator := database.NewMigrator(cfg, logger)
version, dirty, err := migrator.Version()
if err != nil {
return fmt.Errorf("failed to get version: %w", err)
}
if dirty {
logger.Warn("Current migration version is DIRTY - requires manual intervention",
zap.Uint("version", uint(version)))
} else {
logger.Info("Current migration version",
zap.Uint("version", uint(version)))
}
return nil
},
}
}
// forceCmd forces a specific migration version
func forceCmd() *cobra.Command {
var version int
cmd := &cobra.Command{
Use: "force",
Short: "Force database to a specific migration version (use with caution)",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
logger := zap.NewNop()
migrator := database.NewMigrator(cfg, logger)
if err := migrator.ForceVersion(version); err != nil {
return fmt.Errorf("failed to force version: %w", err)
}
return nil
},
}
cmd.Flags().IntVarP(&version, "version", "v", 0, "Migration version to force")
cmd.MarkFlagRequired("version")
return cmd
}

View file

@ -0,0 +1,30 @@
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/cmd/daemon"
"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/cmd/migrate"
"codeberg.org/mapleopentech/monorepo/cloud/maplepress-backend/cmd/version"
)
var rootCmd = &cobra.Command{
Use: "maplepress-backend",
Short: "MaplePress Backend Service",
Long: `MaplePress Backend - Clean Architecture with Wire DI and Cassandra multi-tenancy`,
}
// Execute runs the root command
func Execute() {
rootCmd.AddCommand(daemon.DaemonCmd())
rootCmd.AddCommand(migrate.MigrateCmd())
rootCmd.AddCommand(version.VersionCmd())
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

View file

@ -0,0 +1,25 @@
package version
import (
"github.com/spf13/cobra"
"go.uber.org/zap"
)
const (
Version = "0.1.0"
)
// VersionCmd returns the version command
func VersionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print the version number",
Run: func(cmd *cobra.Command, args []string) {
// Create console logger for CLI output
logger, _ := zap.NewDevelopment()
defer logger.Sync()
logger.Info("MaplePress Backend", zap.String("version", Version))
},
}
}