fileserver

package
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 8 Imported by: 0

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

View Source
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.

func (*DiskResolver) Resolve

func (d *DiskResolver) Resolve(remainingPath string) ([]byte, string, error)

Resolve implements the Resolver interface.

type HTTPError

type HTTPError struct {
	Status  int
	Message string
}

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).

func (*HTTPError) Error

func (e *HTTPError) Error() string

type Handler

type Handler struct {
	Resolver    Resolver
	CacheMaxAge int // seconds; 0 = no Cache-Control header.
}

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

type Resolver interface {
	Resolve(remainingPath string) (data []byte, mimeType string, err error)
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL