Skip to content

Commit

Permalink
go vet and golint the project, release 3.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Kieltyka committed Aug 2, 2017
1 parent 394e5ca commit 25354a5
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 59 deletions.
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`

This comment has been minimized.

Copy link
@VojtechVitek

VojtechVitek Aug 10, 2017

Contributor

Breaking API changes should happen on major releases only (v4.0.0). Or at least minor version (v3.2.0). Not sure if this is worthy just to satisfy golint :)

However, ServerBaseContext() isn't something many people use. Hopefully they won't be that mad at us :)



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

- Fix a few minor issues after v3 release
Expand Down
2 changes: 1 addition & 1 deletion _examples/graceful/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

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

Expand Down Expand Up @@ -60,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 {
Expand Down Expand Up @@ -110,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
}
Expand All @@ -122,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 {

This comment has been minimized.

Copy link
@djui

djui Aug 10, 2017

I understand this to be an API backwards incompatible change. Should this deserve a new major version (v4) to stay honest to SemVer?
Maybe at least a minor version change (v3.2.0)?

We are using dep already and have set it up to automatically bump patch versions, which I believe this change will break.

fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
baseCtx := baseCtx
Expand Down
13 changes: 13 additions & 0 deletions middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
)

Expand All @@ -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) {
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions middleware/url_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
)

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

Expand Down
3 changes: 3 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Loading

0 comments on commit 25354a5

Please sign in to comment.