Skip to content

Commit

Permalink
Merge pull request #9 from gol4ng/fixup-request-id
Browse files Browse the repository at this point in the history
fixup #7
  • Loading branch information
rmasclef authored Sep 2, 2019
2 parents 020d776 + 70c5869 commit 2baf857
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
5 changes: 3 additions & 2 deletions middleware/request_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ func RequestId(config *request_id.Config) httpware.Middleware {
return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
id := req.Header.Get(config.HeaderName)
if id == "" {
id = config.GenerateId(req)
id = config.IdGenerator(req)
}
// add the request id to the context request
// add the request id to the current context request
r := req.WithContext(context.WithValue(req.Context(), config.HeaderName, id))
// add it to the response headers
writer.Header().Set(config.HeaderName, id)
next.ServeHTTP(writer, r)
})
Expand Down
12 changes: 6 additions & 6 deletions middleware/request_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestRequestIdCustom(t *testing.T) {
handlerReq = r
})
config := request_id.NewConfig()
config.GenerateId = func(request *http.Request) string {
config.IdGenerator = func(request *http.Request) string {
return "my_fake_request_id"
}

Expand All @@ -66,12 +66,12 @@ func ExampleRequestId() {
// you can override default header name
config.HeaderName = "my-personal-header-name"
// you can override default id generator
config.GenerateId = func(request *http.Request) string {
config.IdGenerator = func(request *http.Request) string {
return "my-fixed-request-id"
}

// we recommend to use MiddlewareStack to simplify managing all wanted middleware
// caution middleware order matter
// we recommend to use MiddlewareStack to simplify managing all wanted middlewares
// caution middleware order matters
stack := httpware.MiddlewareStack(
middleware.RequestId(config),
)
Expand All @@ -84,7 +84,7 @@ func ExampleRequestId() {
}()

resp, err := http.Get("http://localhost" + port)
fmt.Printf("%v %v\n", resp.Header.Get(config.HeaderName), err)
fmt.Printf("%s: %v %v\n", config.HeaderName, resp.Header.Get(config.HeaderName), err)

//Output: my-fixed-request-id <nil>
//Output: my-personal-header-name: my-fixed-request-id <nil>
}
10 changes: 4 additions & 6 deletions request_id/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (
const HeaderName = "Request-Id"

type Config struct {
HeaderName string
GenerateId func(*http.Request) string
HeaderName string
IdGenerator func(*http.Request) string
}

func NewConfig() *Config {
return &Config{
HeaderName: HeaderName,
GenerateId: func(request *http.Request) string {
return RandomString(10) // eg: XPF0G5kqEG
},
HeaderName: HeaderName,
IdGenerator: RandomIdGenerator,
}
}
9 changes: 6 additions & 3 deletions request_id/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package request_id

import (
"math/rand"
"net/http"
"unsafe"
)

func RandomIdGenerator(_ *http.Request) string {
return randomString(10) // eg: XPF0G5kqEG
}

//https://stackoverflow.com/a/31832326
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const (
Expand All @@ -13,7 +18,7 @@ const (
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)

func RandomString(n int) string {
func randomString(n int) string {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
Expand All @@ -27,7 +32,5 @@ func RandomString(n int) string {
cache >>= letterIdxBits
remain--
}

return *(*string)(unsafe.Pointer(&b))
}

6 changes: 3 additions & 3 deletions tripperware/request_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/gol4ng/httpware/request_id"
)

// RequestId middleware get request id header if provided or generate a request id
// It will add the request ID to request context and add it to response header to
// RequestId tripperware gets request id header if provided or generates a request id
// It will add the request ID to request context
func RequestId(config *request_id.Config) httpware.Tripperware {
return func(next http.RoundTripper) http.RoundTripper {
return httpware.RoundTripFunc(func(req *http.Request) (resp *http.Response, err error) {
Expand All @@ -18,7 +18,7 @@ func RequestId(config *request_id.Config) httpware.Tripperware {
id = req.Header.Get(config.HeaderName)
}
if id == "" {
id = config.GenerateId(req)
id = config.IdGenerator(req)
}
r := req.WithContext(context.WithValue(req.Context(), config.HeaderName, id))
r.Header.Add(config.HeaderName, id)
Expand Down
6 changes: 3 additions & 3 deletions tripperware/request_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package tripperware_test

import (
"fmt"
"github.com/gol4ng/httpware"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/gol4ng/httpware"
"github.com/gol4ng/httpware/mocks"
"github.com/gol4ng/httpware/request_id"
"github.com/gol4ng/httpware/tripperware"
Expand Down Expand Up @@ -49,7 +49,7 @@ func TestRequestIdCustom(t *testing.T) {
})

config := request_id.NewConfig()
config.GenerateId = func(request *http.Request) string {
config.IdGenerator = func(request *http.Request) string {
return "my_fake_request_id"
}

Expand All @@ -68,7 +68,7 @@ func ExampleRequestId() {
// you can override default header name
config.HeaderName = "my-personal-header-name"
// you can override default id generator
config.GenerateId = func(request *http.Request) string {
config.IdGenerator = func(request *http.Request) string {
return "my-generated-id"
}

Expand Down

0 comments on commit 2baf857

Please sign in to comment.