- Replace dual update systems with single markdown-first editor architecture - Add server-side upsert to eliminate 404 errors on PUT operations - Fix content persistence race condition between preview and save operations - Remove legacy updateElementContent system entirely - Add comprehensive authentication with JWT scaffolding and dev mode - Implement EditContext.updateOriginalContent() for proper baseline management - Enable markdown formatting in all text elements (h1-h6, p, div, etc) - Clean terminology: remove 'unified' references from codebase Technical changes: * core/editor.js: Remove legacy update system, unify content types as markdown * ui/Editor.js: Add updateOriginalContent() method to fix save persistence * ui/Previewer.js: Clean live preview system for all content types * api/handlers.go: Implement UpsertContent for idempotent PUT operations * auth/*: Complete authentication service with OAuth scaffolding * db/queries/content.sql: Add upsert query with ON CONFLICT handling * Schema: Remove type constraints, rely on server-side validation Result: Clean content editing with persistent saves, no 404 errors, markdown support in all text elements
152 lines
4.6 KiB
Go
152 lines
4.6 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/insertr/insertr/internal/api"
|
|
"github.com/insertr/insertr/internal/auth"
|
|
"github.com/insertr/insertr/internal/db"
|
|
)
|
|
|
|
var serveCmd = &cobra.Command{
|
|
Use: "serve",
|
|
Short: "Start the content API server",
|
|
Long: `Start the HTTP API server that provides content storage and retrieval.
|
|
Supports both development and production modes with SQLite or PostgreSQL databases.`,
|
|
Run: runServe,
|
|
}
|
|
|
|
var (
|
|
port int
|
|
devMode bool
|
|
)
|
|
|
|
func init() {
|
|
serveCmd.Flags().IntVarP(&port, "port", "p", 8080, "Server port")
|
|
serveCmd.Flags().BoolVar(&devMode, "dev-mode", false, "Enable development mode features")
|
|
|
|
// Bind flags to viper
|
|
viper.BindPFlag("server.port", serveCmd.Flags().Lookup("port"))
|
|
viper.BindPFlag("dev_mode", serveCmd.Flags().Lookup("dev-mode"))
|
|
}
|
|
|
|
func runServe(cmd *cobra.Command, args []string) {
|
|
// Get configuration values
|
|
port := viper.GetInt("server.port")
|
|
dbPath := viper.GetString("database.path")
|
|
devMode := viper.GetBool("dev_mode")
|
|
|
|
// Initialize database
|
|
database, err := db.NewDatabase(dbPath)
|
|
if err != nil {
|
|
log.Fatalf("Failed to initialize database: %v", err)
|
|
}
|
|
defer database.Close()
|
|
|
|
// Initialize authentication service
|
|
authConfig := &auth.AuthConfig{
|
|
DevMode: viper.GetBool("dev_mode"),
|
|
JWTSecret: viper.GetString("jwt_secret"),
|
|
}
|
|
|
|
// Set default JWT secret if not configured
|
|
if authConfig.JWTSecret == "" {
|
|
authConfig.JWTSecret = "dev-secret-change-in-production"
|
|
if authConfig.DevMode {
|
|
log.Printf("🔑 Using default JWT secret for development")
|
|
}
|
|
}
|
|
|
|
authService := auth.NewAuthService(authConfig)
|
|
|
|
// Initialize handlers
|
|
contentHandler := api.NewContentHandler(database, authService)
|
|
|
|
// Setup router
|
|
router := mux.NewRouter()
|
|
|
|
// Add middleware
|
|
router.Use(api.CORSMiddleware)
|
|
router.Use(api.LoggingMiddleware)
|
|
router.Use(api.ContentTypeMiddleware)
|
|
|
|
// Health check endpoint
|
|
router.HandleFunc("/health", api.HealthMiddleware())
|
|
|
|
// API routes
|
|
apiRouter := router.PathPrefix("/api/content").Subrouter()
|
|
|
|
// Content endpoints matching the expected API contract
|
|
apiRouter.HandleFunc("/bulk", contentHandler.GetBulkContent).Methods("GET")
|
|
apiRouter.HandleFunc("/{id}", contentHandler.GetContent).Methods("GET")
|
|
apiRouter.HandleFunc("/{id}", contentHandler.UpdateContent).Methods("PUT")
|
|
apiRouter.HandleFunc("", contentHandler.GetAllContent).Methods("GET")
|
|
apiRouter.HandleFunc("", contentHandler.CreateContent).Methods("POST")
|
|
|
|
// Version control endpoints
|
|
apiRouter.HandleFunc("/{id}/versions", contentHandler.GetContentVersions).Methods("GET")
|
|
apiRouter.HandleFunc("/{id}/rollback", contentHandler.RollbackContent).Methods("POST")
|
|
|
|
// Handle CORS preflight requests explicitly
|
|
apiRouter.HandleFunc("/{id}", api.CORSPreflightHandler).Methods("OPTIONS")
|
|
apiRouter.HandleFunc("", api.CORSPreflightHandler).Methods("OPTIONS")
|
|
apiRouter.HandleFunc("/bulk", api.CORSPreflightHandler).Methods("OPTIONS")
|
|
apiRouter.HandleFunc("/{id}/versions", api.CORSPreflightHandler).Methods("OPTIONS")
|
|
apiRouter.HandleFunc("/{id}/rollback", api.CORSPreflightHandler).Methods("OPTIONS")
|
|
|
|
// Start server
|
|
addr := fmt.Sprintf(":%d", port)
|
|
mode := "production"
|
|
if devMode {
|
|
mode = "development"
|
|
}
|
|
|
|
fmt.Printf("🚀 Insertr Content Server starting (%s mode)...\n", mode)
|
|
fmt.Printf("📁 Database: %s\n", dbPath)
|
|
fmt.Printf("🌐 Server running at: http://localhost%s\n", addr)
|
|
fmt.Printf("💚 Health check: http://localhost%s/health\n", addr)
|
|
fmt.Printf("📊 API endpoints:\n")
|
|
fmt.Printf(" GET /api/content?site_id={site}\n")
|
|
fmt.Printf(" GET /api/content/{id}?site_id={site}\n")
|
|
fmt.Printf(" GET /api/content/bulk?site_id={site}&ids[]={id1}&ids[]={id2}\n")
|
|
fmt.Printf(" POST /api/content\n")
|
|
fmt.Printf(" PUT /api/content/{id}\n")
|
|
fmt.Printf(" GET /api/content/{id}/versions?site_id={site}\n")
|
|
fmt.Printf(" POST /api/content/{id}/rollback\n")
|
|
fmt.Printf("\n🔄 Press Ctrl+C to shutdown gracefully\n\n")
|
|
|
|
// Setup graceful shutdown
|
|
server := &http.Server{
|
|
Addr: addr,
|
|
Handler: router,
|
|
}
|
|
|
|
// Start server in a goroutine
|
|
go func() {
|
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatalf("Server failed to start: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Wait for interrupt signal
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
<-quit
|
|
|
|
fmt.Println("\n🛑 Shutting down server...")
|
|
if err := server.Close(); err != nil {
|
|
log.Fatalf("Server forced to shutdown: %v", err)
|
|
}
|
|
|
|
fmt.Println("✅ Server shutdown complete")
|
|
}
|