Documentation
¶
Overview ¶
Package fileserver serves bytes for a route pattern ending in the catch-all `**` segment. Callers provide a Resolver that turns the URL's path remainder into bytes + mime; the framework handles the routing, the response streaming, and the cache headers.
Typical use in the consumer's wiring:
var mediaResolver fileserver.Resolver = &myapp.MediaResolver{…}
handlerMap["MediaFileServer"] = &fileserver.Handler{
Resolver: mediaResolver,
CacheMaxAge: 600,
}
And in the YAML route:
- path: /api/v1/public/media/** method: GET executor: MediaFileServer security: public
Index ¶
Constants ¶
This section is empty.
Variables ¶
var NotFound = &HTTPError{Status: http.StatusNotFound, Message: "not found"}
NotFound is a convenience for resolvers that want the default 404.
Functions ¶
This section is empty.
Types ¶
type DiskResolver ¶
type DiskResolver struct {
Root string
}
DiskResolver is a batteries-included Resolver that serves files straight from a filesystem root. The URL's path remainder is joined under the configured root, traversal is blocked, and the MIME is inferred from the extension (with a magic-number fallback for unknown extensions).
Use cases: serving bundled admin assets, static marketing pages, whatever doesn't need a database in the loop.
func NewDiskResolver ¶
func NewDiskResolver(root string) *DiskResolver
NewDiskResolver returns a resolver bound to `root`. The directory is expected to exist.
type HTTPError ¶
HTTPError is an optional error type a Resolver can return to control the response status. Plain errors collapse to 404 (the safer default for public serving).
type Handler ¶
Handler implements the HTTP shim over a Resolver. It reads the catch-all remainder the router stashed on the request URI, asks the Resolver for bytes + mime, and streams the response.
func (*Handler) ServeHTTP ¶
func (h *Handler) ServeHTTP(reqCtx request.RequestContext)
ServeHTTP satisfies coco-server's HandlerInterface.
type Resolver ¶
Resolver turns the URL's catch-all path remainder into the content to serve. What the remainder means is entirely up to the resolver — a disk dir, a DB lookup, an object store, anything.
Returning an error causes the Handler to respond 404 by default. If the error is an HTTPError (see errors.go) the Handler honours its Status.