update deps

This commit is contained in:
Umputun
2022-04-15 12:50:05 -05:00
parent 53df70bcef
commit f4856c86d7
161 changed files with 2294 additions and 877 deletions
+8 -11
View File
@@ -93,37 +93,32 @@ Adds the HTTP Deprecation response header, see [draft-dalal-deprecation-header-0
BasicAuth middleware requires basic auth and matches user & passwd with client-provided checker. In case if no basic auth headers returns
`StatusUnauthorized`, in case if checker failed - `StatusForbidden`
## Rewrite middleware
### Rewrite middleware
Rewrites requests with from->to rule. Supports regex (like nginx) and prevents multiple rewrites. For example `Rewrite("^/sites/(.*)/settings/$", "/sites/settings/$1")` will change request's URL from `/sites/id1/settings/` to `/sites/settings/id1`
## NoCache middleware
### NoCache middleware
Sets a number of HTTP headers to prevent a router (handler's) response from being cached by an upstream proxy and/or client.
## Headers middleware
### Headers middleware
Sets headers (passed as key:value) to requests. I.e. `rest.Headers("Server:MyServer", "X-Blah:Foo")`
## Gzip middleware
### Gzip middleware
Compresses response with gzip.
## RealIP middleware
### RealIP middleware
RealIP is a middleware that sets a http.Request's RemoteAddr to the results of parsing either the X-Forwarded-For or X-Real-IP headers.
## Maybe middleware
### Maybe middleware
Maybe middleware will allow you to change the flow of the middleware stack execution depending on return
value of maybeFn(request). This is useful for example if you'd like to skip a middleware handler if
a request does not satisfy the maybeFn logic.
## Headers middleware
Headers middleware adds headers to request
## Helpers
- `rest.Wrap` - converts a list of middlewares to nested handlers calls (in reverse order)
@@ -134,6 +129,8 @@ Headers middleware adds headers to request
- `rest.SendErrorJSON` - makes `{error: blah, details: blah}` json body and responds with given error code. Also, adds context to the logged message
- `rest.NewErrorLogger` - creates a struct providing shorter form of logger call
- `rest.FileServer` - creates a file server for static assets with directory listing disabled
- `realip.Get` - returns client's IP address
- `rest.ParseFromTo` - parses "from" and "to" request's query params with various formats
## Profiler
+15
View File
@@ -296,6 +296,21 @@ func (l *Middleware) sanitizeQuery(rawQuery string) string {
return query.Encode()
}
// AnonymizeIP is a function to reset the last part of IPv4 to 0.
// from 123.212.12.78 it will make 123.212.12.0
func AnonymizeIP(ip string) string {
if ip == "" {
return ""
}
parts := strings.Split(ip, ".")
if len(parts) != 4 {
return ip
}
return strings.Join(parts[:3], ".") + ".0"
}
// customResponseWriter is an HTTP response logger that keeps HTTP status code and
// the number of bytes written.
// It implements http.ResponseWriter, http.Flusher and http.Hijacker.
+31
View File
@@ -5,6 +5,7 @@ import (
"bytes"
"encoding/json"
"net/http"
"time"
"github.com/pkg/errors"
)
@@ -67,3 +68,33 @@ func renderJSONWithStatus(w http.ResponseWriter, data interface{}, code int) {
w.WriteHeader(code)
_, _ = w.Write(buf.Bytes())
}
// ParseFromTo parses from and to query params of the request
func ParseFromTo(r *http.Request) (from, to time.Time, err error) {
parseTimeStamp := func(ts string) (time.Time, error) {
formats := []string{
"2006-01-02T15:04:05.000000000",
"2006-01-02T15:04:05",
"2006-01-02T15:04",
"20060102",
time.RFC3339,
time.RFC3339Nano,
}
for _, f := range formats {
if t, e := time.Parse(f, ts); e == nil {
return t, nil
}
}
return time.Time{}, errors.Errorf("can't parse date %q", ts)
}
if from, err = parseTimeStamp(r.URL.Query().Get("from")); err != nil {
return from, to, errors.Wrap(err, "incorrect from time")
}
if to, err = parseTimeStamp(r.URL.Query().Get("to")); err != nil {
return from, to, errors.Wrap(err, "incorrect to time")
}
return from, to, nil
}