Mercurial > gemma
changeset 4164:6f9d00c8cc38
Made 'golint' and 'staticcheck' happy with middleware package.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 02 Aug 2019 18:13:58 +0200 |
parents | 51cfa54fc5b1 |
children | 8dd59e2c9d3d |
files | pkg/middleware/dbconn.go pkg/middleware/json.go pkg/middleware/notfound.go |
diffstat | 3 files changed, 18 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/middleware/dbconn.go Fri Aug 02 18:01:32 2019 +0200 +++ b/pkg/middleware/dbconn.go Fri Aug 02 18:13:58 2019 +0200 @@ -27,6 +27,8 @@ const wrapDBKey wrapDBKeyType = 0 +// GetDBConn fetches a *sql.Conn from the context of the request. +// Returns nil if no such connection exists. func GetDBConn(req *http.Request) *sql.Conn { if conn, ok := req.Context().Value(wrapDBKey).(*sql.Conn); ok { return conn @@ -34,6 +36,11 @@ return nil } +// DBConn is a middleware that stores a *sql.Conn in the context +// of the incoming request if the user is authorized and +// has a valid session. +// The handler will return with an http.StatusUnauthorized else +// w/o calling the cascaded next handler. func DBConn(next http.Handler) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
--- a/pkg/middleware/json.go Fri Aug 02 18:01:32 2019 +0200 +++ b/pkg/middleware/json.go Fri Aug 02 18:13:58 2019 +0200 @@ -24,12 +24,22 @@ const jsonInputKey jsonInputKeyType = 0 +// DefaultLimit limits the incoming JSON payload to 2K to +// prevent flooding the server. const DefaultLimit = 2048 +// GetJSONInput returns the deserialized JSON data from +// the incoming request if any. func GetJSONInput(req *http.Request) interface{} { return req.Context().Value(jsonInputKey) } +// JSONInput is a middleware to deserialize the incomming +// request body to a object to be created by a given input function +// and stores the result into the context. +// GetJSONInput can be used to receive the deserialized data. +// limit limits the size of the incoming body to prevent +// flooding the server. func JSONInput(next http.Handler, input func(*http.Request) interface{}, limit int64) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
--- a/pkg/middleware/notfound.go Fri Aug 02 18:01:32 2019 +0200 +++ b/pkg/middleware/notfound.go Fri Aug 02 18:13:58 2019 +0200 @@ -20,7 +20,7 @@ // ErrNotFound should be as the argument to panic if the NotFound should // report back a http.StatusNotFound. -var ErrNotFound = errors.New("Not found") +var ErrNotFound = errors.New("not found") // NotFound creates an http.Handler which survives panic(ErrNotFound) and // reports http.StatusNotFound in these cases back to the calling handler.