1
0
mirror of https://github.com/umputun/reproxy.git synced 2024-11-30 08:16:56 +02:00
reproxy/vendor/github.com/go-pkgz/rest
2021-06-20 18:46:46 -05:00
..
logger bump go-pkgz/rest to show hostname in stdout logs 2021-04-17 12:46:22 -05:00
.gitignore vendor deps 2021-04-01 02:37:48 -05:00
.golangci.yml add support of spa-like assets handling 2021-06-07 19:16:18 -05:00
basic_auth.go bump deps 2021-04-02 14:27:40 -05:00
blackwords.go vendor deps 2021-04-01 02:37:48 -05:00
cache_control.go bump github.com/go-pkgz/rest to v1.9.0 2021-04-12 11:38:21 -05:00
depricattion.go vendor deps 2021-04-01 02:37:48 -05:00
file_server.go add support of spa-like assets handling 2021-06-07 19:16:18 -05:00
go.mod bump go-pkgz/rest 2021-04-04 02:39:42 -05:00
go.sum bump go-pkgz/rest 2021-04-04 02:39:42 -05:00
gzip.go bump github.com/go-pkgz/rest to v1.9.0 2021-04-12 11:38:21 -05:00
httperrors.go bump github.com/go-pkgz/rest to v1.9.0 2021-04-12 11:38:21 -05:00
LICENSE vendor deps 2021-04-01 02:37:48 -05:00
metrics.go vendor deps 2021-04-01 02:37:48 -05:00
middleware.go upgrade recoverer to suppress the noise on net/http: abort Handler 2021-04-12 12:08:50 -05:00
nocache.go bump deps 2021-04-02 14:27:40 -05:00
onlyfrom.go vendor deps 2021-04-01 02:37:48 -05:00
README.md bump deps 2021-04-02 14:27:40 -05:00
rest.go bump github.com/go-pkgz/rest to v1.9.0 2021-04-12 11:38:21 -05:00
rewrite.go vendor deps 2021-04-01 02:37:48 -05:00
sizelimit.go add support of spa-like assets handling 2021-06-07 19:16:18 -05:00
throttle.go update go-pkgz/rest deps 2021-06-20 18:46:46 -05:00
trace.go vendor deps 2021-04-01 02:37:48 -05:00

REST helpers and middleware Build Status Go Report Card Coverage Status godoc

Install and update

go get -u github.com/go-pkgz/rest

Middlewares

AppInfo middleware

Adds info to every response header:

  • App-Name - application name
  • App-Version - application version
  • Org - organization
  • M-Host - host name from instance-level $MHOST env

Ping-Pong middleware

Responds with pong on GET /ping. Also, responds to anything with /ping suffix, like /v2/ping.

Example for both:

> http GET https://remark42.radio-t.com/ping

HTTP/1.1 200 OK
Date: Sun, 15 Jul 2018 19:40:31 GMT
Content-Type: text/plain
Content-Length: 4
Connection: keep-alive
App-Name: remark42
App-Version: master-ed92a0b-20180630-15:59:56
Org: Umputun

pong

Logger middleware

Logs request, request handling time and response. Log record fields in order of occurrence:

  • Request's HTTP method
  • Requested URL (with sanitized query)
  • Remote IP
  • Response's HTTP status code
  • Response body size
  • Request handling time
  • Userinfo associated with the request (optional)
  • Request subject (optional)
  • Request ID (if X-Request-ID present)
  • Request body (optional)

remote IP can be masked with user defined function

example: 019/03/05 17:26:12.976 [INFO] GET - /api/v1/find?site=remark - 8e228e9cfece - 200 (115) - 4.47784618s

Recoverer middleware

Recoverer is a middleware that recovers from panics, logs the panic (and a backtrace), and returns an HTTP 500 (Internal Server Error) status if possible.

OnlyFrom middleware

OnlyFrom middleware allows access for limited list of source IPs. Such IPs can be defined as complete ip (like 192.168.1.12), prefix (129.168.) or CIDR (192.168.0.0/16)

Metrics middleware

Metrics middleware responds to GET /metrics with list of expvar. Optionally allows restricting list of source ips.

BlackWords middleware

BlackWords middleware doesn't allow user-defined words in the request body.

SizeLimit middleware

SizeLimit middleware checks if body size is above the limit and returns StatusRequestEntityTooLarge (413)

Trace middleware

It looks for X-Request-ID header and makes it as a random id (if not found), then populates it to the result's header and to the request's context.

Deprecation middleware

Adds the HTTP Deprecation response header, see draft-dalal-deprecation-header-00

BasicAuth middleware

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

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

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

Sets headers (passed as key:value) to requests. I.e. rest.Headers("Server:MyServer", "X-Blah:Foo")

Gzip middleware

Compresses response with gzip.

Helpers

  • rest.Wrap - converts a list of middlewares to nested handlers calls (in reverse order)
  • rest.JSON - map alias, just for convenience type JSON map[string]interface{}
  • rest.RenderJSON - renders json response from interface{}
  • rest.RenderJSONFromBytes - renders json response from []byte
  • rest.RenderJSONWithHTML - renders json response with html tags and forced charset=utf-8
  • 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