Migration This is my new public git source viewer. Source coming soon. Please contact with concerns/bugs. Contact me

internal/core/route.go

c0427d5fc7c0fcf8606c546071085e8af231fb81 · 5.1 KB · 170 lines raw

1 package core
2
3 import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "html/template"
8 "net/http"
9 )
10
11 // TemplateSet holds one independently-parsed template tree per view.
12 // Views are parsed separately because html/template has a single namespace
13 // for defined blocks -- two views both defining "main" would collide.
14 type TemplateSet map[string]*template.Template
15
16 // Renderer turns a Ctx into bytes plus a content type. HTML pages and JSON
17 // endpoints are the same shape of thing; this is where they unify.
18 type Renderer interface {
19 Render(c *Ctx, ts TemplateSet) ([]byte, error)
20 ContentType() string
21 }
22
23 // View renders an HTML template. The template calls modules via `mod`.
24 type View struct{ Template string }
25
26 func (v View) ContentType() string { return "text/html; charset=utf-8" }
27
28 func (v View) Render(c *Ctx, ts TemplateSet) ([]byte, error) {
29 t, ok := ts[v.Template]
30 if !ok {
31 return nil, fmt.Errorf("no such view %q", v.Template)
32 }
33 // Clone per render so the funcmap can close over this Ctx.
34 tt, err := t.Clone()
35 if err != nil {
36 return nil, err
37 }
38 tt = tt.Funcs(FuncMap(c))
39 var buf bytes.Buffer
40 if err := tt.ExecuteTemplate(&buf, v.Template, c); err != nil {
41 return nil, fmt.Errorf("view %s: %w", v.Template, err)
42 }
43 return buf.Bytes(), nil
44 }
45
46 // Endpoint builds the value that will be serialized. It must return a
47 // purpose-built DTO, never a raw module value -- see internal/api.
48 type Endpoint func(c *Ctx) (any, error)
49
50 // JSON renders an API endpoint.
51 type JSON struct{ Build Endpoint }
52
53 func (j JSON) ContentType() string { return "application/json; charset=utf-8" }
54
55 func (j JSON) Render(c *Ctx, _ TemplateSet) ([]byte, error) {
56 v, err := j.Build(c)
57 if err != nil {
58 return nil, err
59 }
60 b, err := json.MarshalIndent(v, "", " ")
61 if err != nil {
62 return nil, err
63 }
64 return append(b, '\n'), nil
65 }
66
67 // ------------------------------------------------------------------ routes
68
69 // Route binds a renderer to a net/http pattern (Go 1.22 wildcards).
70 //
71 // /{repo}/tree/{rest...} -> r.PathValue("repo"), r.PathValue("rest")
72 //
73 // Scope decides how the Ctx is built before Render runs.
74 type Route struct {
75 Name string
76 Pattern string // e.g. "GET /{repo}/commit/{rev}"
77 Render Renderer
78 Scope Scope
79 // Immutable marks responses whose content cannot change for a given
80 // URL -- anything addressed by a commit SHA. Those get a year-long
81 // Cache-Control; ref-addressed URLs get revalidation instead.
82 Immutable bool
83 // NavLabel, when set on a ScopeRepo route, adds a tab for it to the
84 // repo-scoped navigation (see nav.go) -- the top tab bar and the
85 // page-nav chips both derive from this instead of hardcoding links.
86 NavLabel string
87 }
88
89 type Scope int
90
91 const (
92 ScopeRepo Scope = iota // resolves {repo}, and {rest...}/{rev} if present
93 ScopeSite // no repository in the URL
94 )
95
96 // Page sizes for routes that paginate via ?ofs=. Kept in core (not
97 // modules) so buildCtx can quantize an incoming ofs to a real page
98 // boundary without an import cycle. Must match the literal args the
99 // log.html/graph.html templates pass to `mod "log"`/`mod "graph"`.
100 const (
101 LogPageSize = 20
102 GraphPageSize = 150
103 )
104
105 // httpError lets a module or endpoint choose the status code.
106 type httpError struct {
107 Code int
108 Err error
109 }
110
111 func (e httpError) Error() string { return e.Err.Error() }
112 func (e httpError) Unwrap() error { return e.Err }
113
114 func Errorf(code int, format string, a ...any) error {
115 return httpError{Code: code, Err: fmt.Errorf(format, a...)}
116 }
117
118 func statusOf(err error) int {
119 var he httpError
120 if ok := asHTTPError(err, &he); ok {
121 return he.Code
122 }
123 return http.StatusInternalServerError
124 }
125
126 // HeaderSetter lets a renderer add response headers. Optional: the server
127 // type-asserts for it after Render succeeds.
128 type HeaderSetter interface {
129 Headers(c *Ctx, h http.Header)
130 }
131
132 // Raw serves file bytes straight out of the object database.
133 //
134 // SECURITY: a repository can contain an .html file with a <script> tag. If
135 // this served it as text/html on your own origin, any push would be stored
136 // XSS against the whole site -- which is exactly why GitHub puts raw content
137 // on raw.githubusercontent.com, a separate origin. Serving it from the same
138 // host is only safe if the response cannot be interpreted as a document:
139 // text/plain or octet-stream, nosniff so the browser does not second-guess
140 // that, and a sandbox CSP as a backstop.
141 type Raw struct{ Module string }
142
143 func (Raw) ContentType() string { return "text/plain; charset=utf-8" }
144
145 func (r Raw) Render(c *Ctx, _ TemplateSet) ([]byte, error) {
146 v, err := c.Mod(r.Module)
147 if err != nil {
148 return nil, err
149 }
150 b, ok := v.(interface{ Bytes() []byte })
151 if !ok {
152 return nil, fmt.Errorf("raw: module %q is not raw-serving", r.Module)
153 }
154 return b.Bytes(), nil
155 }
156
157 func (r Raw) Headers(c *Ctx, h http.Header) {
158 h.Set("X-Content-Type-Options", "nosniff")
159 h.Set("Content-Security-Policy", "default-src 'none'; sandbox")
160 h.Set("Content-Disposition", "inline")
161
162 v, err := c.Mod(r.Module)
163 if err != nil {
164 return
165 }
166 if bin, ok := v.(interface{ IsBinary() bool }); ok && bin.IsBinary() {
167 h.Set("Content-Type", "application/octet-stream")
168 h.Set("Content-Disposition", "attachment")
169 }
170 }