http

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const HealthCheckRoute = "health"

HealthCheckRoute is the default URI for health checks

View Source
const InvalidCredentialsErrorMessage = "Invalid Credentials"

InvalidCredentialsErrorMessage is returned when Credentials are invalid

Variables

View Source
var (
	WildCard = "*"
	Slash    = "/"
)

WildCard character for paths.

Functions

func NewNoContentError

func NewNoContentError(path, method string) errors.TracerError

NewNoContentError instantiates a NoContentError with a stack trace

Types

type Authentication added in v1.1.0

type Authentication interface {
	// Type of authentication this instance provides
	GetKind() string
	// UserID that is requesting authentication
	GetUserID() string
	// GetValidity indicates whether the user id on this instance is authentic
	GetValidity() bool
}

Authentication provides a base for authentication implementations that are used as part of the http context chain.

type Authenticator added in v1.1.0

type Authenticator interface {
	// Authenticate the passed context and provide the authentication and bool indicating whether the
	// context is authenticated
	Authenticate(context *Context) (Authentication, bool)
	// SetUserAuthentication on the passed context for the passed user ID.
	SetUserAuthentication(context *Context, userID string) (Authentication, bool)
}

Authenticator is responsible for the initialization of a Authentication from a http context.

type Context

type Context struct {
	context.Context
	URIParameters map[string]string
	URLParameters url.Values
	URI           string
	URL           *url.URL
	Method        string

	Request  *http.Request
	Response http.ResponseWriter
	Route    *RouteNode

	Authentication Authentication
	Model          interface{}
	Error          *qerror.RestError

	Extended map[string]interface{}

	Body string
	// contains filtered or unexported fields
}

Context serves as a structure that tracks the state of a given http Request Response chain.

func CreateContext

func CreateContext(writer http.ResponseWriter, request *http.Request,
	router Router) *Context

CreateContext initializes a Context from the passed Response and Request pair, and router. The router is used for detemplating and populating the URIParameters

func (*Context) AddError

func (context *Context) AddError(err qerror.FieldError)

AddError adds an error detail to the context Primary use case is to add a series of validation type errors

func (*Context) HasError

func (context *Context) HasError() bool

HasError checks if there is an Error set on the Context

func (*Context) Read

func (context *Context) Read() ([]byte, error)

Read reads the entire body of the request and returns it as a slice of bytes

func (*Context) ReadObject

func (context *Context) ReadObject(target interface{}) error

ReadObject reads the body of the Request and unmarshals an object the same type as the passed implementation of interface{}

func (*Context) ReadQueryParams

func (context *Context) ReadQueryParams(target interface{}) error

ReadQueryParams converts URL Parameters into an Object

func (*Context) SetError

func (context *Context) SetError(err *qerror.RestError, status int)

SetError sets the HTTP status of the response and the error to be returned

func (*Context) SetResponse

func (context *Context) SetResponse(model interface{}, status int) bool

SetResponse sets the HTTP status and model to be rendered in the response write Returns false if there is an Error on the context otherwise true

func (*Context) Status

func (context *Context) Status() int

Status returns the HTTP status of the response

func (*Context) Write

func (context *Context) Write(s string)

Write writes a string to the response body.

type Controller

type Controller interface {
	// GetRoutes that this controller serves
	GetRoutes() []string
	Get(context *Context)
	Post(context *Context)
	Put(context *Context)
	Patch(context *Context)
	Delete(context *Context)
	Options(context *Context)
	Authenticate(context *Context) (Authentication, bool)
}

Controller is the main interface for the request handlers in the Router

type NoContentError

type NoContentError struct {
	RequestPath   string
	RequestMethod string
	// contains filtered or unexported fields
}

NoContentError is returned when Read is called and the Request has a 0 content length.

func (*NoContentError) Error

func (err *NoContentError) Error() string

func (*NoContentError) Trace

func (err *NoContentError) Trace() []string

Trace returns the stack trace for the error

type RESTServer

type RESTServer struct {
	Address string
	Port    int
	Router  Router
}

RESTServer is a struct for managing the configuration and start up of a http/s server using the routing and controller logic in this package.

func CreateRESTServer

func CreateRESTServer(address string, rootController Controller) RESTServer

CreateRESTServer initializes a RESTServer struct and returns it.

func (*RESTServer) CompleteRequest

func (server *RESTServer) CompleteRequest(start time.Time, context *Context)

CompleteRequest generates output and completes the Request

func (*RESTServer) ListenAndServe

func (server *RESTServer) ListenAndServe() error

ListenAndServe starts a http server listening on the address specified on the RESTServer instance.

func (*RESTServer) ListenAndServeTLS

func (server *RESTServer) ListenAndServeTLS(address string, port int) error

ListenAndServeTLS starts a https server listening on the address specified on the RESTServer instance.

func (*RESTServer) ServeHTTP

func (server *RESTServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP processes the HTTP Request

type RouteNode

type RouteNode struct {
	// The word value assigned to this node.
	Value string
	// Any subroutes nested below this node.
	SubRoutes map[string]*RouteNode
	// The route template that was mapped to this node (if termnial).
	TemplateRoute string
	// The Controller for this node if terminal.
	Controller Controller
}

RouteNode serves as a node in the parse tree for parsing incoming routes.

type Router

type Router struct {
	// Routes All routes currently mapped by the router.
	RouteTree        *RouteNode
	RegisteredRoutes []string
}

Router is the main entry point for the Quimby ReST API server.

func CreateRouter

func CreateRouter(rootController Controller) Router

CreateRouter initializes and returns a new instance of Router.

func (*Router) AddController

func (router *Router) AddController(controller Controller) error

AddController adds a the passed controller on the routes returned by the controllers GetRoutes method.

func (*Router) AddRoute

func (router *Router) AddRoute(route string, controller Controller) error

AddRoute adds a Controller at the specified route. This will not add the routes defined on the controller. Just the route passed.

func (*Router) FindRouteForPath

func (router *Router) FindRouteForPath(path string) (*RouteNode, error)

FindRouteForPath returns the controller that is currently assigned to the passed route. If no controller's route matches the passed path an error will be returned.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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