rest

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2020 License: Apache-2.0 Imports: 10 Imported by: 4

README

REST utilities

GoDoc Build Status Go Report Card codecov

This is a (for now) small package with a few handy utilities when creating HTTP-based APIs:

  • A simple router to handle parameters in routes
  • A CORS filter

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCORSHeaders

func AddCORSHeaders(handler http.HandlerFunc) http.HandlerFunc

AddCORSHeaders adds CORS headers to the response.

func BasicAuthWrapper

func BasicAuthWrapper(realm string, authStore CredentialStore, handlerFunc http.HandlerFunc) http.HandlerFunc

BasicAuthWrapper wraps all requests with WWW-Authenticate headers and validates against the credential store.

func DefaultKeepAliveMessage

func DefaultKeepAliveMessage() interface{}

DefaultKeepAliveMessage is the default KeepAlive-message

func GetPathKey

func GetPathKey(name string, r *http.Request) string

GetPathKey is a simple utility function that will return the (string) value for the specified key in the path. If there's an error it will return an empty string.

func WebsocketHandler

func WebsocketHandler(streamer WebsocketStreamer) http.HandlerFunc

WebsocketHandler generates a http.HandlerFunc from the websocket adapter

Types

type CredentialStore

type CredentialStore interface {
	Valid(username, password string) bool
}

CredentialStore is a simple credential store

type MemoryCredentialStore

type MemoryCredentialStore struct {
	// contains filtered or unexported fields
}

MemoryCredentialStore is a memory-backed credential store. Suitable for testing, nothing more.

func NewMemoryCredentialStore

func NewMemoryCredentialStore(username, password string) *MemoryCredentialStore

NewMemoryCredentialStore creates a new MemoryCredentialStore instance with a single entry

func (*MemoryCredentialStore) Valid

func (m *MemoryCredentialStore) Valid(username, password string) bool

Valid validates user name and password

type ParameterRouter

type ParameterRouter struct {
	// contains filtered or unexported fields
}

ParameterRouter implements a request URI router that allows for path parameters in URIs, ie requests like https://example.com/api/thing/0424242/subthing/ - ie presenting resources with IDs in the request URI rather than having to rely on query parameters. The router can be plugged in in the standard http package.

func NewParameterRouter

func NewParameterRouter() ParameterRouter

NewParameterRouter creates a new router instance

func (*ParameterRouter) AddRoute

func (r *ParameterRouter) AddRoute(pattern string, handler http.HandlerFunc)

AddRoute adds a new route described by the specified pattern, handled by the supplied ParameterHandler. This method isn't thread safe.

func (*ParameterRouter) GetHandler

func (r *ParameterRouter) GetHandler(uri string) http.HandlerFunc

GetHandler returns a matching handler (as a closure) for the matching uri. If no matching route is found it will return the default http.NotFound handler.

type PathParameter

type PathParameter string

PathParameter is the type used when storing parameters in the context

type WebsocketStreamer

type WebsocketStreamer interface {
	Setup(r *http.Request) error
	Cleanup()
	Input() <-chan interface{}
	KeepaliveMessage() interface{}
}

WebsocketStreamer is an adapter type for websocket streams. They all follow a similar pattern: Set up request, get a channel for (more or less) realtime data and push data to the client whenever the channel gets something. A keepalive message is sent at regular intervals if there's no data.

Jump to

Keyboard shortcuts

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