Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MiddlewareBuilder ¶
type MiddlewareBuilder struct {
// contains filtered or unexported fields
}
MiddlewareBuilder is a struct responsible for creating middleware that can manage response caching. This cache is used to store and retrieve pre-generated responses for different HTTP status codes.
func InitMiddlewareBuilder ¶
func InitMiddlewareBuilder() *MiddlewareBuilder
InitMiddlewareBuilder is a function that initializes a new instance of MiddlewareBuilder. The purpose of this initialization is to set up a MiddlewareBuilder with a pre-initialized map that is ready to cache responses by their HTTP status codes.
Return:
Returns a pointer to a newly allocated MiddlewareBuilder with a response map ready for use.
func (*MiddlewareBuilder) AddCode ¶
func (m *MiddlewareBuilder) AddCode(status int, data []byte) *MiddlewareBuilder
AddCode is a method on the MiddlewareBuilder struct. It associates a given HTTP status code with a response body which is intended to be cached and reused. By calling this method, users can define standard responses for specific HTTP status codes to be used across the application.
Parameters:
status: An integer representing the HTTP status code. Common status codes are 200 for OK, 404 for Not Found, 500 for Internal Server Error, etc. data: A slice of bytes representing the response body to be cached in association with the given status code. This could be a static file, a dynamically generated payload, or even an error message.
Returns:
Returns a pointer to the MiddlewareBuilder itself, which allows for method chaining when setting up multiple status code and response body pairings.
Example:
mb := InitMiddlewareBuilder() mb.AddCode(404, []byte("Not Found")) // This can now be chained like so... mb.AddCode(500, []byte("Internal Server Error")).AddCode(200, []byte("OK"))
func (*MiddlewareBuilder) Build ¶
func (m *MiddlewareBuilder) Build() mist.Middleware
Build creates and returns a middleware function compatible with the mist framework's Middleware type. The middleware function produced by Build intercepts the HTTP response by checking the current response status code in the context against the cached responses in the MiddlewareBuilder. If a cached response is found, it replaces the response data in the context with the cached data. This allows for consistent and potentially pre-processed responses for certain status codes.
Returns:
mist.Middleware: A middleware function that can be used with the mist framework to intercept and potentially modify response data based on status code caching.
Example:
mb := InitMiddlewareBuilder(). AddCode(404, []byte("Not Found")). AddCode(500, []byte("Internal Server Error")) router.Use(mb.Build()) // Applying the middleware to the router