Home

ugit @ba126a68908f21449d07eb2898587676322e577e - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / internal / http / middleware.go
- 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
package http

import (
	"context"
	"errors"
	"io/fs"
	"net/http"

	"github.com/go-chi/chi/v5"
	"go.jolheiser.com/ugit/internal/git"
	"go.jolheiser.com/ugit/internal/http/httperr"
)

type ugitCtxKey string

var repoCtxKey = ugitCtxKey("repo")

func (rh repoHandler) repoMiddleware(next http.Handler) http.Handler {
	return httperr.Handler(func(w http.ResponseWriter, r *http.Request) error {
		repoName := chi.URLParam(r, "repo")
		repo, err := git.NewRepo(rh.s.RepoDir, repoName)
		if err != nil {
			httpErr := http.StatusInternalServerError
			if errors.Is(err, fs.ErrNotExist) {
				httpErr = http.StatusNotFound
			}
			return httperr.Status(err, httpErr)
		}
		if repo.Meta.Private {
			return httperr.Status(errors.New("could not get git repo"), http.StatusNotFound)
		}
		r = r.WithContext(context.WithValue(r.Context(), repoCtxKey, repo))
		next.ServeHTTP(w, r)
		return nil
	})
}