Skip to content

Commit

Permalink
temp handler middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Son Luong Ngoc committed Jul 12, 2021
1 parent 749cf04 commit 07f1136
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
5 changes: 4 additions & 1 deletion go.mod
Expand Up @@ -2,4 +2,7 @@ module github.com/sluongng/git-cache

go 1.16

require github.com/matryer/is v1.4.0 // indirect
require (
github.com/google/gitprotocolio v0.0.0-20210704173409-b5a56823ae52
github.com/matryer/is v1.4.0
)
2 changes: 2 additions & 0 deletions go.sum
@@ -1,2 +1,4 @@
github.com/google/gitprotocolio v0.0.0-20210704173409-b5a56823ae52 h1:/a887PZoXM9aLYwXS2ufq+Gnr5KUg5gm8gBoxKjnQuo=
github.com/google/gitprotocolio v0.0.0-20210704173409-b5a56823ae52/go.mod h1:O2KL6wjnwAu7+dPSZhhrjp35gFdyoHlP/f6dhc9YupY=
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
41 changes: 40 additions & 1 deletion handler.go
@@ -1,10 +1,15 @@
package main

import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"

"github.com/google/gitprotocolio"
)

const (
Expand Down Expand Up @@ -38,7 +43,41 @@ func (s *server) ReceivePackHandler() http.HandlerFunc {
// - git-archive
func (s *server) UploadPackHandler() http.HandlerFunc {
log.Println("UploadPackHandler")
return s.proxyHandler()
return func(w http.ResponseWriter, r *http.Request) {
proxyHandler := s.proxyHandler()

// Middleware layer
b, err := io.ReadAll(r.Body)
if err != nil {
log.Fatalln("Unable to read body: %w", err)
}
r.Body = io.NopCloser(bytes.NewReader(b))

scanner := gitprotocolio.NewProtocolV2Request(bytes.NewReader(b))
for {
if ok := scanner.Scan(); !ok {
if scanner.Err() != nil {
log.Printf("Unable to scan request: %s\n", scanner.Err())
}

break
}

c := scanner.Chunk()

data, err := json.MarshalIndent(c, "", " ")
if err != nil {
log.Fatalln("Unable to unmarshal chunk: %w", err)
}
log.Printf("chunk: %s", string(data))

if c.EndRequest {
break
}
}

proxyHandler(w, r)
}
}

func (s *server) proxyHandler() http.HandlerFunc {
Expand Down

0 comments on commit 07f1136

Please sign in to comment.