Skip to content

Commit ed12cab

Browse files
author
Jesse Ezell
committed
Add support for custom repo resolvers
1 parent 1d9485b commit ed12cab

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

auth/auth.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type AuthInfo struct {
1717
// But could also be: "some_repo.git"
1818
Repo string
1919

20+
Request *http.Request
21+
2022
// Are we pushing or fetching ?
2123
Push bool
2224
Fetch bool
@@ -40,6 +42,7 @@ func Authenticator(authf func(AuthInfo) (bool, error)) func(http.Handler) http.H
4042
info := AuthInfo{
4143
Username: auth.Name,
4244
Password: auth.Pass,
45+
Request: req,
4346
Repo: repoName(req.URL.Path),
4447
Push: isPush(req),
4548
Fetch: isFetch(req),

githttp.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ import (
1010
"strings"
1111
)
1212

13+
type ResolveInfo struct {
14+
Path string
15+
Request *http.Request
16+
}
17+
18+
// Resolve a URL path to a repo location
19+
type PathResolver func(info ResolveInfo) (string, error)
20+
1321
type GitHttp struct {
1422
// Root directory to serve repos from
1523
ProjectRoot string
@@ -23,6 +31,8 @@ type GitHttp struct {
2331

2432
// Event handling functions
2533
EventHandler func(ev Event)
34+
35+
PathResolver PathResolver
2636
}
2737

2838
// Implement the http.Handler interface
@@ -216,7 +226,16 @@ func sendFile(content_type string, hr HandlerReq) error {
216226
return nil
217227
}
218228

219-
func (g *GitHttp) getGitDir(file_path string) (string, error) {
229+
func (g *GitHttp) getGitDir(file_path string, req *http.Request) (string, error) {
230+
231+
var err error
232+
if g.PathResolver != nil {
233+
file_path, err = g.PathResolver(ResolveInfo{Path: file_path, Request: req})
234+
if err != nil {
235+
return "", err
236+
}
237+
}
238+
220239
root := g.ProjectRoot
221240

222241
if root == "" {

routing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (g *GitHttp) requestHandler(w http.ResponseWriter, r *http.Request) {
9090
file := strings.Replace(r.URL.Path, repo+"/", "", 1)
9191

9292
// Resolve directory
93-
dir, err := g.getGitDir(repo)
93+
dir, err := g.getGitDir(repo, r)
9494

9595
// Repo not found on disk
9696
if err != nil {

0 commit comments

Comments
 (0)