all repos

mugit @ 7a05bd07557cf1d2e05e5d47ff0b5c9efcc18d01

🐮 git server that your cow will love

mugit/internal/handlers/handles.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
package handlers

import (
	"html/template"
	"net/http"
	"path/filepath"

	"olexsmir.xyz/mugit/internal/config"
)

type handlers struct {
	c *config.Config
	t *template.Template
}

func InitRoutes(cfg *config.Config) *http.ServeMux {
	tmpls := template.Must(template.ParseGlob(
		filepath.Join(cfg.Meta.TemplatesDir, "*"),
	))
	h := handlers{cfg, tmpls}

	mux := http.NewServeMux()
	mux.HandleFunc("GET /", h.index)

	return mux
}

// multiplex if request smells like gitprotocol-http(5) passes it  to the git
// http service renders templates.
func (h *handlers) multiplex(w http.ResponseWriter, r *http.Request) {
	if r.URL.RawQuery == "service=git-receive-pack" {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte("http pushing isn't supported"))
		return
	}

	path := r.PathValue("rest")
	if path == "info/refs" &&
		r.URL.RawQuery == "service=git-upload-pack" &&
		r.Method == "GET" {
		h.infoRefs(w, r)
	} else if path == "git-upload-pack" && r.Method == "POST" {
		h.uploadPack(w, r)
	} else if r.Method == "GET" {
		h.repoIndex(w, r)
	}
}