internal/httpd/routes.go

ab56677031290fee18323c467841a25216d45768
gitbay/internal/httpd/routes.go history · blame · raw

53 lines · 2279 bytes

 1package httpd
 2
 3import "net/http"
 4
 5// Route is one entry in the explicit route table. The view-only guarantee is
 6// structural: Handler() consults web.mode when building the table, and the
 7// route test asserts no mutating route exists in view_only mode.
 8type Route struct {
 9	Method  string
10	Pattern string // without method prefix
11	// Mutating marks routes that can change server state. The git transport
12	// POSTs are not mutating: upload-pack is a pure read, and the
13	// receive-pack endpoint is a static refusal that writes nothing.
14	Mutating bool
15	Handler  http.HandlerFunc
16}
17
18// Routes returns the route table for the configured web.mode.
19func (s *Server) Routes() []Route {
20	// Git smart transport (anonymous, public repos only).
21	routes := []Route{
22		{Method: "GET", Pattern: "/{owner}/{repo}/info/refs", Handler: s.infoRefs},
23		{Method: "POST", Pattern: "/{owner}/{repo}/git-upload-pack", Handler: s.uploadPack},
24		{Method: "POST", Pattern: "/{owner}/{repo}/git-receive-pack", Handler: s.receivePackRefusal},
25	}
26
27	// Web UI, read-only. These exist in every mode.
28	routes = append(routes,
29		Route{Method: "GET", Pattern: "/{$}", Handler: s.index},
30		Route{Method: "GET", Pattern: "/static/style.css", Handler: s.stylesheet},
31		Route{Method: "GET", Pattern: "/{owner}/{repo}", Handler: s.repoHome},
32		Route{Method: "GET", Pattern: "/{owner}/{repo}/tree/{ref}/{path...}", Handler: s.tree},
33		Route{Method: "GET", Pattern: "/{owner}/{repo}/blob/{ref}/{path...}", Handler: s.blob},
34		Route{Method: "GET", Pattern: "/{owner}/{repo}/raw/{ref}/{path...}", Handler: s.raw},
35		Route{Method: "GET", Pattern: "/{owner}/{repo}/log", Handler: s.log},
36		Route{Method: "GET", Pattern: "/{owner}/{repo}/log/{ref}", Handler: s.log},
37		Route{Method: "GET", Pattern: "/{owner}/{repo}/commit/{sha}", Handler: s.commit},
38		Route{Method: "GET", Pattern: "/{owner}/{repo}/refs", Handler: s.refs},
39		Route{Method: "GET", Pattern: "/{owner}/{repo}/archive/{file}", Handler: s.archive},
40	)
41
42	// Account-mode routes (login, web edits) are appended here in M8 —
43	// and only when s.cfg.Web.Mode == "accounts".
44	return routes
45}
46
47func (s *Server) Handler() http.Handler {
48	mux := http.NewServeMux()
49	for _, r := range s.Routes() {
50		mux.HandleFunc(r.Method+" "+r.Pattern, r.Handler)
51	}
52	return mux
53}