Lindenii Project Forge
Add template rendering
package web import (
"html/template"
"net/http"
"path/filepath"
"go.lindenii.runxiyu.org/forge/forged/internal/common/misc"
handlers "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/handlers" repoHandlers "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/handlers/repo"
"go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/templates"
) type handler struct { r *Router } func NewHandler(cfg Config) http.Handler { h := &handler{r: NewRouter().ReverseProxy(cfg.ReverseProxy)}
// Static files staticDir := filepath.Join(cfg.Root, "static") staticFS := http.FileServer(http.Dir(staticDir))
staticFS := http.FileServer(http.Dir(cfg.StaticPath))
h.r.ANYHTTP("-/static/*rest", http.StripPrefix("/-/static/", staticFS), WithDirIfEmpty("rest"), )
// Feature handler instances indexHTTP := handlers.NewIndexHTTP() groupHTTP := handlers.NewGroupHTTP() repoHTTP := repoHandlers.NewHTTP()
funcs := template.FuncMap{ "path_escape": misc.PathEscape, "query_escape": misc.QueryEscape, "minus": misc.Minus, "first_line": misc.FirstLine, "dereference_error": misc.DereferenceOrZero[error], } t := templates.MustParseDir(cfg.TemplatesPath, funcs) renderer := templates.New(t) indexHTTP := handlers.NewIndexHTTP(renderer) groupHTTP := handlers.NewGroupHTTP(renderer) repoHTTP := repoHandlers.NewHTTP(renderer)
notImpl := handlers.NewNotImplementedHTTP() // Index h.r.GET("/", indexHTTP.Index) // Top-level utilities h.r.ANY("-/login", notImpl.Handle) h.r.ANY("-/users", notImpl.Handle) // Group index h.r.GET("@group/", groupHTTP.Index) // Repo index h.r.GET("@group/-/repos/:repo/", repoHTTP.Index) // Repo (not implemented yet) h.r.ANY("@group/-/repos/:repo/info", notImpl.Handle) h.r.ANY("@group/-/repos/:repo/git-upload-pack", notImpl.Handle) // Repo features h.r.GET("@group/-/repos/:repo/branches/", notImpl.Handle) h.r.GET("@group/-/repos/:repo/log/", notImpl.Handle) h.r.GET("@group/-/repos/:repo/commit/:commit", notImpl.Handle) h.r.GET("@group/-/repos/:repo/tree/*rest", repoHTTP.Tree, WithDirIfEmpty("rest")) h.r.GET("@group/-/repos/:repo/raw/*rest", repoHTTP.Raw, WithDirIfEmpty("rest")) h.r.GET("@group/-/repos/:repo/contrib/", notImpl.Handle) h.r.GET("@group/-/repos/:repo/contrib/:mr", notImpl.Handle) return h } func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.r.ServeHTTP(w, r) }
package handlers import ( "net/http" "strings"
"go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/templates"
wtypes "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/types" )
type GroupHTTP struct{}
type GroupHTTP struct { r templates.Renderer }
func NewGroupHTTP() *GroupHTTP { return &GroupHTTP{} }
func NewGroupHTTP(r templates.Renderer) *GroupHTTP { return &GroupHTTP{r: r} }
func (h *GroupHTTP) Index(w http.ResponseWriter, r *http.Request, _ wtypes.Vars) { base := wtypes.Base(r)
_, _ = w.Write([]byte("group index for: /" + strings.Join(base.GroupPath, "/") + "/"))
_ = h.r.Render(w, "group/index.html", struct { GroupPath string }{ GroupPath: "/" + strings.Join(base.GroupPath, "/") + "/", })
}
package handlers import (
"log"
"net/http"
"go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/templates"
wtypes "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/types" )
type IndexHTTP struct{}
type IndexHTTP struct { r templates.Renderer }
func NewIndexHTTP() *IndexHTTP { return &IndexHTTP{} }
func NewIndexHTTP(r templates.Renderer) *IndexHTTP { return &IndexHTTP{r: r} }
func (h *IndexHTTP) Index(w http.ResponseWriter, _ *http.Request, _ wtypes.Vars) {
_, _ = w.Write([]byte("index: replace with template render"))
err := h.r.Render(w, "index", struct { Title string }{ Title: "Home", }) if err != nil { log.Println("failed to render index page", "error", err) }
}
package repo import (
"fmt"
"net/http" "strings"
"go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/templates"
wtypes "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/types" )
type HTTP struct{}
type HTTP struct { r templates.Renderer }
func NewHTTP() *HTTP { return &HTTP{} }
func NewHTTP(r templates.Renderer) *HTTP { return &HTTP{r: r} }
func (h *HTTP) Index(w http.ResponseWriter, r *http.Request, v wtypes.Vars) { base := wtypes.Base(r) repo := v["repo"]
_, _ = w.Write([]byte(fmt.Sprintf("repo index: group=%q repo=%q", "/"+strings.Join(base.GroupPath, "/")+"/", repo)))
_ = h.r.Render(w, "repo/index.html", struct { Group string Repo string }{ Group: "/" + strings.Join(base.GroupPath, "/") + "/", Repo: repo, })
}
package templates import ( "html/template" "io/fs" "os" "path/filepath" ) func MustParseDir(dir string, funcs template.FuncMap) *template.Template { base := template.New("").Funcs(funcs) err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { if err != nil { return err } if d.IsDir() { return nil } b, err := os.ReadFile(path) if err != nil { return err } _, err = base.Parse(string(b)) return err }) if err != nil { panic(err) } return base }
package templates import ( "html/template" "net/http" ) type Renderer interface { Render(w http.ResponseWriter, name string, data any) error } type tmplRenderer struct { t *template.Template } func New(t *template.Template) Renderer { return &tmplRenderer{t: t} } func (r *tmplRenderer) Render(w http.ResponseWriter, name string, data any) error { w.Header().Set("Content-Type", "text/html; charset=utf-8") return r.t.ExecuteTemplate(w, name, data) }