Skip to content

Commit d4ccbde

Browse files
committed
#21 change handler function interface
1 parent 3e6e2dd commit d4ccbde

File tree

9 files changed

+160
-182
lines changed

9 files changed

+160
-182
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ func main() {
6262
}
6363
```
6464
You can add optional parameters to constructor function.
65-
* `WithoutUploadPack`: Disabled `git-upload-pack` command.
66-
* `WithoutReceivePack`: Disabled `git-receive-pack` command.
67-
* `WithoutDumbProto`: Disabled `dumb protocol` handling.
65+
* `WithoutUploadPack` : Disable `git-upload-pack` command.
66+
* `WithoutReceivePack`: Disable `git-receive-pack` command.
67+
* `WithoutDumbProto` : Disable `dumb protocol` handling.
6868
```go
6969
ght, err := githttptransfer.New(
7070
"/data/git",
@@ -88,13 +88,12 @@ func main() {
8888
ght.Router.Add(githttptransfer.NewRoute(
8989
http.MethodGet,
9090
regexp.MustCompile("(.*?)/hello$"),
91-
func(ctx githttptransfer.Context) error {
91+
func(ctx githttptransfer.Context) {
9292
resp, req := ctx.Response(), ctx.Request()
9393
rp, fp := ctx.RepoPath(), ctx.FilePath()
9494
fmt.Fprintf(resp.Writer,
9595
"Hi there. URI: %s, RepoPath: %s, FilePath: %s",
9696
req.URL.RequestURI(), rp, fp)
97-
return nil
9897
},
9998
))
10099

addon/archivehandler/archivehandler.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type ArchiveHandler struct {
2323
*githttptransfer.GitHTTPTransfer
2424
}
2525

26-
func (ght *ArchiveHandler) HandlerFunc(ctx githttptransfer.Context) error {
26+
func (ght *ArchiveHandler) HandlerFunc(ctx githttptransfer.Context) {
2727

2828
res, repoPath, filePath := ctx.Response(), ctx.RepoPath(), ctx.FilePath()
2929

@@ -38,23 +38,25 @@ func (ght *ArchiveHandler) HandlerFunc(ctx githttptransfer.Context) error {
3838

3939
stdout, err := cmd.StdoutPipe()
4040
if err != nil {
41-
return err
41+
githttptransfer.RenderInternalServerError(res.Writer)
42+
return
4243
}
4344
defer stdout.Close()
4445

4546
if err := cmd.Start(); err != nil {
46-
return err
47+
githttptransfer.RenderInternalServerError(res.Writer)
48+
return
4749
}
4850

4951
res.SetContentType("application/octet-stream")
5052
res.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
5153
res.Header().Add("Content-Transfer-Encoding", "binary")
5254

5355
if _, err := res.Copy(stdout); err != nil {
54-
return err
56+
githttptransfer.RenderInternalServerError(res.Writer)
57+
return
5558
}
5659
if err := cmd.Wait(); err != nil {
57-
return err
60+
githttptransfer.RenderInternalServerError(res.Writer)
5861
}
59-
return nil
6062
}

example/main.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,28 @@ func main() {
2525
return
2626
}
2727

28-
ght.Event.On(githttptransfer.PrepareServiceRPCUpload, func(ctx githttptransfer.Context) error {
28+
ght.Event.On(githttptransfer.PrepareServiceRPCUpload, func(ctx githttptransfer.Context) {
2929
// prepare run service rpc upload.
30-
return nil
3130
})
3231

33-
ght.Event.On(githttptransfer.PrepareServiceRPCReceive, func(ctx githttptransfer.Context) error {
32+
ght.Event.On(githttptransfer.PrepareServiceRPCReceive, func(ctx githttptransfer.Context) {
3433
// prepare run service rpc receive.
35-
return nil
3634
})
3735

38-
ght.Event.On(githttptransfer.AfterMatchRouting, func(ctx githttptransfer.Context) error {
36+
ght.Event.On(githttptransfer.AfterMatchRouting, func(ctx githttptransfer.Context) {
3937
// after match routing.
40-
return nil
4138
})
4239

4340
// You can add some custom route.
4441
ght.Router.Add(githttptransfer.NewRoute(
4542
http.MethodGet,
4643
regexp.MustCompile("(.*?)/hello$"),
47-
func(ctx githttptransfer.Context) error {
44+
func(ctx githttptransfer.Context) {
4845
resp, req := ctx.Response(), ctx.Request()
4946
rp, fp := ctx.RepoPath(), ctx.FilePath()
5047
fmt.Fprintf(resp.Writer,
5148
"Hi there. URI: %s, RepoPath: %s, FilePath: %s",
5249
req.URL.RequestURI(), rp, fp)
53-
return nil
5450
},
5551
))
5652

githttptransfer/error.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,3 @@ func (e *MethodNotAllowedError) Error() string {
2020
return fmt.Sprintf("Method Not Allowed: Method %s, Path %s", e.Method, e.Path)
2121
}
2222

23-
type NoAccessError struct {
24-
Dir string
25-
}
26-
27-
func (e *NoAccessError) Error() string {
28-
return "No Access: " + e.Dir
29-
}

githttptransfer/githttptransfer.go

Lines changed: 68 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,33 @@ var (
2525
getIdxFile = regexp.MustCompile("(.*?)/objects/pack/pack-[0-9a-f]{40}\\.idx$")
2626
)
2727

28-
type gitHTTPTransferOptions struct {
29-
uploadPack bool
28+
type options struct {
29+
uploadPack bool
3030
receivePack bool
31-
dumbProto bool
31+
dumbProto bool
3232
}
3333

34-
type GitHTTPTransferOption func(*gitHTTPTransferOptions)
34+
type Option func(*options)
3535

36-
func WithoutUploadPack() GitHTTPTransferOption {
37-
return func(o *gitHTTPTransferOptions) {
36+
func WithoutUploadPack() Option {
37+
return func(o *options) {
3838
o.uploadPack = false
3939
}
4040
}
4141

42-
func WithoutReceivePack() GitHTTPTransferOption {
43-
return func(o *gitHTTPTransferOptions) {
42+
func WithoutReceivePack() Option {
43+
return func(o *options) {
4444
o.receivePack = false
4545
}
4646
}
4747

48-
func WithoutDumbProto() GitHTTPTransferOption {
49-
return func(o *gitHTTPTransferOptions) {
48+
func WithoutDumbProto() Option {
49+
return func(o *options) {
5050
o.dumbProto = false
5151
}
5252
}
5353

54-
func New(gitRootPath, gitBinPath string, opts ...GitHTTPTransferOption) (*GitHTTPTransfer, error) {
54+
func New(gitRootPath, gitBinPath string, opts ...Option) (*GitHTTPTransfer, error) {
5555

5656
if gitRootPath == "" {
5757
cwd, err := os.Getwd()
@@ -61,7 +61,7 @@ func New(gitRootPath, gitBinPath string, opts ...GitHTTPTransferOption) (*GitHTT
6161
gitRootPath = cwd
6262
}
6363

64-
ghtOpts := &gitHTTPTransferOptions{true, true, true}
64+
ghtOpts := &options{true, true, true}
6565

6666
for _, opt := range opts {
6767
opt(ghtOpts)
@@ -109,28 +109,14 @@ func (ght *GitHTTPTransfer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
109109

110110
ctx := NewContext(rw, r, repoPath, filePath)
111111

112-
if err := ght.Event.emit(AfterMatchRouting, ctx); err != nil {
113-
RenderInternalServerError(ctx.Response().Writer)
114-
return
115-
}
112+
ght.Event.emit(AfterMatchRouting, ctx)
116113

117114
if !ght.Git.Exists(ctx.RepoPath()) {
118115
RenderNotFound(ctx.Response().Writer)
119116
return
120117
}
121118

122-
if err := handler(ctx); err != nil {
123-
if os.IsNotExist(err) {
124-
RenderNotFound(ctx.Response().Writer)
125-
return
126-
}
127-
switch err.(type) {
128-
case *NoAccessError:
129-
RenderNoAccess(ctx.Response().Writer)
130-
return
131-
}
132-
RenderInternalServerError(ctx.Response().Writer)
133-
}
119+
handler(ctx)
134120
}
135121

136122
func (ght *GitHTTPTransfer) matchRouting(method, path string) (repoPath string, filePath string, handler HandlerFunc, err error) {
@@ -144,11 +130,11 @@ func (ght *GitHTTPTransfer) matchRouting(method, path string) (repoPath string,
144130
}
145131

146132
const (
147-
uploadPack string = "upload-pack"
148-
receivePack string = "receive-pack"
133+
uploadPack = "upload-pack"
134+
receivePack = "receive-pack"
149135
)
150136

151-
type HandlerFunc func(ctx Context) error
137+
type HandlerFunc func(ctx Context)
152138

153139
func newEvent() *event {
154140
return &event{map[EventKey]HandlerFunc{}}
@@ -166,38 +152,34 @@ type event struct {
166152
listeners map[EventKey]HandlerFunc
167153
}
168154

169-
func (e *event) emit(evt EventKey, ctx Context) error {
155+
func (e *event) emit(evt EventKey, ctx Context) {
170156
v, ok := e.listeners[evt]
171157
if ok {
172-
return v(ctx)
158+
v(ctx)
173159
}
174-
return nil
175160
}
176161

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

181-
func (ght *GitHTTPTransfer) serviceRPCUpload(ctx Context) error {
182-
if err := ght.Event.emit(PrepareServiceRPCUpload, ctx); err != nil {
183-
return err
184-
}
185-
return ght.serviceRPC(ctx, uploadPack)
166+
func (ght *GitHTTPTransfer) serviceRPCUpload(ctx Context) {
167+
ght.Event.emit(PrepareServiceRPCUpload, ctx)
168+
ght.serviceRPC(ctx, uploadPack)
186169
}
187170

188-
func (ght *GitHTTPTransfer) serviceRPCReceive(ctx Context) error {
189-
if err := ght.Event.emit(PrepareServiceRPCReceive, ctx); err != nil {
190-
return err
191-
}
192-
return ght.serviceRPC(ctx, receivePack)
171+
func (ght *GitHTTPTransfer) serviceRPCReceive(ctx Context) {
172+
ght.Event.emit(PrepareServiceRPCReceive, ctx)
173+
ght.serviceRPC(ctx, receivePack)
193174
}
194175

195-
func (ght *GitHTTPTransfer) serviceRPC(ctx Context, rpc string) error {
176+
func (ght *GitHTTPTransfer) serviceRPC(ctx Context, rpc string) {
196177

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

199180
if !ght.Git.HasAccess(req, rpc, true) {
200-
return &NoAccessError{Dir: ght.Git.GetAbsolutePath(repoPath)}
181+
RenderNoAccess(ctx.Response().Writer)
182+
return
201183
}
202184

203185
var body io.ReadCloser
@@ -206,7 +188,8 @@ func (ght *GitHTTPTransfer) serviceRPC(ctx Context, rpc string) error {
206188
if req.Header.Get("Content-Encoding") == "gzip" {
207189
body, err = gzip.NewReader(req.Body)
208190
if err != nil {
209-
return err
191+
RenderInternalServerError(ctx.Response().Writer)
192+
return
210193
}
211194
} else {
212195
body = req.Body
@@ -220,17 +203,19 @@ func (ght *GitHTTPTransfer) serviceRPC(ctx Context, rpc string) error {
220203

221204
stdin, err := cmd.StdinPipe()
222205
if err != nil {
223-
return err
206+
RenderInternalServerError(ctx.Response().Writer)
207+
return
224208
}
225209

226210
stdout, err := cmd.StdoutPipe()
227211
if err != nil {
228-
return err
212+
RenderInternalServerError(ctx.Response().Writer)
213+
return
229214
}
230215

231-
err = cmd.Start() // could be merged in one statement
232-
if err != nil {
233-
return err
216+
if err = cmd.Start(); err != nil {
217+
RenderInternalServerError(ctx.Response().Writer)
218+
return
234219
}
235220

236221
var wg sync.WaitGroup
@@ -250,23 +235,28 @@ func (ght *GitHTTPTransfer) serviceRPC(ctx Context, rpc string) error {
250235

251236
wg.Wait()
252237

253-
return cmd.Wait()
254-
238+
if err = cmd.Wait(); err != nil {
239+
RenderInternalServerError(ctx.Response().Writer)
240+
return
241+
}
255242
}
256243

257-
func (ght *GitHTTPTransfer) getInfoRefs(ctx Context) error {
244+
func (ght *GitHTTPTransfer) getInfoRefs(ctx Context) {
258245
res, req, repoPath := ctx.Response(), ctx.Request(), ctx.RepoPath()
259246

260247
serviceName := getServiceType(req)
261248
if !ght.Git.HasAccess(req, serviceName, false) {
262249
ght.Git.UpdateServerInfo(repoPath)
263250
res.HdrNocache()
264-
return ght.sendFile("text/plain; charset=utf-8", ctx)
251+
if err := ght.sendFile("text/plain; charset=utf-8", ctx); err != nil {
252+
RenderNotFound(ctx.Response().Writer)
253+
}
265254
}
266255

267256
refs, err := ght.Git.GetInfoRefs(repoPath, serviceName)
268257
if err != nil {
269-
return err
258+
RenderNotFound(ctx.Response().Writer)
259+
return
270260
}
271261

272262
res.HdrNocache()
@@ -275,33 +265,42 @@ func (ght *GitHTTPTransfer) getInfoRefs(ctx Context) error {
275265
res.PktWrite("# service=git-" + serviceName + "\n")
276266
res.PktFlush()
277267
res.Write(refs)
278-
279-
return nil
280268
}
281269

282-
func (ght *GitHTTPTransfer) getInfoPacks(ctx Context) error {
270+
func (ght *GitHTTPTransfer) getInfoPacks(ctx Context) {
283271
ctx.Response().HdrCacheForever()
284-
return ght.sendFile("text/plain; charset=utf-8", ctx)
272+
if err := ght.sendFile("text/plain; charset=utf-8", ctx); err != nil {
273+
RenderNotFound(ctx.Response().Writer)
274+
}
285275
}
286276

287-
func (ght *GitHTTPTransfer) getLooseObject(ctx Context) error {
277+
func (ght *GitHTTPTransfer) getLooseObject(ctx Context) {
288278
ctx.Response().HdrCacheForever()
289-
return ght.sendFile("application/x-git-loose-object", ctx)
279+
if err := ght.sendFile("application/x-git-loose-object", ctx); err != nil {
280+
RenderNotFound(ctx.Response().Writer)
281+
}
290282
}
291283

292-
func (ght *GitHTTPTransfer) getPackFile(ctx Context) error {
284+
func (ght *GitHTTPTransfer) getPackFile(ctx Context) {
293285
ctx.Response().HdrCacheForever()
294-
return ght.sendFile("application/x-git-packed-objects", ctx)
286+
if err := ght.sendFile("application/x-git-packed-objects", ctx); err != nil {
287+
RenderNotFound(ctx.Response().Writer)
288+
}
289+
295290
}
296291

297-
func (ght *GitHTTPTransfer) getIdxFile(ctx Context) error {
292+
func (ght *GitHTTPTransfer) getIdxFile(ctx Context) {
298293
ctx.Response().HdrCacheForever()
299-
return ght.sendFile("application/x-git-packed-objects-toc", ctx)
294+
if err := ght.sendFile("application/x-git-packed-objects-toc", ctx); err != nil {
295+
RenderNotFound(ctx.Response().Writer)
296+
}
300297
}
301298

302-
func (ght *GitHTTPTransfer) getTextFile(ctx Context) error {
299+
func (ght *GitHTTPTransfer) getTextFile(ctx Context) {
303300
ctx.Response().HdrNocache()
304-
return ght.sendFile("text/plain", ctx)
301+
if err := ght.sendFile("text/plain", ctx); err != nil {
302+
RenderNotFound(ctx.Response().Writer)
303+
}
305304
}
306305

307306
func (ght *GitHTTPTransfer) sendFile(contentType string, ctx Context) error {
@@ -314,6 +313,5 @@ func (ght *GitHTTPTransfer) sendFile(contentType string, ctx Context) error {
314313
res.SetContentLength(fmt.Sprintf("%d", fileInfo.Size()))
315314
res.SetLastModified(fileInfo.ModTime().Format(http.TimeFormat))
316315
http.ServeFile(res.Writer, req, fileInfo.AbsolutePath)
317-
318316
return nil
319317
}

0 commit comments

Comments
 (0)