package main import ( "bytes" "errors" "html/template" "io/fs" "iter" "log/slog" "net/http" "path" ) type Server struct { config *Config static http.FileSystem } type indexData struct { Modules iter.Seq[moduleData] } type moduleData struct { Module string Path string VCS string Repo string Subdir string } type errorData struct { Code int Message string } func renderError(w http.ResponseWriter, r *http.Request, tmpl *template.Template, code int) { data := errorData{Code: code, Message: http.StatusText(code)} if !renderTemplate(w, r, tmpl, "error.html", code, data) { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } } func render(w http.ResponseWriter, r *http.Request, tmpl *template.Template, name string, status int, data any) { if !renderTemplate(w, r, tmpl, name, status, data) { renderError(w, r, tmpl, http.StatusInternalServerError) } } // renderTemplate buffers execution so failures leave the response untouched. func renderTemplate(w http.ResponseWriter, r *http.Request, tmpl *template.Template, name string, status int, data any) bool { var body bytes.Buffer if err := tmpl.ExecuteTemplate(&body, name, data); err != nil { slog.ErrorContext(r.Context(), "failed to render template", slog.String("template", name), slog.Any("error", err)) return false } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(status) _, _ = body.WriteTo(w) return true } func (s *Server) Index(w http.ResponseWriter, r *http.Request) { snapshot := s.config.current.Load() render(w, r, snapshot.Template, "index.html", http.StatusOK, indexData{ Modules: func(yield func(moduleData) bool) { for _, modulePath := range snapshot.ModulePaths { target := snapshot.Modules[modulePath] if !yield(moduleData{Module: path.Join(snapshot.Host, modulePath), Path: path.Join("/", modulePath), VCS: target.VCS, Repo: target.Repo, Subdir: target.Subdir}) { return } } }, }) } func (s *Server) Fallback(w http.ResponseWriter, r *http.Request) { snapshot := s.config.current.Load() modulePath, target, ok := snapshot.Modules.Resolve(r.URL.Path) if ok { render(w, r, snapshot.Template, "mod.html", http.StatusOK, moduleData{ Module: path.Join(snapshot.Host, modulePath), Path: path.Join("/", modulePath), VCS: target.VCS, Repo: target.Repo, Subdir: target.Subdir, }) return } served, err := s.serveStatic(w, r) if err != nil { slog.ErrorContext(r.Context(), "failed to serve static file", slog.String("path", r.URL.Path), slog.Any("error", err)) renderError(w, r, snapshot.Template, http.StatusInternalServerError) return } if served { return } renderError(w, r, snapshot.Template, http.StatusNotFound) } // serveStatic serves regular files only, leaving missing files and directories // to the application's 404 page. func (s *Server) serveStatic(w http.ResponseWriter, r *http.Request) (bool, error) { if s.static == nil { return false, nil } file, err := s.static.Open(r.URL.Path) if err != nil { if errors.Is(err, fs.ErrNotExist) { return false, nil } return false, err } defer file.Close() info, err := file.Stat() if err != nil { if errors.Is(err, fs.ErrNotExist) { return false, nil } return false, err } if !info.Mode().IsRegular() { return false, nil } http.ServeContent(w, r, info.Name(), info.ModTime(), file) return true, nil }