From dc801fb26b3a3d119f2f01173d38f60ee787d282 Mon Sep 17 00:00:00 2001 From: Joakim Date: Thu, 23 Oct 2025 22:32:42 +0200 Subject: [PATCH] Remove internal/content package and use engine directly - Eliminate content.Enhancer wrapper around engine.ContentEngine - Update cmd/enhance.go to call engine.ProcessFile/ProcessDirectory directly - Update sites/manager.go to use engine.NewContentEngineWithAuth directly - Remove unused EnhancementConfig and discovery configuration logic - Simplify enhance command to use input path as site ID - Remove CLI config dependency from config package --- cmd/enhance.go | 62 ++-------------- internal/config/config.go | 21 ++---- internal/config/loader.go | 7 -- internal/config/validation.go | 25 +------ internal/content/enhancer.go | 130 ---------------------------------- internal/sites/manager.go | 29 +------- 6 files changed, 16 insertions(+), 258 deletions(-) delete mode 100644 internal/content/enhancer.go diff --git a/cmd/enhance.go b/cmd/enhance.go index d1d69d6..76f389f 100644 --- a/cmd/enhance.go +++ b/cmd/enhance.go @@ -9,9 +9,8 @@ import ( "github.com/spf13/cobra" - "github.com/insertr/insertr/internal/config" - "github.com/insertr/insertr/internal/content" "github.com/insertr/insertr/internal/db" + "github.com/insertr/insertr/internal/engine" ) var enhanceCmd = &cobra.Command{ @@ -37,6 +36,7 @@ func init() { func runEnhance(cmd *cobra.Command, args []string) { inputPath := args[0] + siteID := inputPath // Validate input path and determine if it's a file or directory inputStat, err := os.Stat(inputPath) @@ -68,30 +68,6 @@ func runEnhance(cmd *cobra.Command, args []string) { outputDir = "./dist" // default } - // Auto-derive site_id for demo paths or validate for production - if strings.Contains(inputPath, "/demos/") || strings.Contains(inputPath, "./demos/") { - // Auto-derive site_id from demo path - cfg.CLI.SiteID = content.DeriveOrValidateSiteID(inputPath, cfg.CLI.SiteID) - } else { - // Validate site_id for non-demo paths - if cfg.CLI.SiteID == "" || cfg.CLI.SiteID == "demo" { - log.Fatalf(`โŒ site_id must be explicitly configured for non-demo sites. - -๐Ÿ’ก Examples: - # Set via command line: - insertr enhance --site-id mysite /path/to/site - - # Set in insertr.yaml: - cli: - site_id: "mysite" - - # Set via environment: - INSERTR_CLI_SITE_ID=mysite insertr enhance /path/to/site - -๐Ÿš€ For demo sites under demos/, site_id is auto-derived from the directory name.`) - } - } - // Create content client var client db.ContentRepository if cfg.API.URL != "" { @@ -109,37 +85,13 @@ func runEnhance(cmd *cobra.Command, args []string) { panic("๐Ÿงช No database or API configured\n") } - // Load site-specific configuration - enhancementConfig := content.EnhancementConfig{ - Discovery: config.DiscoveryConfig{ - Enabled: false, // Default: disabled for explicit class="insertr" markings only - Aggressive: false, - Containers: true, - Individual: true, - }, - ContentInjection: true, - GenerateIDs: true, - } - - // Override with site-specific discovery config if available - for _, site := range cfg.Server.Sites { - if site.SiteID == cfg.CLI.SiteID && site.Discovery != nil { - enhancementConfig.Discovery.Enabled = site.Discovery.Enabled - enhancementConfig.Discovery.Aggressive = site.Discovery.Aggressive - enhancementConfig.Discovery.Containers = site.Discovery.Containers - enhancementConfig.Discovery.Individual = site.Discovery.Individual - fmt.Printf("๐Ÿ”ง Site '%s': discovery.enabled=%v\n", cfg.CLI.SiteID, site.Discovery.Enabled) - break - } - } - - // Create enhancer with loaded configuration - enhancer := content.NewEnhancer(client, cfg.CLI.SiteID, enhancementConfig) + // Create content engine directly + contentEngine := engine.NewContentEngine(client) fmt.Printf("๐Ÿš€ Starting enhancement process...\n") fmt.Printf("๐Ÿ“ Input: %s\n", inputPath) fmt.Printf("๐Ÿ“ Output: %s\n", outputDir) - fmt.Printf("๐Ÿท๏ธ Site ID: %s\n\n", cfg.CLI.SiteID) + fmt.Printf("๐Ÿท๏ธ Site ID: %s\n\n", siteID) // Enhance based on input type if isFile { @@ -151,11 +103,11 @@ func runEnhance(cmd *cobra.Command, args []string) { } // If output doesn't exist or is a file, use it as-is as the output file path - if err := enhancer.EnhanceFile(inputPath, outputFilePath); err != nil { + if err := contentEngine.ProcessFile(inputPath, outputFilePath, siteID, engine.Enhancement); err != nil { log.Fatalf("Enhancement failed: %v", err) } } else { - if err := enhancer.EnhanceDirectory(inputPath, outputDir); err != nil { + if err := contentEngine.ProcessDirectory(inputPath, outputDir, siteID, engine.Enhancement); err != nil { log.Fatalf("Enhancement failed: %v", err) } } diff --git a/internal/config/config.go b/internal/config/config.go index 1618f29..dcbefe7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -3,7 +3,6 @@ package config type Config struct { Database DatabaseConfig `yaml:"database" mapstructure:"database"` API APIConfig `yaml:"api" mapstructure:"api"` - CLI CLIConfig `yaml:"cli" mapstructure:"cli"` Server ServerConfig `yaml:"server" mapstructure:"server"` Auth AuthConfig `yaml:"auth" mapstructure:"auth"` Library LibraryConfig `yaml:"library" mapstructure:"library"` @@ -18,10 +17,6 @@ type APIConfig struct { Key string `yaml:"key" mapstructure:"key"` } -type CLIConfig struct { - SiteID string `yaml:"site_id" mapstructure:"site_id"` -} - type ServerConfig struct { Port int `yaml:"port" mapstructure:"port"` Host string `yaml:"host" mapstructure:"host"` @@ -29,18 +24,10 @@ type ServerConfig struct { } type AuthConfig struct { - DevMode bool `yaml:"dev_mode" mapstructure:"dev_mode"` - Provider string `yaml:"provider" mapstructure:"provider"` - JWTSecret string `yaml:"jwt_secret" mapstructure:"jwt_secret"` - OAuthConfigs map[string]OAuthConfig `yaml:"oauth_configs" mapstructure:"oauth_configs"` - OIDC *OIDCConfig `yaml:"oidc" mapstructure:"oidc"` -} - -type OAuthConfig struct { - ClientID string `yaml:"client_id" mapstructure:"client_id"` - ClientSecret string `yaml:"client_secret" mapstructure:"client_secret"` - RedirectURL string `yaml:"redirect_url" mapstructure:"redirect_url"` - Scopes []string `yaml:"scopes" mapstructure:"scopes"` + DevMode bool `yaml:"dev_mode" mapstructure:"dev_mode"` + Provider string `yaml:"provider" mapstructure:"provider"` + JWTSecret string `yaml:"jwt_secret" mapstructure:"jwt_secret"` + OIDC *OIDCConfig `yaml:"oidc" mapstructure:"oidc"` } type OIDCConfig struct { diff --git a/internal/config/loader.go b/internal/config/loader.go index b117a17..c4c534c 100644 --- a/internal/config/loader.go +++ b/internal/config/loader.go @@ -69,9 +69,6 @@ func (l *viperLoader) LoadWithFlags(dbPath, apiURL, apiKey, siteID string) (*Con if apiKey != "" { config.API.Key = apiKey } - if siteID != "" { - config.CLI.SiteID = siteID - } return config, validate(config) } @@ -81,10 +78,6 @@ func (l *viperLoader) setDefaults(config *Config) error { config.Database.Path = "./insertr.db" } - if config.CLI.SiteID == "" { - config.CLI.SiteID = "demo" - } - if config.Server.Port == 0 { config.Server.Port = 8080 } diff --git a/internal/config/validation.go b/internal/config/validation.go index c10828a..41637dd 100644 --- a/internal/config/validation.go +++ b/internal/config/validation.go @@ -5,6 +5,7 @@ import ( "net/url" "os" "path/filepath" + "slices" "strings" ) @@ -17,10 +18,6 @@ func validate(config *Config) error { return fmt.Errorf("api config: %w", err) } - if err := validateCLI(config.CLI); err != nil { - return fmt.Errorf("cli config: %w", err) - } - if err := validateServer(config.Server); err != nil { return fmt.Errorf("server config: %w", err) } @@ -66,18 +63,6 @@ func validateAPI(config APIConfig) error { return nil } -func validateCLI(config CLIConfig) error { - if config.SiteID == "" { - return fmt.Errorf("site_id is required") - } - - if strings.TrimSpace(config.SiteID) != config.SiteID { - return fmt.Errorf("site_id cannot have leading or trailing whitespace") - } - - return nil -} - func validateServer(config ServerConfig) error { if config.Port < 1 || config.Port > 65535 { return fmt.Errorf("port must be between 1 and 65535, got %d", config.Port) @@ -136,13 +121,7 @@ func validateAuth(config AuthConfig) error { } validProviders := []string{"mock", "authentik"} - valid := false - for _, provider := range validProviders { - if config.Provider == provider { - valid = true - break - } - } + valid := slices.Contains(validProviders, config.Provider) if !valid { return fmt.Errorf("invalid provider %q, must be one of: %s", config.Provider, strings.Join(validProviders, ", ")) } diff --git a/internal/content/enhancer.go b/internal/content/enhancer.go deleted file mode 100644 index 5506b14..0000000 --- a/internal/content/enhancer.go +++ /dev/null @@ -1,130 +0,0 @@ -package content - -import ( - "path/filepath" - "strings" - - "github.com/insertr/insertr/internal/config" - "github.com/insertr/insertr/internal/db" - "github.com/insertr/insertr/internal/engine" -) - -// EnhancementConfig configures the enhancement pipeline -type EnhancementConfig struct { - Discovery config.DiscoveryConfig - ContentInjection bool - GenerateIDs bool -} - -// Enhancer combines discovery, ID generation, and content injection in unified pipeline -type Enhancer struct { - engine *engine.ContentEngine - config EnhancementConfig - siteID string -} - -// NewEnhancer creates a new HTML enhancer with unified pipeline -func NewEnhancer(client db.ContentRepository, siteID string, config EnhancementConfig) *Enhancer { - return &Enhancer{ - engine: engine.NewContentEngine(client), - config: config, - siteID: siteID, - } -} - -// NewEnhancerWithAuth creates a new HTML enhancer with auth provider -func NewEnhancerWithAuth(client db.ContentRepository, siteID string, config EnhancementConfig, authProvider *engine.AuthProvider) *Enhancer { - return &Enhancer{ - engine: engine.NewContentEngineWithAuth(client, authProvider), - config: config, - siteID: siteID, - } -} - -// NewDefaultEnhancer creates an enhancer with default configuration -func NewDefaultEnhancer(client db.ContentRepository, siteID string) *Enhancer { - defaultConfig := EnhancementConfig{ - Discovery: config.DiscoveryConfig{ - Enabled: true, - Aggressive: false, - Containers: true, - Individual: true, - }, - ContentInjection: true, - GenerateIDs: true, - } - return NewEnhancer(client, siteID, defaultConfig) -} - -// EnhanceFile processes a single HTML file through the engine -func (e *Enhancer) EnhanceFile(inputPath, outputPath string) error { - return e.engine.ProcessFile(inputPath, outputPath, e.siteID, engine.Enhancement) -} - -// EnhanceDirectory processes all files in a directory through the engine -func (e *Enhancer) EnhanceDirectory(inputDir, outputDir string) error { - return e.engine.ProcessDirectory(inputDir, outputDir, e.siteID, engine.Enhancement) -} - -// SetSiteID sets the site ID for the enhancer -func (e *Enhancer) SetSiteID(siteID string) { - e.siteID = siteID -} - -// EnhanceInPlace performs in-place enhancement of static site files -func (e *Enhancer) EnhanceInPlace(sitePath string, siteID string) error { - // Use the provided siteID (derivation should happen at CLI level) - e.siteID = siteID - - // Use EnhanceDirectory with same input and output (in-place) - return e.EnhanceDirectory(sitePath, sitePath) -} - -// DeriveOrValidateSiteID automatically derives site_id for demo paths or validates for production -func DeriveOrValidateSiteID(sitePath string, configSiteID string) string { - // Check if this is a demo path - if strings.Contains(sitePath, "/demos/") || strings.Contains(sitePath, "./demos/") { - return deriveDemoSiteID(sitePath) - } - - // For non-demo paths, return the configured site_id - // Validation of non-demo site_id will be handled at the CLI level - return configSiteID -} - -// deriveDemoSiteID extracts site_id from demo directory structure -func deriveDemoSiteID(sitePath string) string { - // Convert to absolute path and clean it - absPath, err := filepath.Abs(sitePath) - if err != nil { - absPath = sitePath - } - absPath = filepath.Clean(absPath) - - // Flattened structure - just use directory name after demos/ - // demos/default -> "default" - // demos/simple -> "simple" - // demos/dan-eden-portfolio -> "dan-eden-portfolio" - - parts := strings.Split(absPath, string(filepath.Separator)) - - // Find the demos directory index - demosIndex := -1 - for i, part := range parts { - if part == "demos" { - demosIndex = i - break - } - } - - if demosIndex == -1 || demosIndex >= len(parts)-1 { - // Fallback if demos not found in path - return "default" - } - - // Get the segment after demos/ and clean it - dirName := parts[demosIndex+1] - dirName = strings.TrimSuffix(dirName, "_enhanced") - - return dirName -} diff --git a/internal/sites/manager.go b/internal/sites/manager.go index c44a6e8..adea76a 100644 --- a/internal/sites/manager.go +++ b/internal/sites/manager.go @@ -9,7 +9,6 @@ import ( "sync" "github.com/insertr/insertr/internal/config" - "github.com/insertr/insertr/internal/content" "github.com/insertr/insertr/internal/db" "github.com/insertr/insertr/internal/engine" "maps" @@ -18,7 +17,6 @@ import ( // SiteManager handles registration and enhancement of static sites type SiteManager struct { sites map[string]*config.SiteConfig - enhancer *content.Enhancer mutex sync.RWMutex devMode bool contentClient db.ContentRepository @@ -29,7 +27,6 @@ type SiteManager struct { func NewSiteManager(contentClient db.ContentRepository, devMode bool) *SiteManager { return &SiteManager{ sites: make(map[string]*config.SiteConfig), - enhancer: content.NewDefaultEnhancer(contentClient, ""), // siteID will be set per operation devMode: devMode, contentClient: contentClient, authProvider: &engine.AuthProvider{Type: "mock"}, // default @@ -151,31 +148,11 @@ func (sm *SiteManager) EnhanceSite(siteID string) error { return fmt.Errorf("failed to create output directory %s: %w", outputPath, err) } - // Create enhancer with auth provider for this operation - // Discovery disabled by default - developers should explicitly mark elements with class="insertr" - discoveryConfig := config.DiscoveryConfig{ - Enabled: false, // Changed from true - respect developer intent - Aggressive: false, - Containers: true, - Individual: true, - } - - // Override with site-specific discovery config if provided - if site.Discovery != nil { - discoveryConfig = *site.Discovery - log.Printf("๐Ÿ”ง Using site-specific discovery config for %s: enabled=%v, aggressive=%v", - siteID, discoveryConfig.Enabled, discoveryConfig.Aggressive) - } - - config := content.EnhancementConfig{ - Discovery: discoveryConfig, - ContentInjection: true, - GenerateIDs: true, - } - enhancer := content.NewEnhancerWithAuth(sm.contentClient, siteID, config, sm.authProvider) + // Create content engine with auth provider for this operation + contentEngine := engine.NewContentEngineWithAuth(sm.contentClient, sm.authProvider) // Perform enhancement from source to output - if err := enhancer.EnhanceDirectory(sourcePath, outputPath); err != nil { + if err := contentEngine.ProcessDirectory(sourcePath, outputPath, siteID, engine.Enhancement); err != nil { return fmt.Errorf("failed to enhance site %s: %w", siteID, err) }