all repos

mugit @ e2cbacb

🐮 git server that your cow will love

mugit/internal/handlers/handlers.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
keep handlers naming consistent, 3 months ago
1
package handlers
2
3
import (
4
	"html/template"
5
	"net/http"
6
	"net/url"
7
	"path/filepath"
8
	"strings"
9
	"time"
10
11
	"olexsmir.xyz/mugit/internal/cache"
12
	"olexsmir.xyz/mugit/internal/config"
13
	"olexsmir.xyz/mugit/internal/git"
14
	"olexsmir.xyz/mugit/internal/humanize"
15
	"olexsmir.xyz/mugit/web"
16
)
17
18
type handlers struct {
19
	c *config.Config
20
	t *template.Template
21
22
	repoListCache cache.Cacher[[]repoList]
23
	readmeCache   cache.Cacher[template.HTML]
24
	diffCache     cache.Cacher[*git.NiceDiff]
25
}
26
27
func InitRoutes(cfg *config.Config) http.Handler {
28
	tmpls := template.Must(template.New("").
29
		Funcs(templateFuncs).
30
		ParseFS(web.TemplatesFS, "*"))
31
	h := handlers{
32
		cfg, tmpls,
33
		cache.NewInMemory[[]repoList](cfg.Cache.HomePage),
34
		cache.NewInMemory[template.HTML](cfg.Cache.Readme),
35
		cache.NewInMemory[*git.NiceDiff](cfg.Cache.Diff),
36
	}
37
38
	mux := http.NewServeMux()
39
	mux.HandleFunc("GET /", h.indexHandler)
40
	mux.HandleFunc("GET /index.xml", h.indexFeedHandler)
41
	mux.HandleFunc("GET /static/{file}", h.serveStaticHandler)
42
	mux.HandleFunc("GET /{name}", h.repoIndexHandler)
43
	mux.HandleFunc("GET /{name}/info/refs", h.infoRefsHandler)
44
	mux.HandleFunc("POST /{name}/git-upload-pack", h.uploadPackHandler)
45
	mux.HandleFunc("POST /{name}/git-receive-pack", h.receivePackHandler)
46
	mux.HandleFunc("GET /{name}/feed/{$}", h.repoFeedHandler)
47
	mux.HandleFunc("GET /{name}/tree/{ref}/{rest...}", h.repoTreeHandler)
48
	mux.HandleFunc("GET /{name}/blob/{ref}/{rest...}", h.fileContentsHandler)
49
	mux.HandleFunc("GET /{name}/log/{ref}", h.logHandler)
50
	mux.HandleFunc("GET /{name}/commit/{ref}", h.commitHandler)
51
	mux.HandleFunc("GET /{name}/refs/{$}", h.refsHandler)
52
	mux.HandleFunc("GET /{name}/archive/{ref}", h.archiveHandler)
53
54
	handler := h.recoverMiddleware(mux)
55
	return h.loggingMiddleware(handler)
56
}
57
58
func (h *handlers) serveStaticHandler(w http.ResponseWriter, r *http.Request) {
59
	f := filepath.Clean(r.PathValue("file"))
60
	http.ServeFileFS(w, r, web.StaticFS, f)
61
}
62
63
// parseRef parses url encoded ref name.
64
// If it fails it falls back to raw provided value.
65
func (h handlers) parseRef(name string) string {
66
	ref, err := url.PathUnescape(name)
67
	if err != nil {
68
		return name
69
	}
70
	return ref
71
}
72
73
var templateFuncs = template.FuncMap{
74
	"humanizeRelTime": func(t time.Time) string { return humanize.Time(t) },
75
	"humanizeTime":    func(t time.Time) string { return t.Format("2006-01-02 15:04:05 MST") },
76
	"urlencode":       func(s string) string { return url.PathEscape(s) },
77
	"commitSummary": func(s string) string {
78
		before, after, found := strings.Cut(s, "\n")
79
		first := strings.TrimSuffix(before, "\r")
80
		if !found {
81
			return first
82
		}
83
84
		if strings.Contains(after, "\n") {
85
			return first + "..."
86
		}
87
88
		return first
89
	},
90
}