routes/handler.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | package routes
import (
"io/fs"
"log"
"net/http"
"path"
"path/filepath"
"github.com/dimfeld/httptreemux/v5"
"github.com/fsnotify/fsnotify"
"alin.ovh/elgit/config"
"alin.ovh/elgit/templates"
)
var ErrNotAllowed = templates.Error{
Code: http.StatusMethodNotAllowed,
Message: "that's not possible.",
}
// Checks for gitprotocol-http(5) specific smells; if found, passes
// the request on to the git http service, else render the web frontend.
func (d *deps) Multiplex(w http.ResponseWriter, r *http.Request, params map[string]string) {
rest := params["rest"]
switch params["name"] {
case "info", "git-upload-pack":
rest = path.Join(params["name"], params["rest"])
params["name"] = params["category"]
params["category"] = ""
}
if d.isNotAllowed(path.Join(params["category"], params["name"])) {
d.Write404(w)
return
}
if r.URL.RawQuery == "service=git-receive-pack" {
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write([]byte("no pushing allowed!"))
if err != nil {
log.Println(err)
d.Write500(w)
return
}
return
}
switch r.Method {
case http.MethodGet:
switch {
case rest == "info/refs" &&
r.URL.RawQuery == "service=git-upload-pack":
d.InfoRefs(w, r, params)
default:
d.RepoIndex(w, r, params)
}
case http.MethodPost:
switch rest {
case "git-upload-pack":
d.UploadPack(w, r, params)
default:
d.WriteError(w, ErrNotAllowed)
}
default:
d.WriteError(w, ErrNotAllowed)
}
}
func Handlers(c *config.Config, staticFiles fs.FS) *httptreemux.TreeMux {
mux := httptreemux.New()
plist := filepath.Join(c.Repo.Root, "projects.list")
projects, err := ReadProjectsList(plist)
if err != nil {
log.Fatal(err)
}
d := deps{
c,
projects,
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
err = watcher.Add(plist)
if err != nil {
log.Fatal(err)
}
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
continue
}
if event.Op.Has(fsnotify.Write) {
d.projects, err = ReadProjectsList(plist)
if err != nil {
log.Println(err)
}
}
case err, ok := <-watcher.Errors:
if !ok {
continue
}
log.Println("error:", err)
}
}
}()
mux.NotFoundHandler = func(w http.ResponseWriter, _ *http.Request) {
d.Write404(w)
}
mux.GET("/", d.Index)
mux.NewGroup("/static/").
UsingContext().
Handler("GET", "/*", http.FileServerFS(staticFiles))
d.mountRepoPaths(mux.NewGroup("/:name"))
d.mountRepoPaths(mux.NewGroup("/:category/:name"))
return mux
}
func (d deps) mountRepoPaths(group *httptreemux.Group) {
group.GET("/tree/:ref/*rest", d.RepoTree)
group.GET("/blob/:ref/*rest", d.FileContent)
group.GET("/tree/:ref/", d.RepoTree)
group.GET("/log/:ref/", d.Log)
group.GET("/archive/:file", d.Archive)
group.GET("/commit/:ref", d.Diff)
group.GET("/commit/:ref/*file", d.FileDiff)
group.GET("/refs/", d.Refs)
group.GET("", d.Multiplex)
group.POST("", d.Multiplex)
group.GET("/*rest", d.Multiplex)
group.POST("/*rest", d.Multiplex)
}
|