- Implement Go-based CLI for build-time HTML enhancement - Add container expansion: div.insertr auto-expands to viable children - Create servedev command with live development workflow - Add Air configuration for automatic rebuilds and serving - Enable transition from runtime JS to build-time enhancement approach
88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var servedevCmd = &cobra.Command{
|
|
Use: "servedev",
|
|
Short: "Development server that parses and serves enhanced HTML files",
|
|
Long: `Servedev starts a development HTTP server that automatically parses HTML files
|
|
for insertr elements and serves the enhanced content. Perfect for development workflow
|
|
with live rebuilds via Air.`,
|
|
Run: runServedev,
|
|
}
|
|
|
|
var (
|
|
inputDir string
|
|
port int
|
|
)
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(servedevCmd)
|
|
|
|
servedevCmd.Flags().StringVarP(&inputDir, "input", "i", ".", "Input directory to serve")
|
|
servedevCmd.Flags().IntVarP(&port, "port", "p", 3000, "Port to serve on")
|
|
}
|
|
|
|
func runServedev(cmd *cobra.Command, args []string) {
|
|
// Resolve absolute path for input directory
|
|
absInputDir, err := filepath.Abs(inputDir)
|
|
if err != nil {
|
|
log.Fatalf("Error resolving input directory: %v", err)
|
|
}
|
|
|
|
// Check if input directory exists
|
|
if _, err := os.Stat(absInputDir); os.IsNotExist(err) {
|
|
log.Fatalf("Input directory does not exist: %s", absInputDir)
|
|
}
|
|
|
|
fmt.Printf("🚀 Starting development server...\n")
|
|
fmt.Printf("📁 Serving directory: %s\n", absInputDir)
|
|
fmt.Printf("🌐 Server running at: http://localhost:%d\n", port)
|
|
fmt.Printf("🔄 Manually refresh browser to see changes\n\n")
|
|
|
|
// Create file server
|
|
fileServer := http.FileServer(&enhancedFileSystem{
|
|
fs: http.Dir(absInputDir),
|
|
dir: absInputDir,
|
|
})
|
|
|
|
// Handle all requests with our enhanced file server
|
|
http.Handle("/", fileServer)
|
|
|
|
// Start server
|
|
addr := fmt.Sprintf(":%d", port)
|
|
log.Fatal(http.ListenAndServe(addr, nil))
|
|
}
|
|
|
|
// enhancedFileSystem wraps http.FileSystem to provide enhanced HTML serving
|
|
type enhancedFileSystem struct {
|
|
fs http.FileSystem
|
|
dir string
|
|
}
|
|
|
|
func (efs *enhancedFileSystem) Open(name string) (http.File, error) {
|
|
file, err := efs.fs.Open(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// For HTML files, we'll eventually enhance them here
|
|
// For now, just serve them as-is
|
|
if strings.HasSuffix(name, ".html") {
|
|
fmt.Printf("📄 Serving HTML: %s\n", name)
|
|
fmt.Println("🔍 Parser ran!")
|
|
// TODO: Parse for insertr elements and enhance
|
|
}
|
|
|
|
return file, nil
|
|
}
|