git.go
58 lines1
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
package controllers
import (
"net/http"
"strings"
"congo.gg/pkg/application"
"github.com/sosedoff/gitkit"
)
// GitController serves the congo repo over read-only git HTTP protocol.
type GitController struct {
application.BaseController
server *gitkit.Server
}
// Git returns the controller name and instance.
func Git() (string, *GitController) {
return "git", &GitController{}
}
// Setup registers the git smart HTTP routes.
func (c *GitController) Setup(app *application.App) {
c.BaseController.Setup(app)
c.server = gitkit.New(gitkit.Config{
Dir: "/opt",
AutoCreate: false,
})
http.HandleFunc("GET /git/congo.git/", c.handle)
http.HandleFunc("POST /git/congo.git/", c.handle)
http.HandleFunc("GET /git/congo.git/info/refs", c.handle)
http.HandleFunc("POST /git/congo.git/git-upload-pack", c.handle)
}
// handle proxies to gitkit, blocking push operations.
func (c *GitController) handle(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("service") == "git-receive-pack" {
http.Error(w, "Push not allowed", http.StatusForbidden)
return
}
if strings.HasSuffix(r.URL.Path, "git-receive-pack") {
http.Error(w, "Push not allowed", http.StatusForbidden)
return
}
// gitkit expects paths like /congo.git/...
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/git")
c.server.ServeHTTP(w, r)
}
// Handle returns a request-scoped controller instance.
func (c GitController) Handle(r *http.Request) application.Controller {
c.Request = r
return &c
}