content

package
v0.0.0-...-0c295b2 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: BSD-3-Clause Imports: 15 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(err error, code int) error

Error annotates an error with http status information.

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

func Server(fsys fs.FS, handlers ...*handler) http.Handler

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

Text renders an http status code as a text response.

func Template

func Template(w http.ResponseWriter, fsys fs.FS, tmplPath string, data any, code int) error

Template executes a template response.

func Text

func Text(w http.ResponseWriter, data any, code int) error

Text formats data as a text response with a status code.

Types

type HandlerFunc

type HandlerFunc func(http.ResponseWriter, *http.Request) error

func (HandlerFunc) ServeHTTP

func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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