-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package server | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
) | ||
|
||
type Middleware func(http.Handler) http.Handler | ||
|
||
// The Context type helps middleware pass along information through the chain. | ||
type Context struct { | ||
responseStatusCode int | ||
} | ||
|
||
// The ResponseWriterCloner struct implements the http.ResponseWriter class, and copies the status | ||
// code of the response for the middleware to be able to read the responses. | ||
type ResponseWriterCloner struct { | ||
http.ResponseWriter | ||
statusCode int | ||
} | ||
|
||
// NewResponseWriter returns a new ResponseWriterCloner struct | ||
func NewResponseWriter(w http.ResponseWriter) *ResponseWriterCloner { | ||
return &ResponseWriterCloner{w, http.StatusOK} | ||
} | ||
|
||
// WriteHeader duplicates the status code into the cloner struct for reading | ||
func (rwc *ResponseWriterCloner) WriteHeader(code int) { | ||
rwc.statusCode = code | ||
rwc.ResponseWriter.WriteHeader(code) | ||
} | ||
|
||
// The Metrics middleware captures any request relevant to a metric and records it for prometheus. | ||
func Metrics(ctx *Context) Middleware { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
next.ServeHTTP(w, r) | ||
if ctx.responseStatusCode != 200 { | ||
return | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// The logging middleware captures any http request coming through, and logs it. | ||
func Logging(ctx *Context) Middleware { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
clonedWwriter := NewResponseWriter(w) | ||
next.ServeHTTP(w, r) | ||
log.Println(r.Method, r.URL.Path, clonedWwriter.statusCode, http.StatusText(clonedWwriter.statusCode)) | ||
ctx.responseStatusCode = clonedWwriter.statusCode | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package server_test |