Documentation
¶
Overview ¶
Package http is a generated GoMock package.
Index ¶
- Constants
- Variables
- type Authentication
- type Authenticator
- type Context
- func (ctx *Context[T]) Deadline() (deadline time.Time, ok bool)
- func (ctx *Context[T]) Done() <-chan struct{}
- func (ctx *Context[T]) Err() error
- func (ctx *Context[T]) GetAuthentication() Authentication
- func (ctx *Context[T]) GetBase() T
- func (ctx *Context[T]) GetLogger() log.Logger
- func (ctx *Context[T]) GetMethod() string
- func (ctx *Context[T]) GetRequest() *http.Request
- func (ctx *Context[T]) GetResponse() http.ResponseWriter
- func (ctx *Context[T]) GetRoute() *RouteNode[T]
- func (ctx *Context[T]) GetSession() string
- func (ctx *Context[T]) GetUrl() *url.URL
- func (ctx *Context[T]) HasError() bool
- func (ctx *Context[T]) Read() ([]byte, error)
- func (ctx *Context[T]) ReadObject(target interface{}) error
- func (ctx *Context[T]) ReadQueryParams(target interface{}) error
- func (ctx *Context[T]) SetAuthentication(auth Authentication)
- func (ctx *Context[T]) SetBase(base T)
- func (ctx *Context[T]) SetError(err *qerror.RestError, status int)
- func (ctx *Context[T]) SetLogger(logger log.Logger) log.Logger
- func (ctx *Context[T]) SetRedirect(model interface{}, location string) bool
- func (ctx *Context[T]) SetResponse(model interface{}, status int) bool
- func (ctx *Context[T]) SetSession(prefix generator.IDPrefix)
- func (ctx *Context[T]) Status() int
- func (ctx *Context[T]) Value(key any) any
- func (ctx *Context[T]) Write(bytes []byte) error
- type ContextBase
- type Controller
- type DefaultBase
- func (c *DefaultBase) GetAuthentication() Authentication
- func (c *DefaultBase) GetLogger() log.Logger
- func (c *DefaultBase) GetSession() string
- func (c *DefaultBase) SetAuthentication(auth Authentication)
- func (c *DefaultBase) SetLogger(logger log.Logger) log.Logger
- func (c *DefaultBase) SetSession(prefix generator.IDPrefix)
- type MockContextBase
- func (m *MockContextBase) Deadline() (time.Time, bool)
- func (m *MockContextBase) Done() <-chan struct{}
- func (m *MockContextBase) EXPECT() *MockContextBaseMockRecorder
- func (m *MockContextBase) Err() error
- func (m *MockContextBase) GetAuthentication() Authentication
- func (m *MockContextBase) GetLogger() log.Logger
- func (m *MockContextBase) GetSession() string
- func (m *MockContextBase) SetAuthentication(auth Authentication)
- func (m *MockContextBase) SetLogger(logger log.Logger) log.Logger
- func (m *MockContextBase) SetSession(prefix generator.IDPrefix)
- func (m *MockContextBase) Value(key any) any
- type MockContextBaseMockRecorder
- func (mr *MockContextBaseMockRecorder) Deadline() *gomock.Call
- func (mr *MockContextBaseMockRecorder) Done() *gomock.Call
- func (mr *MockContextBaseMockRecorder) Err() *gomock.Call
- func (mr *MockContextBaseMockRecorder) GetAuthentication() *gomock.Call
- func (mr *MockContextBaseMockRecorder) GetLogger() *gomock.Call
- func (mr *MockContextBaseMockRecorder) GetSession() *gomock.Call
- func (mr *MockContextBaseMockRecorder) SetAuthentication(auth any) *gomock.Call
- func (mr *MockContextBaseMockRecorder) SetLogger(logger any) *gomock.Call
- func (mr *MockContextBaseMockRecorder) SetSession(prefix any) *gomock.Call
- func (mr *MockContextBaseMockRecorder) Value(key any) *gomock.Call
- type NewContextBase
- type RESTServer
- type RouteNode
- type Router
Constants ¶
const HealthCheckRoute = "health"
HealthCheckRoute is the default URI for health checks
const InvalidCredentialsErrorMessage = "Invalid Credentials"
InvalidCredentialsErrorMessage is returned when Credentials are invalid
Variables ¶
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]) GetAuthentication ¶ added in v2.4.0
func (ctx *Context[T]) GetAuthentication() Authentication
func (*Context[T]) GetRequest ¶ added in v2.4.0
func (*Context[T]) GetResponse ¶ added in v2.4.0
func (ctx *Context[T]) GetResponse() http.ResponseWriter
func (*Context[T]) GetSession ¶ added in v2.4.0
func (*Context[T]) Read ¶
Read reads the entire body of the request and returns it as a slice of bytes
func (*Context[T]) ReadObject ¶
ReadObject reads the body of the Request and unmarshals an object the same type as the passed implementation of interface{}
func (*Context[T]) ReadQueryParams ¶
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]) SetError ¶
SetError sets the HTTP status of the response and the error to be returned
func (*Context[T]) SetRedirect ¶ added in v2.2.5
func (*Context[T]) SetResponse ¶
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
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
func (m *MockContextBase) EXPECT() *MockContextBaseMockRecorder
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
func (mr *MockContextBaseMockRecorder) Done() *gomock.Call
Done indicates an expected call of Done.
func (*MockContextBaseMockRecorder) Err ¶ added in v2.4.0
func (mr *MockContextBaseMockRecorder) Err() *gomock.Call
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.
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.