Skip to content

Commit

Permalink
style: go format
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo committed Feb 11, 2025
1 parent 93c87d4 commit fa77c93
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 44 deletions.
6 changes: 2 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ func NewRouteContext() *Context {
return &Context{}
}

var (
// RouteCtxKey is the context.Context key to store the request context.
RouteCtxKey = &contextKey{"RouteContext"}
)
// RouteCtxKey is the context.Context key to store the request context.
var RouteCtxKey = &contextKey{"RouteContext"}

// Context is the default routing context set on the root node of a
// request context to track route patterns, URL parameters and
Expand Down
6 changes: 4 additions & 2 deletions middleware/content_charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ func contentEncoding(ce string, charsets ...string) bool {

// Split a string in two parts, cleaning any whitespace.
func split(str, sep string) (string, string) {
var a, b string
var parts = strings.SplitN(str, sep, 2)
var (
a, b string
parts = strings.SplitN(str, sep, 2)
)
a = strings.TrimSpace(parts[0])
if len(parts) == 2 {
b = strings.TrimSpace(parts[1])
Expand Down
8 changes: 5 additions & 3 deletions middleware/realip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"strings"
)

var trueClientIP = http.CanonicalHeaderKey("True-Client-IP")
var xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
var xRealIP = http.CanonicalHeaderKey("X-Real-IP")
var (
trueClientIP = http.CanonicalHeaderKey("True-Client-IP")
xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
xRealIP = http.CanonicalHeaderKey("X-Real-IP")
)

// RealIP is a middleware that sets a http.Request's RemoteAddr to the results
// of parsing either the True-Client-IP, X-Real-IP or the X-Forwarded-For headers
Expand Down
3 changes: 1 addition & 2 deletions middleware/recoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ func PrintPrettyStack(rvr interface{}) {
}
}

type prettyStack struct {
}
type prettyStack struct{}

func (s prettyStack) parse(debugStack []byte, rvr interface{}) ([]byte, error) {
var err error
Expand Down
13 changes: 7 additions & 6 deletions middleware/request_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ type ctxKeyRequestID int
// RequestIDKey is the key that holds the unique request ID in a request context.
const RequestIDKey ctxKeyRequestID = 0

// RequestIDHeader is the name of the HTTP Header which contains the request id.
// Exported so that it can be changed by developers
var RequestIDHeader = "X-Request-Id"

var prefix string
var reqid uint64
var (
// RequestIDHeader is the name of the HTTP Header which contains the request id.
// Exported so that it can be changed by developers
RequestIDHeader = "X-Request-Id"
prefix string
reqid uint64
)

// A quick note on the statistics here: we're trying to calculate the chance that
// two randomly generated base62 prefixes will collide. We use the formula from
Expand Down
4 changes: 1 addition & 3 deletions middleware/throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ const (
errContextCanceled = "Context was canceled."
)

var (
defaultBacklogTimeout = time.Second * 60
)
var defaultBacklogTimeout = time.Second * 60

// ThrottleOpts represents a set of throttling options.
type ThrottleOpts struct {
Expand Down
24 changes: 12 additions & 12 deletions middleware/throttle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestThrottleBacklog(t *testing.T) {
// before the clients time out (5s) is 40.
for i := 0; i < 40; i++ {
wg.Add(1)
go func(i int) {
go func() {
defer wg.Done()

res, err := client.Get(server.URL)
Expand All @@ -49,7 +49,7 @@ func TestThrottleBacklog(t *testing.T) {
buf, err := io.ReadAll(res.Body)
assertNoError(t, err)
assertEqual(t, testContent, buf)
}(i)
}()
}

wg.Wait()
Expand Down Expand Up @@ -77,11 +77,11 @@ func TestThrottleClientTimeout(t *testing.T) {

for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
go func() {
defer wg.Done()
_, err := client.Get(server.URL)
assertError(t, err)
}(i)
}()
}

wg.Wait()
Expand Down Expand Up @@ -110,13 +110,13 @@ func TestThrottleTriggerGatewayTimeout(t *testing.T) {
// These requests will be processed normally until they finish.
for i := 0; i < 50; i++ {
wg.Add(1)
go func(i int) {
go func() {
defer wg.Done()

res, err := client.Get(server.URL)
assertNoError(t, err)
assertEqual(t, http.StatusOK, res.StatusCode)
}(i)
}()
}

time.Sleep(time.Second * 1)
Expand All @@ -125,7 +125,7 @@ func TestThrottleTriggerGatewayTimeout(t *testing.T) {
// too much time, so they will eventually receive a timeout error.
for i := 0; i < 50; i++ {
wg.Add(1)
go func(i int) {
go func() {
defer wg.Done()

res, err := client.Get(server.URL)
Expand All @@ -135,7 +135,7 @@ func TestThrottleTriggerGatewayTimeout(t *testing.T) {
assertNoError(t, err)
assertEqual(t, http.StatusTooManyRequests, res.StatusCode)
assertEqual(t, errTimedOut, strings.TrimSpace(string(buf)))
}(i)
}()
}

wg.Wait()
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestThrottleMaximum(t *testing.T) {

for i := 0; i < 20; i++ {
wg.Add(1)
go func(i int) {
go func() {
defer wg.Done()

res, err := client.Get(server.URL)
Expand All @@ -173,7 +173,7 @@ func TestThrottleMaximum(t *testing.T) {
buf, err := io.ReadAll(res.Body)
assertNoError(t, err)
assertEqual(t, testContent, buf)
}(i)
}()
}

// Wait less time than what the server takes to reply.
Expand All @@ -183,7 +183,7 @@ func TestThrottleMaximum(t *testing.T) {
// will be beyond the server capacity.
for i := 0; i < 20; i++ {
wg.Add(1)
go func(i int) {
go func() {
defer wg.Done()

res, err := client.Get(server.URL)
Expand All @@ -193,7 +193,7 @@ func TestThrottleMaximum(t *testing.T) {
assertNoError(t, err)
assertEqual(t, http.StatusTooManyRequests, res.StatusCode)
assertEqual(t, errCapacityExceeded, strings.TrimSpace(string(buf)))
}(i)
}()
}

wg.Wait()
Expand Down
8 changes: 3 additions & 5 deletions middleware/url_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import (
"github.com/go-chi/chi/v5"
)

var (
// URLFormatCtxKey is the context.Context key to store the URL format data
// for a request.
URLFormatCtxKey = &contextKey{"URLFormat"}
)
// URLFormatCtxKey is the context.Context key to store the URL format data
// for a request.
var URLFormatCtxKey = &contextKey{"URLFormat"}

// URLFormat is a middleware that parses the url extension from a request path and stores it
// on the context as a string under the key `middleware.URLFormatCtxKey`. The middleware will
Expand Down
6 changes: 4 additions & 2 deletions middleware/wrap_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ func (f *flushHijackWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return hj.Hijack()
}

var _ http.Flusher = &flushHijackWriter{}
var _ http.Hijacker = &flushHijackWriter{}
var (
_ http.Flusher = &flushHijackWriter{}
_ http.Hijacker = &flushHijackWriter{}
)

// httpFancyWriter is a HTTP writer that additionally satisfies
// http.Flusher, http.Hijacker, and io.ReaderFrom. It exists for the common case
Expand Down
7 changes: 3 additions & 4 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"net/http"
"net/http/httptest"
"strconv"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -1277,7 +1278,6 @@ func TestMuxSubroutes(t *testing.T) {
if routePatterns[2] != expected {
t.Fatalf("routePattern, expected:%s got:%s", expected, routePatterns[2])
}

}

func TestSingleHandler(t *testing.T) {
Expand Down Expand Up @@ -1383,7 +1383,7 @@ func TestServeHTTPExistingContext(t *testing.T) {
func TestNestedGroups(t *testing.T) {
handlerPrintCounter := func(w http.ResponseWriter, r *http.Request) {
counter, _ := r.Context().Value(ctxKey{"counter"}).(int)
w.Write([]byte(fmt.Sprintf("%v", counter)))
w.Write([]byte(strconv.Itoa(counter)))
}

mwIncreaseCounter := func(next http.Handler) http.Handler {
Expand Down Expand Up @@ -1421,7 +1421,6 @@ func TestNestedGroups(t *testing.T) {
r.Get("/5", handlerPrintCounter)
// r.Handle(GET, "/6", Chain(mwIncreaseCounter).HandlerFunc(handlerPrintCounter))
r.With(mwIncreaseCounter).Get("/6", handlerPrintCounter)

})
})
})
Expand Down Expand Up @@ -1974,7 +1973,7 @@ func testRequest(t *testing.T, ts *httptest.Server, method, path string, body io
return resp, string(respBody)
}

func testHandler(t *testing.T, h http.Handler, method, path string, body io.Reader) (*http.Response, string) {
func testHandler(_ *testing.T, h http.Handler, method, path string, body io.Reader) (*http.Response, string) {
r, _ := http.NewRequest(method, path, body)
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
Expand Down
2 changes: 1 addition & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (n *node) InsertRoute(method methodTyp, pattern string, handler http.Handle

// We're going to be searching for a wild node next,
// in this case, we need to get the tail
var label = search[0]
label := search[0]
var segTail byte
var segEndIdx int
var segTyp nodeTyp
Expand Down

0 comments on commit fa77c93

Please sign in to comment.