Package server

import "github.com/matrix-org/go-neb/server"

Overview ▾

Package server contains building blocks for REST APIs.

func MakeJSONAPI

func MakeJSONAPI(handler JSONRequestHandler) http.HandlerFunc

MakeJSONAPI creates an HTTP handler which always responds to incoming requests with JSON responses.

func Protect

func Protect(handler http.HandlerFunc) http.HandlerFunc

Protect panicking HTTP requests from taking down the entire process, and log them using the correct logger, returning a 500 with a JSON response rather than abruptly closing the connection.

func SetCORSHeaders

func SetCORSHeaders(w http.ResponseWriter)

SetCORSHeaders sets unrestricted origin Access-Control headers on the response writer

func WithCORSOptions

func WithCORSOptions(handler http.HandlerFunc) http.HandlerFunc

WithCORSOptions intercepts all OPTIONS requests and responds with CORS headers. The request handler is not invoked when this happens.

type JSONError

type JSONError struct {
    Message string `json:"message"`

JSONError represents a JSON API error response

type JSONRequestHandler

type JSONRequestHandler interface {
    OnIncomingRequest(req *http.Request) (interface{}, *errors.HTTPError)

JSONRequestHandler represents an interface that must be satisfied in order to respond to incoming HTTP requests with JSON. The interface returned will be marshalled into JSON to be sent to the client, unless the interface is []byte in which case the bytes are sent to the client unchanged. If an error is returned, a JSON error response will also be returned, unless the error code is a 302 REDIRECT in which case a redirect is sent based on the Message field.