2015-01-30 13:14:31 +08:00
|
|
|
// Package middleware provides some types and functions common among middleware.
|
2015-01-14 03:43:45 +08:00
|
|
|
package middleware
|
|
|
|
|
2015-01-30 13:08:40 +08:00
|
|
|
import "net/http"
|
2015-01-14 03:43:45 +08:00
|
|
|
|
2015-01-19 14:11:21 +08:00
|
|
|
type (
|
|
|
|
// Generator represents the outer layer of a middleware that
|
|
|
|
// parses tokens to configure the middleware instance.
|
2015-01-30 13:02:17 +08:00
|
|
|
Generator func(Controller) (Middleware, error)
|
2015-01-19 14:11:21 +08:00
|
|
|
|
|
|
|
// Middleware is the middle layer which represents the traditional
|
|
|
|
// idea of middleware: it is passed the next HandlerFunc in the chain
|
|
|
|
// and returns the inner layer, which is the actual HandlerFunc.
|
|
|
|
Middleware func(http.HandlerFunc) http.HandlerFunc
|
|
|
|
|
2015-03-21 08:11:54 +08:00
|
|
|
// A Control provides structured access to tokens from a configuration file
|
|
|
|
// and also to properties of the server being configured. Middleware generators
|
|
|
|
// use a Controller to construct their middleware instance.
|
2015-01-30 13:02:17 +08:00
|
|
|
Controller interface {
|
2015-03-21 08:11:54 +08: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 14:11:21 +08:00
|
|
|
Next() bool
|
2015-03-21 08:11:54 +08: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 14:11:21 +08:00
|
|
|
NextArg() bool
|
2015-03-21 08:11:54 +08: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 14:11:21 +08:00
|
|
|
NextLine() bool
|
2015-03-21 08:11:54 +08:00
|
|
|
|
|
|
|
// NextBlock advances the cursor to the next token only
|
|
|
|
// if the current token is an open curly brace on the
|
|
|
|
// same line. If so, that token is consumed and this
|
|
|
|
// function will return true until the closing curly
|
|
|
|
// brace gets consumed by this method. Usually, you would
|
|
|
|
// use this as the condition of a for loop to parse
|
|
|
|
// tokens while being inside a block.
|
2015-01-22 03:09:49 +08:00
|
|
|
NextBlock() bool
|
2015-03-21 08:11:54 +08:00
|
|
|
|
|
|
|
// Val gets the text of the current token.
|
2015-01-19 14:11:21 +08:00
|
|
|
Val() string
|
2015-03-21 08:11:54 +08: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-22 08:51:47 +08:00
|
|
|
Args(...*string) bool
|
2015-03-21 08:11:54 +08:00
|
|
|
|
|
|
|
// RemainingArgs is a convenience function that loads any more arguments
|
|
|
|
// (tokens on the same line) into a slice and returns them. If an open curly
|
|
|
|
// brace token is encountered before the end of the line, that token is
|
|
|
|
// considered the end of the arguments (and the curly brace is not consumed).
|
2015-03-17 01:23:17 +08:00
|
|
|
RemainingArgs() []string
|
2015-03-21 08:11:54 +08: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 13:02:17 +08:00
|
|
|
ArgErr() error
|
2015-03-21 08:11:54 +08:00
|
|
|
|
|
|
|
// Err generates a custom parse error with a message of msg.
|
2015-01-30 13:02:17 +08:00
|
|
|
Err(string) error
|
2015-03-21 08:11:54 +08:00
|
|
|
|
|
|
|
// Startup registers a function to execute when the server starts.
|
2015-01-19 14:11:21 +08:00
|
|
|
Startup(func() error)
|
2015-03-21 08:11:54 +08:00
|
|
|
|
|
|
|
// Root returns the file path from which the server is serving.
|
2015-01-19 14:11:21 +08:00
|
|
|
Root() string
|
2015-03-21 08:11:54 +08:00
|
|
|
|
|
|
|
// Host returns the hostname the server is bound to.
|
2015-01-19 14:11:21 +08:00
|
|
|
Host() string
|
2015-03-21 08:11:54 +08:00
|
|
|
|
|
|
|
// Port returns the port that the server is listening on.
|
2015-01-19 14:11:21 +08:00
|
|
|
Port() string
|
2015-03-21 08:11:54 +08:00
|
|
|
|
|
|
|
// Context returns the path scope that the Controller is in.
|
|
|
|
// Note: This is not currently used, but may be in the future.
|
2015-03-04 00:49:01 +08:00
|
|
|
Context() Path
|
2015-01-19 14:11:21 +08:00
|
|
|
}
|
|
|
|
)
|