mid

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORS

func CORS(domain string, options ...OptionModifier) echo.MiddlewareFunc

CORS configures echo's CORS middleware with the following default allowed headers for the specified domain: Origin, Content-Type, Accept, Accept-Encoding, Content-Length, Authorization, Cache-Control.

You can use the two modifier functions to add additional domains, or additional headers, for example:

  • WithDomains("example.net", "jquery.com")
  • WithHeaders("X-Suborbital-Something", "X-Marks-The-Spot")
  • WithSkipper(func(c echo.Context) bool { return c.Path() != "/home" })

func CustomContext added in v0.0.8

func CustomContext() echo.MiddlewareFunc

CustomContext wraps the default echo.Context into our own decorated struct, http.Context. The only addition in ours is a .RequestID() convenience method that returns the request ID stored in the echo.HeaderXRequestID header.

func Logger

func Logger(l zerolog.Logger, skipPaths []string) echo.MiddlewareFunc

Logger middleware configures echo's built in middleware.RequestLoggerWithConfig middleware. The passed in zerolog.Logger is used to output the logs. For each request there are two log entries: - one for the incoming, before it gets passed onto the next handler, and - another one that logs info about the response before the client receives it.

A non-empty slice of paths will be used to entirely skip logging for those routes. Do be careful to pass in the routes as you added them to echo. For example, if it's e.Get("/path/:something", ...), then the entry should look like []string{"/path/:something"}. If the path is inside a group with a path prefix, you need to add the full path, which includes the group prefix.

Important to note that the Logger is configuring the echo middleware to handle errors first, and then return to the middleware. This has a side effect that middlewares further up the chain won't be able to change the response body or status code or headers. The upside is that any error handling happens between the "request started" and "request finished" log entries.

The following fields are printed into the structured output for the incoming request along with a message of "request started":

  • path - c.Path() - this is the one you pass to the request functions. Has the placeholders in.
  • URI - c.Request().RequestURI - this is the actual request path that gets served. The placeholders are substituted
  • requestID - c.Request().Header().Get(echo.HeaderXRequestID) - the RequestID middleware puts that in the header. That middleware needs to wrap this one within it.
  • method - c.Request().Method - the http verb used, e.g. GET, POST, PUT, etc...

When the request is finished, the log will have a "request finished" message, and the following structured info:

  • URI - request URI, same as above
  • status - response status code
  • requestID - same as above
  • method - same as above
  • latency - how long the request took. The time is between the logger middleware seeing the request go in, and the logger middleware seeing the same request return, so it's a total time of every handler and middleware inside of this middleware

func UUIDRequestID

func UUIDRequestID() echo.MiddlewareFunc

UUIDRequestID configures echo's built in request id middleware so that the ID generated is an UUIDv4, and the generated request ID is added to the following three parts of the request: - the echo.HeaderXRequestID header, this is by default - echo.Context's own Set method with the RequestIDKey key - request context with key RequestIDKey

Value of the RequestIDKey is "requestID", however for stability, use the exported constant when referring to it.

Types

type CORSOptions

type CORSOptions struct {
	// contains filtered or unexported fields
}

CORSOptions represents configuration options for the CORS middleware to further customize the built-in echo CORS middleware to suit our needs.

type OptionModifier

type OptionModifier func(c *CORSOptions)

OptionModifier is a type of function that changes values on a CORSOptions struct in place.

func WithDomains

func WithDomains(domains ...string) OptionModifier

WithDomains adds additional domains to the CORS middleware as permitted origins besides the one already passed to the middleware constructor.

func WithHeaders

func WithHeaders(headers ...string) OptionModifier

WithHeaders adds additional header keys that are returned in cross-origin responses besides the ones the CORS middleware already allows.

func WithSkipper

func WithSkipper(skipper func(echo.Context) bool) OptionModifier

WithSkipper configures a skipper function for the CORS header. If not set, it will use middleware.DefaultSkipper, which enables the middleware to be used on all routes it's attached to.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL