Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RouteParams ¶ added in v1.0.0
RouteParams gathers captured path parameters from the request context.
If the context doesn't contain a parameter map, it returns nil. If Router was used but no parameters were captured in the pattern, it returns a non-nil empty map.
Types ¶
type Handler ¶
Handler is a type which can turn a request into a response.
Handle may return a nil response, in which case the Server is expected to build the protocol-appropriate "Not Found" response.
func FallthroughHandler ¶
FallthroughHandler builds a handler which tries multiple child handlers.
The returned handler will invoke each of the passed-in handlers in order, stopping when it receives a non-nil response.
type Middleware ¶
Middleware is a handler decorator.
It returns a handler which may call the passed-in handler or not, or may transform the request or response in some way.
func Filter ¶
Filter builds a middleware which only calls the wrapped Handler under a condition.
When the condition function returns false it instead invokes the test-failure handler. The failure handler may also be nil, in which case the final handler will return a nil response whenever the condition fails.
type Request ¶
type Request struct { // URL is the specific URL being fetched by the request. *url.URL // Server is the server which received the request. // // This is only populated in servers. // It is unused on the client end. Server Server // RemoteAddr is the address of the other side of the connection. // // This will be the server address for clients, or the connecting // client's address in servers. // // Be aware though that proxies (and reverse proxies) can confuse this. RemoteAddr net.Addr // TLSState contains information about the TLS encryption over the connection. // // This includes peer certificates and version information. TLSState *tls.ConnectionState }
Request represents a request over any small web protocol.
Because protocols have so many differences, this type represents a greatest common denominator of request/response-oriented protocols.
func (Request) UnescapedQuery ¶
UnescapedQuery performs %XX unescaping on the URL query segment.
Like URL.Query(), it silently drops malformed %-encoded sequences.
type Response ¶
type Response struct { // Status is the status code of the response. Status Status // Meta contains status-specific additional information. Meta any // Body is the response body, if any. Body io.Reader }
Response contains the data in a response over the small web.
Because protocols have so many differences, this type represents a greatest common denominator of request/response-oriented protocols.
type ResponseReader ¶ added in v0.9.0
ResponseReader is an object which can serialize a response to a protocol.
type Router ¶ added in v1.0.0
type Router struct {
// contains filtered or unexported fields
}
Router stores a mapping of request path patterns to handlers.
Pattern may begin with "/" and then contain slash-delimited segments.
- Segments beginning with colon (:) are wildcards and will match any path segment at that location. It may optionally have a word after the colon, which will be the parameter name the path segment is captured into.
- Segments beginning with asterisk (*) are remainder wildcards. This must come last and will capture any remainder of the path. It may have a name after the asterisk which will be the parameter name.
- Any other segment in the pattern must match a path segment exactly.
These patterns do not match any path which shares a prefix, rather then full path must match a pattern. If you want to only match a prefix of the path you can end the pattern with a *remainder segment.
The zero value is a usable Router which will fail to match any requst path.
func (Router) Handler ¶ added in v1.0.0
Handler matches against the request path and dipatches to a route handler.
If no route matches, it returns a nil response. Captured path parameters will be stored in the context passed into the handler and can be retrieved with RouteParams().
func (Router) Match ¶ added in v1.0.0
Match returns the matched handler and captured path parameters, or (nil, nil).
The returned handlers will be wrapped with any middleware attached to the router.
func (*Router) Mount ¶ added in v1.0.0
Mount attaches a sub-router to handle path suffixes after an initial prefix pattern.
The prefix pattern may include segment :wildcards, but no *remainder segment. The mounted sub-router should have patterns which only include the portion of the path after whatever was matched by the prefix pattern.
func (*Router) Use ¶ added in v1.0.0
func (r *Router) Use(mw Middleware)
Use attaches a middleware to the router.
Any routes set on the router will have their handlers decorated by the attached middlewares in reverse order (the first middleware attached will be the outer-most: first to see requests and the last to see responses).
Use will panic if Route or Mount have already been called on the router - middlewares must be set before any routes.
type Server ¶
type Server interface { // Serve blocks listening for connections on an interface. // // It will only return after Close() has been called. Serve() error // Close initiates a graceful shutdown of the server. // // It blocks until all resources have been cleaned up and all // outstanding requests have been handled and responses sent. Close() // Closed indicates whether Close has been called. // // It may be true even if the graceful shutdown procedure // hasn't yet completed. Closed() bool // Protocol returns the protocol being served by the server. Protocol() string // Network returns the network type on which the server is running. Network() string // Address returns the address on which the server is listening. Address() string // Hostname returns just the hostname portion of the listen address. Hostname() string // Port returns the port on which the server is listening. // // It will return the empty string if the network type does not // have ports (unix sockets, for example). Port() string // LogError sends a log message to the server's error log. LogError(keyvals ...any) error }
Server is a type which can serve a protocol.
Directories ¶
Path | Synopsis |
---|---|
Contrib contains sub-packages with specific functionality for small web servers.
|
Contrib contains sub-packages with specific functionality for small web servers. |
examples
|
|
The gemini package contains everything needed for building clients and servers on the gemini protocol.
|
The gemini package contains everything needed for building clients and servers on the gemini protocol. |
gemtext
The gemtext package contains a gemtext AST and parser.
|
The gemtext package contains a gemtext AST and parser. |