web

package
v0.24.8 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2024 License: BSD-3-Clause Imports: 8 Imported by: 1

Documentation

Overview

Package web provides utilities for working with go's net/http package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleCors

func HandleCors(allowedOrigin string, h http.Handler) http.Handler

Allow some remote origin access to the specified handler.

func HandleResource

func HandleResource(root Resource) http.HandlerFunc

HandleResource turns a Resource into an http.HandlerFunc; providing responses to http get and post requests.

func HandleResourceWithContext

func HandleResourceWithContext(root Resource, xform func(context.Context) context.Context) http.HandlerFunc

HandleResource turns a Resource into an http.HandlerFunc; providing responses to http get and post requests.

Types

type Error

type Error struct {
	Message error
}

Error generates an error for every endpoint.

func (Error) Find

func (Error) Find(string) Resource

func (Error) Get

Read a resource

func (Error) Post

Alter a resource

func (Error) Put

Receive a resource

type MethodHandler

type MethodHandler map[string]http.Handler

implements a handler per method. ( doesnt attempt to validate method names, and doesn't notice duplicate registration. last one wins )

func (MethodHandler) ServeHTTP

func (m MethodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Port

type Port int

implements the flag.Value interface for reading ports from the command line by default will return the default port ( and true )

func (Port) GetPort

func (p Port) GetPort(defaultPort int) (ret int, okay bool)

func (Port) Int

func (p Port) Int() (ret int)

func (*Port) Set

func (p *Port) Set(s string) (err error)

func (Port) String

func (p Port) String() string

type Resource

type Resource interface {
	// Return the named child resource
	Find(string) Resource
	// Read a resource
	Get(context.Context, http.ResponseWriter) error
	// Alter a resource
	Post(context.Context, io.Reader, http.ResponseWriter) error
	// Receive a resource
	Put(context.Context, io.Reader, http.ResponseWriter) error
}

Resource interfaces with a rest-ish endpoint, there are methods for each major verb. See also Wrapper which provides a function-based adapter for creating resources.

func FindResource

func FindResource(res Resource, path string) (ret Resource, err error)

FindResource expands the passed resource, using each element of the passed path in turn. Returns an error, PathError, describing the extent of the matched path.

type Status

type Status int

turn a status code into an error

func (Status) Error

func (s Status) Error() (ret string)

type Wrapper

type Wrapper struct {
	Finds func(string) Resource
	Gets  func(context.Context, http.ResponseWriter) error
	Posts func(context.Context, io.Reader, http.ResponseWriter) error
	Puts  func(context.Context, io.Reader, http.ResponseWriter) error
}

Turn one or more Resource compatible functions into a full interface implementation.

func (*Wrapper) Find

func (n *Wrapper) Find(child string) (ret Resource)

func (*Wrapper) Get

func (n *Wrapper) Get(ctx context.Context, w http.ResponseWriter) (err error)

func (*Wrapper) Post

func (n *Wrapper) Post(ctx context.Context, r io.Reader, w http.ResponseWriter) (err error)

func (*Wrapper) Put

func (n *Wrapper) Put(ctx context.Context, r io.Reader, w http.ResponseWriter) (err error)

Directories

Path Synopsis
Package js provides some simple json utilities: MapSlice, for preserving map key order when unmarshaling, and Builder for writing json-like data without marshaling.
Package js provides some simple json utilities: MapSlice, for preserving map key order when unmarshaling, and Builder for writing json-like data without marshaling.
Package markup converts a custom html-like markup into markdown-like text.
Package markup converts a custom html-like markup into markdown-like text.
Package useraction: handle long running actions initiated from the browser.
Package useraction: handle long running actions initiated from the browser.

Jump to

Keyboard shortcuts

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