Skip to content

Commit

Permalink
Merge pull request #7 from vvatanabe/issue/6/event-dispatcher-for-ser…
Browse files Browse the repository at this point in the history
…vice-rpc

#6 add event emitter for prepare run service rpc
  • Loading branch information
vvatanabe authored Oct 1, 2017
2 parents 6456075 + 4b81e07 commit 628ca17
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 18 deletions.
12 changes: 6 additions & 6 deletions addon/archivehandler/archivehandler_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package archivehandler

import (
"testing"
"os/exec"
"net/http/httptest"
"io/ioutil"
"path"
"net/http/httptest"
"os"
"os/exec"
"path"
"testing"

"github.com/vvatanabe/go-git-http-transfer/githttptransfer"
)
Expand Down Expand Up @@ -82,12 +82,12 @@ func Test_it_should_download_archive_repository(t *testing.T) {
return
}

if _, err := execCmd(destDir, "wget", "-O-", remoteRepoUrl + "/archive/master.zip"); err != nil {
if _, err := execCmd(destDir, "wget", "-O-", remoteRepoUrl+"/archive/master.zip"); err != nil {
t.Errorf("execute command error: %s", err.Error())
return
}

if _, err := execCmd(destDir, "wget", "-O-", remoteRepoUrl + "/archive/master.tar"); err != nil {
if _, err := execCmd(destDir, "wget", "-O-", remoteRepoUrl+"/archive/master.tar"); err != nil {
t.Errorf("execute command error: %s", err.Error())
return
}
Expand Down
15 changes: 15 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ func main() {

ght := githttptransfer.New("/data/git", "/usr/bin/git", true, true)

ght.Event.On(githttptransfer.PrepareServiceRpcUpload, func(ctx githttptransfer.Context) error {
log.Println("prepare run service rpc upload.")
return nil
})

ght.Event.On(githttptransfer.PrepareServiceRpcReceive, func(ctx githttptransfer.Context) error {
log.Println("prepare run service rpc receive.")
return nil
})

ght.Event.On(githttptransfer.AfterMatchRouting, func(ctx githttptransfer.Context) error {
log.Println("after match routing.")
return nil
})

// You can add some custom route.
ght.AddRoute(githttptransfer.NewRoute(
http.MethodGet,
Expand Down
63 changes: 55 additions & 8 deletions githttptransfer/githttptransfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
getIdxFile = regexp.MustCompile("(.*?)/objects/pack/pack-[0-9a-f]{40}\\.idx$")
)

func New(gitRootPath string, gitBinPath string, uploadPack bool, receivePack bool) *GitHttpTransfer {
func New(gitRootPath, gitBinPath string, uploadPack, receivePack bool) *GitHttpTransfer {

if gitRootPath == "" {
cwd, err := os.Getwd()
Expand All @@ -39,8 +39,9 @@ func New(gitRootPath string, gitBinPath string, uploadPack bool, receivePack boo

git := newGit(gitRootPath, gitBinPath, uploadPack, receivePack)
router := newRouter()
event := newEvent()

gsh := &GitHttpTransfer{git, router}
gsh := &GitHttpTransfer{git, router, event}
gsh.AddRoute(NewRoute(http.MethodPost, serviceRpcUpload, gsh.serviceRpcUpload))
gsh.AddRoute(NewRoute(http.MethodPost, serviceRpcReceive, gsh.serviceRpcReceive))
gsh.AddRoute(NewRoute(http.MethodGet, getInfoRefs, gsh.getInfoRefs))
Expand All @@ -58,6 +59,7 @@ func New(gitRootPath string, gitBinPath string, uploadPack bool, receivePack boo
type GitHttpTransfer struct {
Git *git
router *router
Event Event
}

func (ght *GitHttpTransfer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
Expand All @@ -72,24 +74,29 @@ func (ght *GitHttpTransfer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
return
}

if !ght.Git.Exists(repoPath) {
RenderNotFound(rw)
ctx := NewContext(rw, r, repoPath, filePath)

if err := ght.Event.emit(AfterMatchRouting, ctx); err != nil {
RenderInternalServerError(ctx.Response().Writer)
return
}

ctx := NewContext(rw, r, repoPath, filePath)
if !ght.Git.Exists(ctx.RepoPath()) {
RenderNotFound(ctx.Response().Writer)
return
}

if err := handler(ctx); err != nil {
if os.IsNotExist(err) {
RenderNotFound(rw)
RenderNotFound(ctx.Response().Writer)
return
}
switch err.(type) {
case *NoAccessError:
RenderNoAccess(rw)
RenderNoAccess(ctx.Response().Writer)
return
}
RenderInternalServerError(rw)
RenderInternalServerError(ctx.Response().Writer)
}
}

Expand All @@ -114,15 +121,55 @@ const (

type HandlerFunc func(ctx Context) error

func newEvent() Event {
return &event{map[EventKey]HandlerFunc{}}
}

type EventKey string

const (
PrepareServiceRpcUpload EventKey = "prepare-service-rpc-upload"
PrepareServiceRpcReceive EventKey = "prepare-service-rpc-receive"
AfterMatchRouting EventKey = "after-match-routing"
)

type Event interface {
emit(evt EventKey, ctx Context) error
On(evt EventKey, listener HandlerFunc)
}

type event struct {
listeners map[EventKey]HandlerFunc
}

func (e *event) emit(evt EventKey, ctx Context) error {
v, ok := e.listeners[evt]
if ok {
return v(ctx)
}
return nil
}

func (e *event) On(evt EventKey, listener HandlerFunc) {
e.listeners[evt] = listener
}

func (ght *GitHttpTransfer) serviceRpcUpload(ctx Context) error {
if err := ght.Event.emit(PrepareServiceRpcUpload, ctx); err != nil {
return err
}
return ght.serviceRpc(ctx, uploadPack)
}

func (ght *GitHttpTransfer) serviceRpcReceive(ctx Context) error {
if err := ght.Event.emit(PrepareServiceRpcReceive, ctx); err != nil {
return err
}
return ght.serviceRpc(ctx, receivePack)
}

func (ght *GitHttpTransfer) serviceRpc(ctx Context, rpc string) error {

res, req, repoPath := ctx.Response(), ctx.Request(), ctx.RepoPath()

if !ght.Git.HasAccess(req, rpc, true) {
Expand Down
19 changes: 15 additions & 4 deletions githttptransfer/githttptransfer_end_to_end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path"
"regexp"
"testing"
"os"
"strings"
"testing"
)

type EndToEndTestParams struct {
Expand Down Expand Up @@ -42,12 +42,24 @@ func setupEndToEndTest(t *testing.T) error {
endToEndTestParams.repoName = "e2e_test.git"

endToEndTestParams.ght = New(endToEndTestParams.gitRootPath, endToEndTestParams.gitBinPath, true, true)
endToEndTestParams.ght.Event.On(PrepareServiceRpcUpload, func(ctx Context) error {
t.Log("prepare run service rpc upload.")
return nil
})
endToEndTestParams.ght.Event.On(PrepareServiceRpcReceive, func(ctx Context) error {
t.Log("prepare run service rpc receive.")
return nil
})
endToEndTestParams.ght.Event.On(AfterMatchRouting, func(ctx Context) error {
t.Log("after match routing.")
return nil
})

endToEndTestParams.ts = httptest.NewServer(endToEndTestParams.ght)

endToEndTestParams.absRepoPath = endToEndTestParams.ght.Git.GetAbsolutePath(endToEndTestParams.repoName)
os.Mkdir(endToEndTestParams.absRepoPath, os.ModeDir)


if _, err := execCmd(endToEndTestParams.absRepoPath, "git", "init", "--bare", "--shared"); err != nil {
t.Errorf("execute command error: %s", err.Error())
return err
Expand Down Expand Up @@ -310,4 +322,3 @@ func Test_End_To_End_it_should_succeed_request_to_get_info_packs(t *testing.T) {
}

}

0 comments on commit 628ca17

Please sign in to comment.