internal/httpd/routes.go

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

119 lines · 5448 bytes

  1package httpd
  2
  3import (
  4	"fmt"
  5	"net"
  6	"net/http"
  7	"strings"
  8)
  9
 10// Route is one entry in the explicit route table. The view-only guarantee is
 11// structural: Handler() consults web.mode when building the table, and the
 12// route test asserts no mutating route exists in view_only mode.
 13type Route struct {
 14	Method  string
 15	Pattern string // without method prefix
 16	// Mutating marks routes that can change server state. The git transport
 17	// POSTs are not mutating: upload-pack is a pure read, and the
 18	// receive-pack endpoint is a static refusal that writes nothing.
 19	Mutating bool
 20	Handler  http.HandlerFunc
 21}
 22
 23// Routes returns the route table for the configured web.mode.
 24func (s *Server) Routes() []Route {
 25	// Git smart transport (anonymous, public repos only).
 26	routes := []Route{
 27		{Method: "GET", Pattern: "/{owner}/{repo}/info/refs", Handler: s.infoRefs},
 28		{Method: "POST", Pattern: "/{owner}/{repo}/git-upload-pack", Handler: s.uploadPack},
 29		{Method: "POST", Pattern: "/{owner}/{repo}/git-receive-pack", Handler: s.receivePackRefusal},
 30	}
 31
 32	// Web UI, read-only. These exist in every mode.
 33	routes = append(routes,
 34		Route{Method: "GET", Pattern: "/{$}", Handler: s.index},
 35		Route{Method: "GET", Pattern: "/explore", Handler: s.explore},
 36		Route{Method: "GET", Pattern: "/static/style.css", Handler: s.stylesheet},
 37		Route{Method: "GET", Pattern: "/favicon.svg", Handler: s.favicon},
 38		Route{Method: "GET", Pattern: "/{owner}", Handler: s.ownerPage},
 39		Route{Method: "GET", Pattern: "/{owner}/{repo}", Handler: s.repoHome},
 40		Route{Method: "GET", Pattern: "/{owner}/{repo}/tree/{ref}/{path...}", Handler: s.tree},
 41		Route{Method: "GET", Pattern: "/{owner}/{repo}/blob/{ref}/{path...}", Handler: s.blob},
 42		Route{Method: "GET", Pattern: "/{owner}/{repo}/blame/{ref}/{path...}", Handler: s.blame},
 43		Route{Method: "GET", Pattern: "/{owner}/{repo}/search", Handler: s.search},
 44		Route{Method: "GET", Pattern: "/{owner}/{repo}/milestones", Handler: s.milestones},
 45		Route{Method: "GET", Pattern: "/{owner}/{repo}/raw/{ref}/{path...}", Handler: s.raw},
 46		Route{Method: "GET", Pattern: "/{owner}/{repo}/log", Handler: s.log},
 47		Route{Method: "GET", Pattern: "/{owner}/{repo}/log/{ref}", Handler: s.log},
 48		Route{Method: "GET", Pattern: "/{owner}/{repo}/commit/{sha}", Handler: s.commit},
 49		Route{Method: "GET", Pattern: "/{owner}/{repo}/refs", Handler: s.refs},
 50		Route{Method: "GET", Pattern: "/{owner}/{repo}/archive/{file}", Handler: s.archive},
 51		Route{Method: "GET", Pattern: "/{owner}/{repo}/issues", Handler: s.issues},
 52		Route{Method: "GET", Pattern: "/{owner}/{repo}/issues/{n}", Handler: s.issue},
 53		Route{Method: "GET", Pattern: "/{owner}/{repo}/mrs", Handler: s.mrs},
 54		Route{Method: "GET", Pattern: "/{owner}/{repo}/mrs/{n}", Handler: s.mr},
 55	)
 56
 57	// The JSON API is its own opt-in surface, independent of web.mode.
 58	if s.cfg.API.Enabled {
 59		routes = append(routes,
 60			Route{Method: "POST", Pattern: "/api/v1/cmd", Mutating: true, Handler: s.apiCmd},
 61		)
 62	}
 63
 64	// Account-mode routes exist only when web.mode = "accounts". In
 65	// view_only they are never registered — the structural guarantee.
 66	if s.cfg.Web.Mode == "accounts" {
 67		routes = append(routes,
 68			Route{Method: "GET", Pattern: "/login", Handler: s.login, Mutating: true}, // consumes a one-time token
 69			Route{Method: "POST", Pattern: "/logout", Mutating: true,
 70				Handler: s.checkOrigin(s.logout)},
 71			Route{Method: "GET", Pattern: "/new", Handler: s.requireUser(s.newRepoForm)},
 72			Route{Method: "POST", Pattern: "/new", Mutating: true,
 73				Handler: s.checkOrigin(s.requireUser(s.newRepoSubmit))},
 74			Route{Method: "GET", Pattern: "/{owner}/{repo}/issues/new",
 75				Handler: s.requireUser(s.issueCreateForm)},
 76			Route{Method: "POST", Pattern: "/{owner}/{repo}/issues/new", Mutating: true,
 77				Handler: s.checkOrigin(s.requireUser(s.issueCreateSubmit))},
 78			Route{Method: "POST", Pattern: "/{owner}/{repo}/issues/{n}/comment", Mutating: true,
 79				Handler: s.checkOrigin(s.requireUser(s.issueCommentSubmit))},
 80			Route{Method: "POST", Pattern: "/{owner}/{repo}/mrs/{n}/comment", Mutating: true,
 81				Handler: s.checkOrigin(s.requireUser(s.mrCommentSubmit))},
 82			Route{Method: "GET", Pattern: "/{owner}/{repo}/edit/{ref}/{path...}",
 83				Handler: s.requireUser(s.editForm)},
 84			Route{Method: "POST", Pattern: "/{owner}/{repo}/edit/{ref}/{path...}", Mutating: true,
 85				Handler: s.checkOrigin(s.requireUser(s.editSubmit))},
 86		)
 87	}
 88	return routes
 89}
 90
 91func (s *Server) Handler() http.Handler {
 92	mux := http.NewServeMux()
 93	for _, r := range s.Routes() {
 94		mux.HandleFunc(r.Method+" "+r.Pattern, r.Handler)
 95	}
 96	if len(s.cfg.GoImport) == 0 {
 97		return mux
 98	}
 99	// Vanity Go modules: ?go-get=1 requests under a configured module
100	// path answer with the go-import meta tag before normal routing.
101	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
102		if r.URL.Query().Get("go-get") == "1" {
103			host := r.Host
104			if h, _, err := net.SplitHostPort(host); err == nil {
105				host = h
106			}
107			requested := host + strings.TrimSuffix(r.URL.Path, "/")
108			for module, repo := range s.cfg.GoImport {
109				if requested == module || strings.HasPrefix(requested, module+"/") {
110					w.Header().Set("Content-Type", "text/html; charset=utf-8")
111					fmt.Fprintf(w, `<!DOCTYPE html><html><head><meta name="go-import" content="%s git %s/%s.git"></head><body>%s</body></html>`,
112						module, s.cfg.Server.SiteURL, repo, module)
113					return
114				}
115			}
116		}
117		mux.ServeHTTP(w, r)
118	})
119}