Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: go-chi/chi
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.1.4
Choose a base ref
...
head repository: go-chi/chi
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.1.5
Choose a head ref
  • 3 commits
  • 11 files changed
  • 2 contributors

Commits on Jul 31, 2017

  1. Add comment about Context.RoutePattern()

    Peter Kieltyka committed Jul 31, 2017
    Copy the full SHA
    168b7ac View commit details

Commits on Aug 2, 2017

  1. add go vet and golint to travis checks (#237)

    zemirco authored and Peter Kieltyka committed Aug 2, 2017
    Copy the full SHA
    394e5ca View commit details
  2. go vet and golint the project, release 3.1.5

    Peter Kieltyka committed Aug 2, 2017
    2

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    25354a5 View commit details
Showing with 119 additions and 59 deletions.
  1. +3 −0 .travis.yml
  2. +7 −0 CHANGELOG.md
  3. +1 −1 _examples/graceful/main.go
  4. +2 −0 chain.go
  5. +25 −1 context.go
  6. +13 −0 middleware/logger.go
  7. +2 −0 middleware/url_format.go
  8. +3 −0 mux.go
  9. +51 −43 mux_test.go
  10. +11 −13 tree.go
  11. +1 −1 tree_test.go
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -7,9 +7,12 @@ go:

install:
- go get -u golang.org/x/tools/cmd/goimports
- go get -u github.com/golang/lint/golint

script:
- go get -d -t ./...
- go vet ./...
- golint ./...
- go test ./...
- >
goimports -d -e ./ | grep '.*' && { echo; echo "Aborting due to non-empty goimports output."; exit 1; } || :
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v3.1.5

- Setup golint and go vet for the project
- As per golint, we've redefined `func ServerBaseContext(h http.Handler, baseCtx context.Context) http.Handler`
to `func ServerBaseContext(baseCtx context.Context, h http.Handler) http.Handler`


## v3.1.0 (2017-07-10)

- Fix a few minor issues after v3 release
2 changes: 1 addition & 1 deletion _examples/graceful/main.go
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ func main() {
w.Write([]byte(fmt.Sprintf("all done.\n")))
})

srv := http.Server{Addr: ":3333", Handler: chi.ServerBaseContext(r, baseCtx)}
srv := http.Server{Addr: ":3333", Handler: chi.ServerBaseContext(baseCtx, r)}

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
2 changes: 2 additions & 0 deletions chain.go
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ func (mws Middlewares) HandlerFunc(h http.HandlerFunc) http.Handler {
return &ChainHandler{mws, h, chain(mws, h)}
}

// ChainHandler is a http.Handler with support for handler composition and
// execution.
type ChainHandler struct {
Middlewares Middlewares
Endpoint http.Handler
26 changes: 25 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
@@ -4,9 +4,11 @@ import (
"context"
"net"
"net/http"
"strings"
)

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

@@ -59,6 +61,8 @@ func (x *Context) reset() {
x.methodNotAllowed = false
}

// URLParam returns the corresponding URL parameter value from the request
// routing context.
func (x *Context) URLParam(key string) string {
for k := len(x.URLParams.Keys) - 1; k >= 0; k-- {
if x.URLParams.Keys[k] == key {
@@ -68,6 +72,25 @@ func (x *Context) URLParam(key string) string {
return ""
}

// RoutePattern builds the routing pattern string for the particular
// request, at the particular point during routing. This means, the value
// will change throughout the execution of a request in a router. That is
// why its advised to only use this value after calling the next handler.
//
// For example,
//
// func Instrument(next http.Handler) http.Handler {
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// next.ServeHTTP(w, r)
// routePattern := chi.RouteContext(r.Context()).RoutePattern()
// measure(w, r, routePattern)
// })
// }
func (x *Context) RoutePattern() string {
routePattern := strings.Join(x.RoutePatterns, "")
return strings.Replace(routePattern, "/*/", "/", -1)
}

// RouteContext returns chi's routing Context object from a
// http.Request Context.
func RouteContext(ctx context.Context) *Context {
@@ -90,6 +113,7 @@ func URLParamFromCtx(ctx context.Context, key string) string {
return ""
}

// RouteParams is a structure to track URL routing parameters efficiently.
type RouteParams struct {
Keys, Values []string
}
@@ -102,7 +126,7 @@ func (s *RouteParams) Add(key, value string) {

// ServerBaseContext wraps an http.Handler to set the request context to the
// `baseCtx`.
func ServerBaseContext(h http.Handler, baseCtx context.Context) http.Handler {
func ServerBaseContext(baseCtx context.Context, h http.Handler) http.Handler {
fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
baseCtx := baseCtx
13 changes: 13 additions & 0 deletions middleware/logger.go
Original file line number Diff line number Diff line change
@@ -10,8 +10,12 @@ import (
)

var (
// LogEntryCtxKey is the context.Context key to store the request log entry.
LogEntryCtxKey = &contextKey{"LogEntry"}

// DefaultLogger is called by the Logger middleware handler to log each request.
// Its made a package-level variable so that it can be reconfigured for custom
// logging configurations.
DefaultLogger = RequestLogger(&DefaultLogFormatter{Logger: log.New(os.Stdout, "", log.LstdFlags)})
)

@@ -27,6 +31,7 @@ func Logger(next http.Handler) http.Handler {
return DefaultLogger(next)
}

// RequestLogger returns a logger handler using a custom LogFormatter.
func RequestLogger(f LogFormatter) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
@@ -44,29 +49,37 @@ func RequestLogger(f LogFormatter) func(next http.Handler) http.Handler {
}
}

// LogFormatter initiates the beginning of a new LogEntry per request.
// See DefaultLogFormatter for an example implementation.
type LogFormatter interface {
NewLogEntry(r *http.Request) LogEntry
}

// LogEntry records the final log when a request completes.
// See defaultLogEntry for an example implementation.
type LogEntry interface {
Write(status, bytes int, elapsed time.Duration)
Panic(v interface{}, stack []byte)
}

// GetLogEntry returns the in-context LogEntry for a request.
func GetLogEntry(r *http.Request) LogEntry {
entry, _ := r.Context().Value(LogEntryCtxKey).(LogEntry)
return entry
}

// WithLogEntry sets the in-context LogEntry for a request.
func WithLogEntry(r *http.Request, entry LogEntry) *http.Request {
r = r.WithContext(context.WithValue(r.Context(), LogEntryCtxKey, entry))
return r
}

// DefaultLogFormatter is a simple logger that implements a LogFormatter.
type DefaultLogFormatter struct {
Logger *log.Logger
}

// NewLogEntry creates a new LogEntry for the request.
func (l *DefaultLogFormatter) NewLogEntry(r *http.Request) LogEntry {
entry := &defaultLogEntry{
DefaultLogFormatter: l,
2 changes: 2 additions & 0 deletions middleware/url_format.go
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@ import (
)

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

3 changes: 3 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
@@ -317,10 +317,13 @@ func (mx *Mux) Mount(pattern string, handler http.Handler) {
}
}

// Middlewares returns a slice of middleware handler functions.
func (mx *Mux) Middlewares() Middlewares {
return mx.middlewares
}

// Routes returns a slice of routing information from the tree,
// useful for traversing available routes of a router.
func (mx *Mux) Routes() []Route {
return mx.tree.routes()
}
Loading