web

package
v0.0.0-...-ee95db1 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package web is a super minimalistic web/http server library

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PathLast

func PathLast(r *http.Request) (string, int)

PathLast returns the last segment in the path and the number of path segments

func TemplateParseFSRecursive

func TemplateParseFSRecursive(
	templates fs.FS,
	ext string,
	nonRootTemplateNames bool,
	funcMap template.FuncMap) (*template.Template, error)

TemplateParseFSRecursive recursively parses all templates in the FS with the given extension. File paths are used as template names to support duplicate file names. Use nonRootTemplateNames to exclude root directory from template names (e.g. index.html instead of templates/index.html)

Types

type Action

type Action func(r *http.Request) *Response

Action represents a simplified http action that implements http.Handler

func (Action) ServeHTTP

func (a Action) ServeHTTP(rw http.ResponseWriter, r *http.Request)

Action's http.Handler implementation

type Headers

type Headers map[string]string

Headers is a map of string to string where the key is the key for the header And the value is the value for the header

type Hyperlink struct {
	Rel  string `json:"rel"`
	Href string `json:"href"`
}

Hyperlink represents a hyperlink

type Response

type Response struct {
	// StatusCode
	Status int
	// Content Type to writer
	ContentType string
	// Content to be written to the response writer
	Content io.Reader
	// Headers to be written to the response writer
	Headers Headers
}

Response is a generic response object for our handlers

func Data

func Data(status int, content []byte, headers Headers) *Response

Data returns a data response

func DataJSON

func DataJSON(status int, v interface{}, headers Headers) *Response

DataJSON returns a data response in json format

func EmbeddedJS

func EmbeddedJS(status int, t *template.Template, template string, data interface{}, headers Headers) *Response

JS renders an embedded script to a web response

func Empty

func Empty(status int) *Response

Empty returns an empty http response

func Error

func Error(status int, err error, headers Headers) *Response

Error returns an error response

func ErrorJSON

func ErrorJSON(status int, err error, headers Headers) *Response

ErrorJSON returns an error in json format

func HTML

func HTML(status int, t *template.Template, template string, data interface{}, headers Headers) *Response

HTML renders an html template to a web response

func (*Response) Write

func (response *Response) Write(rw http.ResponseWriter)

Write writes a response to an http.ResponseWriter

Jump to

Keyboard shortcuts

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