Documentation ¶
Overview ¶
Package content implements a basic web serving framework.
Content Server ¶
A content server is an http.Handler that serves requests from a file system. Use Server(fsys) to create a new content server.
The server is defined primarily by the content of its file system fsys, which holds files to be served. It renders markdown files and golang templates into HTML.
Page Rendering ¶
A request for a path like "/page" will search the file system for "page.md", "page.html", "page/index.md", and "page/index.html" and render HTML output for the first file found.
Partial templates with the extension ".tmpl" at the root of the file system and in the same directory as the requested page are included in the html/template execution step to allow for sharing and composing logic from multiple templates.
Markdown templates must have an html layout template set in the frontmatter section. The markdown content is available to the layout template as the field `{{.Content}}`.
Index ¶
- func Error(err error, code int) error
- func Handler(path string, h HandlerFunc) *handler
- func JSON(w http.ResponseWriter, data any, code int) error
- func Server(fsys fs.FS, handlers ...*handler) http.Handler
- func Status(w http.ResponseWriter, code int) error
- func Template(w http.ResponseWriter, fsys fs.FS, tmplPath string, data any, code int) error
- func Text(w http.ResponseWriter, data any, code int) error
- type HandlerFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Handler ¶
func Handler(path string, h HandlerFunc) *handler
func JSON ¶
func JSON(w http.ResponseWriter, data any, code int) error
JSON encodes data as JSON response with a status code.
func Server ¶
Server returns a handler that serves HTTP requests with the contents of the file system rooted at fsys. For requests to a path without an extension, the server will search fsys for markdown or html templates first by appending .md, .html, /index.md, and /index.html to the requested url path.
The default behavior of looking for templates within fsys can be overridden by using an optional set of content handlers.
For example, a server can be constructed for a file system with a single template, “index.html“, in a directory, “content“, and a handler:
fsys := os.DirFS("content") s := content.Server(fsys, content.Handler("/", func(w http.ReponseWriter, _ *http.Request) error { return content.Template(w, fsys, "index.html", nil, http.StatusOK) }))
or without a handler:
content.Server(os.DirFS("content"))
Both examples will render the template index.html for requests to "/".
func Status ¶
func Status(w http.ResponseWriter, code int) error
TODO(rfindley): this docstring is stale, and Status should be a pure function. Text renders an http status code as a text response.
Types ¶
type HandlerFunc ¶
type HandlerFunc func(http.ResponseWriter, *http.Request) error
func (HandlerFunc) ServeHTTP ¶
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)