Skip to content

Commit

Permalink
do not use external router
Browse files Browse the repository at this point in the history
  • Loading branch information
cooldarkdryplace committed May 5, 2018
1 parent 98d1595 commit 75cb3ea
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/debugserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func main() {
http.Handle("/", debugserver.API())
http.HandleFunc("/", debugserver.API)

var (
errChan = make(chan error)
Expand Down
82 changes: 62 additions & 20 deletions debugserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,70 @@ import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"io"
"log"
"net"
"net/http"

"github.com/julienschmidt/httprouter"
"strings"
)

var storage = NewStorage()
var emptyResponse = []byte("[]\n")
var errNoID = errors.New("no ID provided")

func API() http.Handler {
router := httprouter.New()

router.DELETE("/buckets/:id", record)
router.GET("/buckets/:id", record)
router.PATCH("/buckets/:id", record)
router.PUT("/buckets/:id", record)
router.POST("/buckets/:id", record)

router.DELETE("/reports/:id", del)
router.GET("/reports/:id", show)
func API(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/buckets/") {
record(w, r)
}

return router
if strings.HasPrefix(r.URL.Path, "/reports/") {
if r.Method == http.MethodDelete {
del(w, r)
}
if r.Method == http.MethodGet {
show(w, r)
}
}
}

type Request struct {
ContentLength int64 `json:"content_length"`
Body string `json:"body"`
Method string `json:"method"`
URL string `json:"url"`
QueryParams string `json:"query_params"`
RemoteIP string `json:"remote_ip"`
Headers map[string][]string `json:"headers"`
}

func record(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("id")
func requestID(path string) (string, error) {
path = strings.TrimPrefix(path, "/")
chunks := strings.Split(path, "/")
if len(chunks) < 2 {
return "", errNoID
}
id := strings.TrimSuffix(chunks[1], "/")
id = strings.TrimSpace(id)

if id == "" {
return "", errNoID
}

return id, nil
}

func record(w http.ResponseWriter, r *http.Request) {
id, err := requestID(r.URL.Path)
if err != nil {
http.Error(w, "no ID provided", http.StatusBadRequest)
return
}

ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Printf("Failed to sptil remote address to IP and Port: %s", err)
}

bodyReader := http.MaxBytesReader(w, r.Body, 1<<20)
defer bodyReader.Close()
Expand All @@ -50,17 +79,24 @@ func record(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
}

storage.Add(id, Request{
RemoteIP: ip,
URL: r.URL.Path,
QueryParams: r.URL.RawQuery,
Method: r.Method,
Headers: r.Header,
Body: base64.StdEncoding.EncodeToString(body.Bytes()),
ContentLength: r.ContentLength,
})
}

func show(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
requests := storage.Get(p.ByName("id"))
func show(w http.ResponseWriter, r *http.Request) {
id, err := requestID(r.URL.Path)
if err != nil {
http.Error(w, "no ID provided", http.StatusBadRequest)
return
}

requests := storage.Get(id)
if requests == nil {
w.Write(emptyResponse)
return
Expand All @@ -74,6 +110,12 @@ func show(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
}
}

func del(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
storage.Del(p.ByName("id"))
func del(w http.ResponseWriter, r *http.Request) {
id, err := requestID(r.URL.Path)
if err != nil {
http.Error(w, "no ID provided", http.StatusBadRequest)
return
}

storage.Del(id)
}
66 changes: 66 additions & 0 deletions debugserver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package debugserver

import "testing"

func TestRequestID(t *testing.T) {
cases := []struct {
name string
input string
expected string
expectedError error
}{
{
name: "valid case",
input: "/buckets/test/",
expected: "test",
expectedError: nil,
},
{
name: "valid case",
input: "/buckets/_/",
expected: "_",
expectedError: nil,
},
{
name: "valid case without trailing slash",
input: "/buckets/test",
expected: "test",
expectedError: nil,
},
{
name: "valid case, with multiple levels",
input: "/buckets/test1/test2",
expected: "test1",
expectedError: nil,
},
{
name: "invalid case with no ID and double slashes",
input: "/buckets//",
expected: "",
expectedError: errNoID,
},
{
name: "invalid case with no ID",
input: "/buckets",
expected: "",
expectedError: errNoID,
},
{
name: "invalid case with blank ID",
input: "/buckets/ /",
expected: "",
expectedError: errNoID,
},
}

for _, c := range cases {
actual, err := requestID(c.input)
if err != c.expectedError {
t.Errorf("Got error: %v, expected: %v, id: %q, case: %q", err, c.expectedError, actual, c.name)
}

if actual != c.expected {
t.Errorf("Got %q, expected: %q, case: %q", actual, c.expected, c.name)
}
}
}

0 comments on commit 75cb3ea

Please sign in to comment.