http

package
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package http is a generated GoMock package.

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

This section is empty.

Types

type Authentication

type Authentication interface {
	// GetKind of authentication this instance provides
	GetKind() string
	// GetUserID 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

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

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

type Context

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

	Request  *http.Request
	Response http.ResponseWriter
	Route    *RouteNode[T]

	Authentication Authentication
	Model          interface{}
	Error          *qerror.RestError

	Body string

	Extended map[string]interface{}
	// contains filtered or unexported fields
}

func CreateContext

func CreateContext[T ContextBase](base T, writer http.ResponseWriter, request *http.Request,
	router Router[T], logger log.Logger) *Context[T]

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

func (*Context[T]) Deadline added in v2.4.0

func (ctx *Context[T]) Deadline() (deadline time.Time, ok bool)

func (*Context[T]) Done added in v2.4.0

func (ctx *Context[T]) Done() <-chan struct{}

func (*Context[T]) Err added in v2.4.0

func (ctx *Context[T]) Err() error

func (*Context[T]) GetAuthentication added in v2.4.0

func (ctx *Context[T]) GetAuthentication() Authentication

func (*Context[T]) GetBase added in v2.4.0

func (ctx *Context[T]) GetBase() T

func (*Context[T]) GetLogger added in v2.4.0

func (ctx *Context[T]) GetLogger() log.Logger

func (*Context[T]) GetMethod added in v2.1.1

func (ctx *Context[T]) GetMethod() string

GetMethod from the request on this context

func (*Context[T]) GetRequest added in v2.4.0

func (ctx *Context[T]) GetRequest() *http.Request

func (*Context[T]) GetResponse added in v2.4.0

func (ctx *Context[T]) GetResponse() http.ResponseWriter

func (*Context[T]) GetRoute added in v2.4.0

func (ctx *Context[T]) GetRoute() *RouteNode[T]

func (*Context[T]) GetSession added in v2.4.0

func (ctx *Context[T]) GetSession() string

func (*Context[T]) GetUrl added in v2.1.1

func (ctx *Context[T]) GetUrl() *url.URL

GetUrl from the request on this context

func (*Context[T]) HasError

func (ctx *Context[T]) HasError() bool

HasError checks if there is an Error set on the Context

func (*Context[T]) Read

func (ctx *Context[T]) Read() ([]byte, error)

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

func (*Context[T]) ReadObject

func (ctx *Context[T]) 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[T]) ReadQueryParams

func (ctx *Context[T]) ReadQueryParams(target interface{}) error

ReadQueryParams converts URL Parameters into an Object

func (*Context[T]) SetAuthentication added in v2.4.0

func (ctx *Context[T]) SetAuthentication(auth Authentication)

func (*Context[T]) SetBase added in v2.4.0

func (ctx *Context[T]) SetBase(base T)

func (*Context[T]) SetError

func (ctx *Context[T]) SetError(err *qerror.RestError, status int)

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

func (*Context[T]) SetLogger added in v2.4.0

func (ctx *Context[T]) SetLogger(logger log.Logger) log.Logger

func (*Context[T]) SetRedirect added in v2.2.5

func (ctx *Context[T]) SetRedirect(model interface{}, location string) bool

func (*Context[T]) SetResponse

func (ctx *Context[T]) SetResponse(model interface{}, status int) bool

SetResponse and the HTTP status and result model, returns a boolean indicating whether this Context is in an error state.

func (*Context[T]) SetSession added in v2.4.0

func (ctx *Context[T]) SetSession(prefix generator.IDPrefix)

func (*Context[T]) Status

func (ctx *Context[T]) Status() int

Status returns the HTTP status of the response

func (*Context[T]) Value added in v2.4.0

func (ctx *Context[T]) Value(key any) any

func (*Context[T]) Write

func (ctx *Context[T]) Write(bytes []byte) error

Write writes a string to the response body.

type ContextBase added in v2.3.0

type ContextBase interface {
	context.Context
	// GetLogger for this context.
	GetLogger() log.Logger
	// SetLogger for this context as a sub-logger of the passed logger with the
	// session off this context.
	SetLogger(logger log.Logger) log.Logger
	// GetSession for this context identified in log messages
	GetSession() string
	// SetSession for this context's log messages
	SetSession(prefix generator.IDPrefix)
	// GetAuthentication for this context
	GetAuthentication() Authentication
	// SetAuthentication for this context
	SetAuthentication(auth Authentication)
}

ContextBase is an interface for base functionality that is common to all contexts

func NewDefaultBase added in v2.3.0

func NewDefaultBase(ctx context.Context) ContextBase

NewDefaultBase returns a default implementation of ContextBase

type Controller

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

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

type DefaultBase added in v2.3.0

type DefaultBase struct {
	context.Context
	// Session for this context identified in log messages
	Session string
	// contains filtered or unexported fields
}

DefaultBase is a default implementation of ContextBase

func (*DefaultBase) GetAuthentication added in v2.4.0

func (c *DefaultBase) GetAuthentication() Authentication

func (*DefaultBase) GetLogger added in v2.3.0

func (c *DefaultBase) GetLogger() log.Logger

func (*DefaultBase) GetSession added in v2.3.0

func (c *DefaultBase) GetSession() string

func (*DefaultBase) SetAuthentication added in v2.4.0

func (c *DefaultBase) SetAuthentication(auth Authentication)

func (*DefaultBase) SetLogger added in v2.3.0

func (c *DefaultBase) SetLogger(logger log.Logger) log.Logger

