internal/httpd/routes.go

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

55 lines · 2438 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		Route{Method: "GET", Pattern: "/{owner}/{repo}/issues", Handler: s.issues},
41		Route{Method: "GET", Pattern: "/{owner}/{repo}/issues/{n}", Handler: s.issue},
42	)
43
44	// Account-mode routes (login, web edits) are appended here in M8 —
45	// and only when s.cfg.Web.Mode == "accounts".
46	return routes
47}
48
49func (s *Server) Handler() http.Handler {
50	mux := http.NewServeMux()
51	for _, r := range s.Routes() {
52		mux.HandleFunc(r.Method+" "+r.Pattern, r.Handler)
53	}
54	return mux
55}