Implements complete offline-first architecture with SQLite caching and MariaDB synchronization. Key features: - Local SQLite database for offline operation (data/quoteforge.db) - Connection settings with encrypted credentials - Component and pricelist caching with auto-sync - Sync API endpoints (/api/sync/status, /components, /pricelists, /all) - Real-time sync status indicator in UI with auto-refresh - Offline mode detection middleware - Migration tool for database initialization - Setup wizard for initial configuration New components: - internal/localdb: SQLite repository layer (components, pricelists, sync) - internal/services/sync: Synchronization service - internal/handlers/sync: Sync API handlers - internal/handlers/setup: Setup wizard handlers - internal/middleware/offline: Offline detection - cmd/migrate: Database migration tool UI improvements: - Setup page for database configuration - Sync status indicator with online/offline detection - Warning icons for pending synchronization - Auto-refresh every 30 seconds Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
44 lines
956 B
Go
44 lines
956 B
Go
package middleware
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"git.mchus.pro/mchus/quoteforge/internal/localdb"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// OfflineDetector creates middleware that detects offline mode
|
|
// Sets context values:
|
|
// - "is_offline" (bool) - true if MariaDB is unavailable
|
|
// - "localdb" (*localdb.LocalDB) - local database instance for fallback
|
|
func OfflineDetector(mariaDB *gorm.DB, local *localdb.LocalDB) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
isOffline := !checkMariaDBOnline(mariaDB)
|
|
|
|
// Set context values for handlers
|
|
c.Set("is_offline", isOffline)
|
|
c.Set("localdb", local)
|
|
|
|
if isOffline {
|
|
slog.Debug("offline mode detected - MariaDB unavailable")
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// checkMariaDBOnline checks if MariaDB is accessible
|
|
func checkMariaDBOnline(mariaDB *gorm.DB) bool {
|
|
sqlDB, err := mariaDB.DB()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
if err := sqlDB.Ping(); err != nil {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|