func (*DefaultBase) SetSession added in v2.3.0

func (c *DefaultBase) SetSession(prefix generator.IDPrefix)

type MockContextBase added in v2.4.0

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

MockContextBase is a mock of ContextBase interface.

func NewMockContextBase added in v2.4.0

func NewMockContextBase(ctrl *gomock.Controller) *MockContextBase

NewMockContextBase creates a new mock instance.

func (*MockContextBase) Deadline added in v2.4.0

func (m *MockContextBase) Deadline() (time.Time, bool)

Deadline mocks base method.

func (*MockContextBase) Done added in v2.4.0

func (m *MockContextBase) Done() <-chan struct{}

Done mocks base method.

func (*MockContextBase) EXPECT added in v2.4.0

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockContextBase) Err added in v2.4.0

func (m *MockContextBase) Err() error

Err mocks base method.

func (*MockContextBase) GetAuthentication added in v2.4.0

func (m *MockContextBase) GetAuthentication() Authentication

GetAuthentication mocks base method.

func (*MockContextBase) GetLogger added in v2.4.0

func (m *MockContextBase) GetLogger() log.Logger

GetLogger mocks base method.

func (*MockContextBase) GetSession added in v2.4.0

func (m *MockContextBase) GetSession() string

GetSession mocks base method.

func (*MockContextBase) SetAuthentication added in v2.4.0

func (m *MockContextBase) SetAuthentication(auth Authentication)

SetAuthentication mocks base method.

func (*MockContextBase) SetLogger added in v2.4.0

func (m *MockContextBase) SetLogger(logger log.Logger) log.Logger

SetLogger mocks base method.

func (*MockContextBase) SetSession added in v2.4.0

func (m *MockContextBase) SetSession(prefix generator.IDPrefix)

SetSession mocks base method.

func (*MockContextBase) Value added in v2.4.0

func (m *MockContextBase) Value(key any) any

Value mocks base method.

type MockContextBaseMockRecorder added in v2.4.0

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

MockContextBaseMockRecorder is the mock recorder for MockContextBase.

func (*MockContextBaseMockRecorder) Deadline added in v2.4.0

func (mr *MockContextBaseMockRecorder) Deadline() *gomock.Call

Deadline indicates an expected call of Deadline.

func (*MockContextBaseMockRecorder) Done added in v2.4.0

Done indicates an expected call of Done.

func (*MockContextBaseMockRecorder) Err added in v2.4.0

Err indicates an expected call of Err.

func (*MockContextBaseMockRecorder) GetAuthentication added in v2.4.0

func (mr *MockContextBaseMockRecorder) GetAuthentication() *gomock.Call

GetAuthentication indicates an expected call of GetAuthentication.

func (*MockContextBaseMockRecorder) GetLogger added in v2.4.0

func (mr *MockContextBaseMockRecorder) GetLogger() *gomock.Call

GetLogger indicates an expected call of GetLogger.

func (*MockContextBaseMockRecorder) GetSession added in v2.4.0

func (mr *MockContextBaseMockRecorder) GetSession() *gomock.Call

GetSession indicates an expected call of GetSession.

func (*MockContextBaseMockRecorder) SetAuthentication added in v2.4.0

func (mr *MockContextBaseMockRecorder) SetAuthentication(auth any) *gomock.Call

SetAuthentication indicates an expected call of SetAuthentication.

func (*MockContextBaseMockRecorder) SetLogger added in v2.4.0

func (mr *MockContextBaseMockRecorder) SetLogger(logger any) *gomock.Call

SetLogger indicates an expected call of SetLogger.

func (*MockContextBaseMockRecorder) SetSession added in v2.4.0

func (mr *MockContextBaseMockRecorder) SetSession(prefix any) *gomock.Call

SetSession indicates an expected call of SetSession.

func (*MockContextBaseMockRecorder) Value added in v2.4.0

func (mr *MockContextBaseMockRecorder) Value(key any) *gomock.Call

Value indicates an expected call of Value.

type NewContextBase added in v2.3.0

type NewContextBase[T ContextBase] func(context.Context) T

NewContextBase for an incoming request

type RESTServer

type RESTServer[T ContextBase] struct {
	Address string
	Port    int
	Router  Router[T]
	// contains filtered or unexported fields
}

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

func CreateRESTServer

func CreateRESTServer[T ContextBase](address string, rootController Controller[T],
	newContext NewContextBase[T]) RESTServer[T]

CreateRESTServer initializes a RESTServer struct and returns it.

func (*RESTServer[T]) CompleteRequest

func (server *RESTServer[T]) CompleteRequest(start time.Time, ctx *Context[T])

CompleteRequest generates output and completes the Request

func (*RESTServer[T]) ListenAndServe

func (server *RESTServer[T]) ListenAndServe() error

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

func (*RESTServer[T]) ListenAndServeTLS

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

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

func (*RESTServer[T]) ServeHTTP

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

ServeHTTP processes the HTTP Request

type RouteNode

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

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

type Router

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

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

func CreateRouter

func CreateRouter[T ContextBase](rootController Controller[T]) Router[T]

CreateRouter initializes and returns a new instance of Router.

func (*Router[T]) AddController

func (router *Router[T]) AddController(controller Controller[T]) error

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

func (*Router[T]) AddRoute

func (router *Router[T]) AddRoute(route string, controller Controller[T]) 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[T]) FindRouteForPath

func (router *Router[T]) FindRouteForPath(path string) (*RouteNode[T], 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