legit/routes/handler.go

54 lines
1.5 KiB
Go
Raw Normal View History

2022-12-11 05:52:47 +00:00
package routes
import (
2022-12-12 15:23:58 +00:00
"net/http"
"html/template"
2022-12-12 15:23:58 +00:00
2022-12-19 03:32:23 +00:00
"git.icyphox.sh/legit/config"
2022-12-11 05:52:47 +00:00
"github.com/alexedwards/flow"
)
2022-12-14 16:10:01 +00:00
// Checks for gitprotocol-http(5) specific smells; if found, passes
2022-12-14 15:44:34 +00:00
// the request on to the git http service, else render the web frontend.
2022-12-24 09:27:44 +00:00
func (d *deps) Multiplex(w http.ResponseWriter, r *http.Request) {
2022-12-14 15:44:34 +00:00
path := flow.Param(r.Context(), "...")
2022-12-14 16:10:01 +00:00
if r.URL.RawQuery == "service=git-receive-pack" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("no pushing allowed!"))
return
}
2022-12-24 09:27:44 +00:00
if path == "info/refs" &&
r.URL.RawQuery == "service=git-upload-pack" &&
r.Method == "GET" {
d.InfoRefs(w, r)
2022-12-14 16:10:01 +00:00
} else if path == "git-upload-pack" && r.Method == "POST" {
2022-12-24 09:27:44 +00:00
d.UploadPack(w, r)
2022-12-14 15:44:34 +00:00
} else if r.Method == "GET" {
2022-12-24 09:27:44 +00:00
d.RepoIndex(w, r)
2022-12-14 15:44:34 +00:00
}
}
func Handlers(c *config.Config, t *template.Template) *flow.Mux {
2022-12-11 05:52:47 +00:00
mux := flow.New()
d := deps{c, t}
2022-12-12 15:23:58 +00:00
mux.NotFound = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
d.Write404(w)
})
mux.HandleFunc("/", d.Index, "GET")
mux.HandleFunc("/static/:file", d.ServeStatic, "GET")
2022-12-24 09:27:44 +00:00
mux.HandleFunc("/:name", d.Multiplex, "GET", "POST")
2022-12-11 08:48:39 +00:00
mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET")
mux.HandleFunc("/:name/blob/raw/:ref/...", d.ServeStaticInRepo, "GET")
2022-12-11 08:48:39 +00:00
mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")
2022-12-11 15:47:04 +00:00
mux.HandleFunc("/:name/log/:ref", d.Log, "GET")
2022-12-12 11:47:49 +00:00
mux.HandleFunc("/:name/commit/:ref", d.Diff, "GET")
2022-12-12 16:28:47 +00:00
mux.HandleFunc("/:name/refs", d.Refs, "GET")
2022-12-24 09:27:44 +00:00
mux.HandleFunc("/:name/...", d.Multiplex, "GET", "POST")
2022-12-14 15:44:34 +00:00
2022-12-11 05:52:47 +00:00
return mux
}