diff --git a/cmd/serve.go b/cmd/serve.go index 71c6171..f7d2120 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -144,6 +144,15 @@ func runServe(cmd *cobra.Command, args []string) { // Static library serving (for demo sites) router.HandleFunc("/insertr.js", contentHandler.ServeInsertrJS).Methods("GET") + // Static site serving - serve registered sites at /sites/{site_id} + siteRouter := router.PathPrefix("/sites").Subrouter() + for siteID, siteConfig := range siteManager.GetAllSites() { + log.Printf("šŸ“ Serving site %s from %s at /sites/%s/", siteID, siteConfig.Path, siteID) + siteRouter.PathPrefix("/" + siteID + "/").Handler( + http.StripPrefix("/sites/"+siteID+"/", + http.FileServer(http.Dir(siteConfig.Path)))) + } + // Handle CORS preflight requests explicitly contentRouter.HandleFunc("/{id}", api.CORSPreflightHandler).Methods("OPTIONS") contentRouter.HandleFunc("", api.CORSPreflightHandler).Methods("OPTIONS") @@ -171,6 +180,10 @@ func runServe(cmd *cobra.Command, args []string) { 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("🌐 Static sites:\n") + for siteID, _ := range siteManager.GetAllSites() { + fmt.Printf(" %s: http://localhost%s/sites/%s/\n", siteID, addr, siteID) + } fmt.Printf("\nšŸ”„ Press Ctrl+C to shutdown gracefully\n\n") // Setup graceful shutdown diff --git a/internal/api/middleware.go b/internal/api/middleware.go index 8183096..dc4d9fa 100644 --- a/internal/api/middleware.go +++ b/internal/api/middleware.go @@ -57,8 +57,11 @@ func (rw *responseWriter) WriteHeader(code int) { // ContentTypeMiddleware ensures JSON responses have proper content type func ContentTypeMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // Set default content type for API responses - if r.URL.Path != "/" && (r.Method == "GET" || r.Method == "POST" || r.Method == "PUT") { + // Set default content type for API responses only, not static sites + if r.URL.Path != "/" && + !strings.HasPrefix(r.URL.Path, "/sites/") && + !strings.HasPrefix(r.URL.Path, "/insertr.js") && + (r.Method == "GET" || r.Method == "POST" || r.Method == "PUT") { w.Header().Set("Content-Type", "application/json") } diff --git a/internal/engine/injector.go b/internal/engine/injector.go index 6829eef..42284c9 100644 --- a/internal/engine/injector.go +++ b/internal/engine/injector.go @@ -352,6 +352,11 @@ func (i *Injector) InjectDemoGateIfNeeded(doc *html.Node) { // InjectEditorScript injects the insertr.js library and initialization script func (i *Injector) InjectEditorScript(doc *html.Node) { + // Check if script is already injected + if i.hasInsertrScript(doc) { + return + } + // Find the head element for the script tag headNode := i.findHeadElement(doc) if headNode == nil { @@ -360,8 +365,8 @@ func (i *Injector) InjectEditorScript(doc *html.Node) { } // Create script element that loads insertr.js from our server - scriptHTML := fmt.Sprintf(` - +