package server import ( "fmt" "io" "net" "net/http" "os" "path/filepath" "time" "beelloo/internal/invoice" "beelloo/internal/render" ) type Config struct { Addr string MDPath string CSSPath string } type Result struct { Listener net.Listener } func Serve(cfg Config, out, errOut io.Writer) error { if cfg.Addr == "" { cfg.Addr = "127.0.0.1:0" } cssPath, err := resolveCSSPath(cfg.MDPath, cfg.CSSPath) if err != nil { return err } cfg.CSSPath = cssPath listener, err := net.Listen("tcp", cfg.Addr) if err != nil { return fmt.Errorf("failed to listen on %s: %w", cfg.Addr, err) } mux := http.NewServeMux() mux.HandleFunc("/style.css", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "no-store") w.Header().Set("Pragma", "no-cache") w.Header().Set("Expires", "0") if cfg.CSSPath != "" { http.ServeFile(w, r, cfg.CSSPath) return } css, err := render.DefaultCSS() if err != nil { http.Error(w, "failed to load default css", http.StatusInternalServerError) return } w.Header().Set("Content-Type", "text/css; charset=utf-8") _, _ = w.Write(css) }) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { file, err := os.Open(cfg.MDPath) if err != nil { http.Error(w, "failed to open markdown", http.StatusInternalServerError) return } defer file.Close() doc, err := invoice.ParseMarkdown(file) if err != nil { http.Error(w, "parse error: "+err.Error(), http.StatusBadRequest) return } if err := invoice.Validate(&doc); err != nil { http.Error(w, "validation error: "+err.Error(), http.StatusBadRequest) return } html, err := render.RenderHTMLWithCSSLink(doc, fmt.Sprintf("/style.css?v=%d", time.Now().UnixNano())) if err != nil { http.Error(w, "render error: "+err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "text/html; charset=utf-8") _, _ = w.Write([]byte(html)) }) server := &http.Server{Handler: mux} if cfg.CSSPath != "" { fmt.Fprintf(out, "Using CSS: %s\n", cfg.CSSPath) } else { fmt.Fprintln(out, "Using embedded CSS") } fmt.Fprintf(out, "Serving on http://%s/\n", listener.Addr().String()) if err := server.Serve(listener); err != nil && err != http.ErrServerClosed { return fmt.Errorf("server error: %w", err) } return nil } func resolveCSSPath(mdPath, override string) (string, error) { if override != "" { if _, err := os.Stat(override); err != nil { return "", fmt.Errorf("failed to stat css file %s: %w", override, err) } return override, nil } if mdPath == "" { return "", nil } mdDir := filepath.Dir(mdPath) candidates := []string{ filepath.Join(mdDir, "style.css"), "style.css", } for _, path := range candidates { if _, err := os.Stat(path); err == nil { return path, nil } } return "", nil }