2015-01-30 00:14:31 -05:00
|
|
|
// Package middleware provides some types and functions common among middleware.
|
2015-01-13 14:43:45 -05:00
|
|
|
package middleware
|
|
|
|
|
2015-01-30 00:08:40 -05:00
|
|
|
import "net/http"
|
2015-01-13 14:43:45 -05:00
|
|
|
|
2015-01-19 01:11:21 -05:00
|
|
|
type (
|
|
|
|
// Generator represents the outer layer of a middleware that
|
|
|
|
// parses tokens to configure the middleware instance.
|
2015-01-30 00:02:17 -05:00
|
|
|
Generator func(Controller) (Middleware, error)
|
2015-01-19 01:11:21 -05:00
|
|
|
|
|
|
|
// Middleware is the middle layer which represents the traditional
|
|
|
|
// idea of middleware: it is passed the next HandlerFunc in the chain
|
2015-03-28 17:37:37 -05:00
|
|
|
// and returns the inner layer, which is the actual Handler.
|
|
|
|
Middleware func(HandlerFunc) HandlerFunc
|
|
|
|
|
|
|
|
// HandlerFunc is like http.HandlerFunc except it returns a status code
|
|
|
|
// and an error. It is the inner-most layer which serves individual
|
2015-03-29 23:01:42 -05:00
|
|
|
// requests. The status code is for the client's benefit; the error
|
2015-03-28 17:37:37 -05:00
|
|
|
// value is for the server's benefit. The status code will be sent to
|
|
|
|
// the client while the error value will be logged privately. Sometimes,
|
|
|
|
// an error status code (4xx or 5xx) may be returned with a nil error
|
|
|
|
// when there is no reason to log the error on the server.
|
|
|
|
//
|
2015-03-29 23:01:42 -05:00
|
|
|
// If a HandlerFunc returns an error (status >= 400), it should NOT
|
|
|
|
// write to the response. This philosophy makes middleware.HandlerFunc
|
|
|
|
// different from http.HandlerFunc: error handling should happen at
|
|
|
|
// the application layer or in dedicated error-handling middleware
|
|
|
|
// only, rather than with an "every middleware for itself" paradigm.
|
|
|
|
//
|
|
|
|
// The application or error-handling middleware should incorporate logic
|
|
|
|
// to ensure that the client always gets a proper response according to
|
|
|
|
// the status code. For security reasons, it should probably not reveal
|
|
|
|
// the actual error message. (Instead it should be logged, for example.)
|
|
|
|
//
|
|
|
|
// Handlers which do write to the response should return a status value
|
|
|
|
// < 400 as a signal that a response has been written. In other words,
|
|
|
|
// only error-handling middleware or the application will write to the
|
|
|
|
// response for a status code >= 400. When ANY handler writes to the
|
|
|
|
// response, it should return a status code < 400 to signal others to
|
|
|
|
// NOT write to the response again, which would be erroneous.
|
2015-03-28 17:37:37 -05:00
|
|
|
HandlerFunc func(http.ResponseWriter, *http.Request) (int, error)
|
|
|
|
|
|
|
|
// Handler is like http.Handler except ServeHTTP returns a status code
|
|
|
|
// and an error. See HandlerFunc documentation for more information.
|
|
|
|
Handler interface {
|
|
|
|
ServeHTTP(http.ResponseWriter, *http.Request) (int, error)
|
|
|
|
}
|
2015-01-19 01:11:21 -05:00
|
|
|
|
2015-03-29 20:56:19 -05:00
|
|
|
// A Controller provides access to properties of the server. Middleware
|
|
|
|
// generators use a Controller to construct their instances.
|
2015-01-30 00:02:17 -05:00
|
|
|
Controller interface {
|
2015-03-29 20:56:19 -05:00
|
|
|
Dispenser
|
|
|
|
|
|
|
|
// Startup registers a function to execute when the server starts.
|
|
|
|
Startup(func() error)
|
|
|
|
|
|
|
|
// Shutdown registers a function to execute when the server exits.
|
|
|
|
Shutdown(func() error)
|
|
|
|
|
|
|
|
// Root returns the file path from which the server is serving.
|
|
|
|
Root() string
|
|
|
|
|
|
|
|
// Host returns the hostname the server is bound to.
|
|
|
|
Host() string
|
|
|
|
|
|
|
|
// Port returns the port that the server is listening on.
|
|
|
|
Port() string
|
|
|
|
|
|
|
|
// Context returns the path scope that the Controller is in.
|
|
|
|
// Note: This is not currently used, but may be in the future.
|
|
|
|
Context() Path
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Dispenser provides structured access to tokens from a configuration
|
|
|
|
// file. It dispenses tokens to middleware for parsing so that middleware
|
|
|
|
// can configure themselves.
|
|
|
|
Dispenser interface {
|
2015-03-20 19:11:54 -05:00
|
|
|
// Next loads the next token. Returns true if a token
|
|
|
|
// was loaded; false otherwise. If false, all tokens
|
|
|
|
// have already been consumed.
|
2015-01-19 01:11:21 -05:00
|
|
|
Next() bool
|
2015-03-20 19:11:54 -05:00
|
|
|
|
|
|
|
// NextArg loads the next token if it is on the same
|
|
|
|
// line. Returns true if a token was loaded; false
|
|
|
|
// otherwise. If false, all tokens on the line have
|
|
|
|
// been consumed.
|
2015-01-19 01:11:21 -05:00
|
|
|
NextArg() bool
|
2015-03-20 19:11:54 -05:00
|
|
|
|
|
|
|
// NextLine loads the next token only if it is NOT on the same
|
|
|
|
// line as the current token, and returns true if a token was
|
|
|
|
// loaded; false otherwise. If false, there is not another token
|
|
|
|
// or it is on the same line.
|
2015-01-19 01:11:21 -05:00
|
|
|
NextLine() bool
|
2015-03-20 19:11:54 -05:00
|
|
|
|
2015-03-21 12:18:37 -05:00
|
|
|
// NextBlock can be used as the condition of a for loop
|
|
|
|
// to load the next token as long as it opens a block or
|
|
|
|
// is already in a block. It returns true if a token was
|
|
|
|
// loaded, or false when the block's closing curly brace
|
|
|
|
// was loaded and thus the block ended. Nested blocks are
|
|
|
|
// not (currently) supported.
|
2015-01-21 14:09:49 -05:00
|
|
|
NextBlock() bool
|
2015-03-20 19:11:54 -05:00
|
|
|
|
|
|
|
// Val gets the text of the current token.
|
2015-01-19 01:11:21 -05:00
|
|
|
Val() string
|
2015-03-20 19:11:54 -05:00
|
|
|
|
|
|
|
// Args is a convenience function that loads the next arguments
|
|
|
|
// (tokens on the same line) into an arbitrary number of strings
|
|
|
|
// pointed to in arguments. If there are fewer tokens available
|
|
|
|
// than string pointers, the remaining strings will not be changed
|
|
|
|
// and false will be returned. If there were enough tokens available
|
|
|
|
// to fill the arguments, then true will be returned.
|
2015-01-21 19:51:47 -05:00
|
|
|
Args(...*string) bool
|
2015-03-20 19:11:54 -05:00
|
|
|
|
2015-03-21 15:36:32 -05:00
|
|
|
// RemainingArgs loads any more arguments (tokens on the same line)
|
|
|
|
// into a slice and returns them. Open curly brace tokens also indicate
|
|
|
|
// the end of arguments, and the curly brace is not included in
|
|
|
|
// the return value nor is it loaded.
|
2015-03-16 12:23:17 -05:00
|
|
|
RemainingArgs() []string
|
2015-03-20 19:11:54 -05:00
|
|
|
|
|
|
|
// ArgErr returns an argument error, meaning that another
|
|
|
|
// argument was expected but not found. In other words,
|
|
|
|
// a line break, EOF, or open curly brace was encountered instead of
|
|
|
|
// an argument.
|
2015-01-30 00:02:17 -05:00
|
|
|
ArgErr() error
|
2015-03-20 19:11:54 -05:00
|
|
|
|
|
|
|
// Err generates a custom parse error with a message of msg.
|
2015-01-30 00:02:17 -05:00
|
|
|
Err(string) error
|
2015-01-19 01:11:21 -05:00
|
|
|
}
|
|
|
|
)
|