web

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2021 License: Apache-2.0 Imports: 20 Imported by: 3

README

Procyon Web

Go Report Card codecov Build Status Gitter PkgGoDev

This gives you a basic understanding of Procyon Web Module. It covers components provided by the framework, such as Controller, Handler Registry, Web Request Context and Interceptors.

Note that you need to register your components by using the function core.Register.

Controller

It's used to define a struct as a Controller Component and register handler methods. If it is implemented by your struct, your struct will be considered as a Controller.

type Controller interface {
	RegisterHandlers(registry HandlerRegistry)
}

Handler Registry

It's used to register handler methods.

type HandlerRegistry interface {
	Register(info ...RequestHandler)
	RegisterGroup(prefix string, info ...RequestHandler)
}
  • Register is used to register a Request Handler.

  • RegisterGroup are used to register multiple Request Handlers.

Request Handler

They are used to get an instance of a request handler by request method.

func Get(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler
func Post(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler 
func Put(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler
func Delete(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler
func Patch(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler
func Options(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler
func Head(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler
Request Handler Options

Options are used to specify handler's properties like Path and Request Object.

func RequestObject(requestObject RequestHandlerObject) RequestHandlerOption
func Path(path string) RequestHandlerOption
  • RequestObject is used to specify the request object. If you have a request type, you have to register it. Otherwise, GetRequest will throw an error.
  • Path is used to specify the path.

Web Request Context

WebRequestContext implements the interface context.Context in procyon-context. That's why it has the methods the following.

func (ctx *WebRequestContext) GetContextId() context.ContextId
func (ctx *WebRequestContext) Get(key string) interface{}
func (ctx *WebRequestContext) Put(key string, value interface{})
  • GetContextId returns a Context Id. It is unique and consists of UUID. It can be used for logging.
  • Get returns the value from context by the given key. If it is not found, it returns nil.
  • Put an key-value pair into context.
func (ctx *WebRequestContext) Next()
  • Next can only be invoked from HandleBefore. If you call it from other methods or functions, nothing will happen. When Next is invoked, it calls next interceptor implementing HandlerInterceptorBefore.
func (ctx *WebRequestContext) GetRequest(request interface{})
func (ctx *WebRequestContext) GetPathVariable(name string) string
func (ctx *WebRequestContext) GetRequestParameter(name string) string
func (ctx *WebRequestContext) GetHeaderValue(key string) string
  • GetRequest is used to bind the request data to the instance of the request object.
  • GetPathVariable is used to get the path variable by name
  • GetRequestParameter is used to get the request parameter by name.
  • GetHeaderValue is used to get the header value by name.
func (ctx *WebRequestContext) GetStatus() int
func (ctx *WebRequestContext) SetStatus(status int) ResponseBodyBuilder

func (ctx *WebRequestContext) SetBody(body interface{}) ResponseBodyBuilder
func (ctx *WebRequestContext) GetBody() interface{}

func (ctx *WebRequestContext) GetContentType() MediaType 
func (ctx *WebRequestContext) SetContentType(mediaType MediaType) ResponseBodyBuilder

func (ctx *WebRequestContext) AddHeader(key string, value string) ResponseHeaderBuilder
  • GetStatus is used to get the status.
  • SetStatus is used to set the status of response.
  • SetBody is used to set the body of response.
  • GetBody is used to get the body of response.
  • GetContentType is used to get the content type.
  • SetContentType is used to set the content type
func (ctx *WebRequestContext) Ok() ResponseBodyBuilder
func (ctx *WebRequestContext) NotFound() ResponseHeaderBuilder
func (ctx *WebRequestContext) NoContent() ResponseHeaderBuilder
func (ctx *WebRequestContext) BadRequest() ResponseBodyBuilder
func (ctx *WebRequestContext) Accepted() ResponseBodyBuilder
func (ctx *WebRequestContext) Created(location string) ResponseBodyBuilder
  • Ok sets the status to 200.
  • NotFound sets the status to 404.
  • NoContent sets the status to 204.
  • BadRequest sets the status to 400.
  • Accepted sets the status to 202.
  • Created sets the status to 201.
func (ctx *WebRequestContext) GetError() error
func (ctx *WebRequestContext) SetError(err error)
func (ctx *WebRequestContext) ThrowError(err error)
  • GetError is used to get the error.
  • SetError is used to put the error into context.
  • ThrowError is used to throw an error. It's an alternative to SetError

Interceptors

Interceptors are used to manipulate requests and responses.

HandlerBefore,Handler Method and HandlerAfter, HandleAfterCompletion are invoked respectively.

Interceptor Before

If you want to do something before handler method is executed, implement the interface HandlerInterceptorBefore.

type HandlerInterceptorBefore interface {
	HandleBefore(requestContext *WebRequestContext)
}
Interceptor After

If you want to do something after handler method is executed, implement the interface HandlerInterceptorAfter.

type HandlerInterceptorAfter interface {
	HandleAfter(requestContext *WebRequestContext)
}
Interceptor After Completion

If you want to do something after the request process is completed, implement the interface HandlerInterceptorAfterCompletion. HandlerAfterCompletion is invoked after response is returned successfully or when any error occurs while request is processed. In case of an error, You can get the error from request context.

type HandlerInterceptorAfterCompletion interface {
	HandleAfterCompletion(requestContext *WebRequestContext)
}

License

Procyon Framework is released under version 2.0 of the Apache License

Documentation

Index

Constants

View Source
const (
	DefaultMediaTypeValue             = MediaTypeApplicationTextHtmlValue
	MediaTypeApplicationTextHtmlValue = "text/html"
	MediaTypeApplicationXmlValue      = "application/xml"
	MediaTypeApplicationJsonValue     = "application/json"
)
View Source
const DefaultWebServerPort uint = 8080

Variables

View Source
var (
	HttpErrorNoContent             = NewHTTPError(http.StatusNoContent)
	HttpErrorBadRequest            = NewHTTPError(http.StatusBadRequest)
	HttpErrorUnauthorized          = NewHTTPError(http.StatusUnauthorized)
	HttpErrorForbidden             = NewHTTPError(http.StatusForbidden)
	HttpErrorNotFound              = NewHTTPError(http.StatusNotFound)
	HttpErrorMethodNotAllowed      = NewHTTPError(http.StatusMethodNotAllowed)
	HttpErrorRequestTimeout        = NewHTTPError(http.StatusRequestTimeout)
	HttpErrorRequestEntityTooLarge = NewHTTPError(http.StatusRequestEntityTooLarge)
	HttpErrorUnsupportedMediaType  = NewHTTPError(http.StatusUnsupportedMediaType)
	HttpErrorTooManyRequests       = NewHTTPError(http.StatusTooManyRequests)

	HttpErrorInternalServerError = NewHTTPError(http.StatusInternalServerError)
	HttpErrorBadGateway          = NewHTTPError(http.StatusBadGateway)
	HttpErrorServiceUnavailable  = NewHTTPError(http.StatusServiceUnavailable)
)

Functions

This section is empty.

Types

type Controller

type Controller interface {
	RegisterHandlers(registry HandlerRegistry)
}

type DefaultErrorHandler

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

func NewDefaultErrorHandler

func NewDefaultErrorHandler(logger context.Logger) DefaultErrorHandler

func (DefaultErrorHandler) HandleError

func (handler DefaultErrorHandler) HandleError(err error, requestContext *WebRequestContext)

type ErrorHandler

type ErrorHandler interface {
	HandleError(err error, requestContext *WebRequestContext)
}

type HTTPError

type HTTPError struct {
	Code    int
	Message interface{}
}

func NewHTTPError

func NewHTTPError(code int, message ...interface{}) *HTTPError

func (*HTTPError) Error

func (err *HTTPError) Error() string

type HandlerChain

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

func NewHandlerChain

func NewHandlerChain(fun RequestHandlerFunction, interceptorRegistry HandlerInterceptorRegistry, metadata *RequestObjectMetadata) *HandlerChain

type HandlerFunction

type HandlerFunction func(requestContext *WebRequestContext)

type HandlerInterceptor

type HandlerInterceptor HandlerFunction

type HandlerInterceptorAfter

type HandlerInterceptorAfter interface {
	HandleAfter(requestContext *WebRequestContext)
}

type HandlerInterceptorAfterCompletion

type HandlerInterceptorAfterCompletion interface {
	AfterCompletion(requestContext *WebRequestContext)
}

type HandlerInterceptorBefore

type HandlerInterceptorBefore interface {
	HandleBefore(requestContext *WebRequestContext)
}

type HandlerInterceptorProcessor

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

func NewHandlerInterceptorProcessor

func NewHandlerInterceptorProcessor(interceptorRegistry HandlerInterceptorRegistry) HandlerInterceptorProcessor

func (HandlerInterceptorProcessor) AfterPeaInitialization

func (processor HandlerInterceptorProcessor) AfterPeaInitialization(peaName string, pea interface{}) (interface{}, error)

func (HandlerInterceptorProcessor) BeforePeaInitialization

func (processor HandlerInterceptorProcessor) BeforePeaInitialization(peaName string, pea interface{}) (interface{}, error)

type HandlerInterceptorRegistry

type HandlerInterceptorRegistry interface {
	RegisterHandlerInterceptor(interceptorInstance interface{})
	GetHandlerBeforeInterceptors() []HandlerInterceptor
	GetHandlerAfterInterceptors() []HandlerInterceptor
	GetHandlerAfterCompletionInterceptors() []HandlerInterceptor
}

type HandlerMapping

type HandlerMapping interface {
	RegisterHandlerMethod(path string, method RequestMethod, handlerFunc RequestHandlerFunction, metadata *RequestObjectMetadata)
	GetHandlerChain(ctx *WebRequestContext)
}

type HandlerRegistry

type HandlerRegistry interface {
	Register(info ...RequestHandler)
	RegisterGroup(prefix string, info ...RequestHandler)
}

type MappingRegistry

type MappingRegistry interface {
	Register(path string, method RequestMethod, handlerChain *HandlerChain)
	Find(ctx *WebRequestContext)
}

type MediaType

type MediaType byte
const (
	DefaultMediaType                       = MediaTypeApplicationTextHtml
	MediaTypeApplicationTextHtml MediaType = 0
	MediaTypeApplicationJson     MediaType = 1
	MediaTypeApplicationXml      MediaType = 2
)

type PathVariable

type PathVariable struct {
	Key   string
	Value string
}

type ProcyonRouter

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

func (*ProcyonRouter) Route

func (router *ProcyonRouter) Route(requestCtx *fasthttp.RequestCtx)

type ProcyonServerApplicationContext

type ProcyonServerApplicationContext struct {
	*context.BaseApplicationContext
	// contains filtered or unexported fields
}

func NewProcyonServerApplicationContext

func NewProcyonServerApplicationContext(appId context.ApplicationId, contextId context.ContextId) *ProcyonServerApplicationContext

func (*ProcyonServerApplicationContext) Configure

func (ctx *ProcyonServerApplicationContext) Configure()

func (*ProcyonServerApplicationContext) FinishConfigure

func (ctx *ProcyonServerApplicationContext) FinishConfigure()

func (*ProcyonServerApplicationContext) GetWebServer

func (ctx *ProcyonServerApplicationContext) GetWebServer() Server

func (*ProcyonServerApplicationContext) OnConfigure

func (ctx *ProcyonServerApplicationContext) OnConfigure()

type ProcyonWebServer

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

func NewProcyonWebServerForBenchmark

func NewProcyonWebServerForBenchmark(handlerRegistry SimpleHandlerRegistry) *ProcyonWebServer

func (*ProcyonWebServer) GetPort

func (server *ProcyonWebServer) GetPort() uint

func (*ProcyonWebServer) Handle

func (server *ProcyonWebServer) Handle(ctx *fasthttp.RequestCtx)

func (*ProcyonWebServer) Run

func (server *ProcyonWebServer) Run() error

func (*ProcyonWebServer) SetProperties

func (server *ProcyonWebServer) SetProperties(properties *configure.WebServerProperties)

func (*ProcyonWebServer) Stop

func (server *ProcyonWebServer) Stop() error

type RequestBinder

type RequestBinder interface {
	BindRequest(request interface{}, ctx *WebRequestContext) error
}

type RequestHandler

type RequestHandler struct {
	Path          string
	Method        RequestMethod
	HandlerFunc   RequestHandlerFunction
	RequestObject RequestHandlerObject
	// contains filtered or unexported fields
}

func Delete

func Get

func Options

func Options(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler

func Patch

func Post

func Put

type RequestHandlerFunction

type RequestHandlerFunction = func(context *WebRequestContext)

type RequestHandlerMapping

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

func NewRequestHandlerMapping

func NewRequestHandlerMapping(mappingRegistry MappingRegistry, interceptorRegistry HandlerInterceptorRegistry) RequestHandlerMapping

func (RequestHandlerMapping) GetHandlerChain

func (requestMapping RequestHandlerMapping) GetHandlerChain(ctx *WebRequestContext)

func (RequestHandlerMapping) RegisterHandlerMethod

func (requestMapping RequestHandlerMapping) RegisterHandlerMethod(path string, method RequestMethod, handlerFunc RequestHandlerFunction, metadata *RequestObjectMetadata)

type RequestHandlerMappingProcessor

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

func NewRequestHandlerMappingProcessor

func NewRequestHandlerMappingProcessor(mapping RequestHandlerMapping) RequestHandlerMappingProcessor

func (RequestHandlerMappingProcessor) AfterPeaInitialization

func (processor RequestHandlerMappingProcessor) AfterPeaInitialization(peaName string, pea interface{}) (interface{}, error)

func (RequestHandlerMappingProcessor) BeforePeaInitialization

func (processor RequestHandlerMappingProcessor) BeforePeaInitialization(peaName string, pea interface{}) (interface{}, error)

type RequestHandlerObject

type RequestHandlerObject interface{}

type RequestHandlerOption

type RequestHandlerOption func(handler *RequestHandler)

func Path

func Path(path string) RequestHandlerOption

func RequestObject

func RequestObject(requestObject RequestHandlerObject) RequestHandlerOption

type RequestMappingRegistry

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

func NewRequestMappingRegistry

func NewRequestMappingRegistry() RequestMappingRegistry

func (RequestMappingRegistry) Find

func (registry RequestMappingRegistry) Find(ctx *WebRequestContext)

func (RequestMappingRegistry) Register

func (registry RequestMappingRegistry) Register(path string, method RequestMethod, handlerChain *HandlerChain)

type RequestMethod

type RequestMethod string
const (
	RequestMethodGet     RequestMethod = http.MethodGet
	RequestMethodPost    RequestMethod = http.MethodPost
	RequestMethodPut     RequestMethod = http.MethodPut
	RequestMethodDelete  RequestMethod = http.MethodDelete
	RequestMethodPatch   RequestMethod = http.MethodPatch
	RequestMethodOptions RequestMethod = http.MethodOptions
	RequestMethodHead    RequestMethod = http.MethodHead
)

type RequestObjectCache

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

type RequestObjectMetadata

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

func ScanRequestObjectMetadata

func ScanRequestObjectMetadata(requestObject interface{}) *RequestObjectMetadata

type Response

type Response interface {
	GetResponseLocation() string
	GetResponseStatus() int
	GetModel() interface{}
	GetResponseBody() []byte
	GetResponseContentType() MediaType
	GetResponseHeader(key string) (string, bool)
}

type ResponseBodyBuilder

type ResponseBodyBuilder interface {
	ResponseHeaderBuilder
	SetResponseStatus(status int) ResponseBodyBuilder
	SetModel(body interface{}) ResponseBodyBuilder
	SetResponseContentType(mediaType MediaType) ResponseBodyBuilder
}

type ResponseBodyWriter

type ResponseBodyWriter interface {
	WriteResponseBody(ctx *WebRequestContext, responseWriter ResponseWriter) error
}

type ResponseEntity

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

type ResponseHeaderBuilder

type ResponseHeaderBuilder interface {
	AddResponseHeader(key string, value string) ResponseHeaderBuilder
}

type ResponseWriter

type ResponseWriter struct {
}

func (ResponseWriter) WriteResponse

func (responseWriter ResponseWriter) WriteResponse(ctx *WebRequestContext, body []byte)

type Router

type Router interface {
	Route(requestCtx *fasthttp.RequestCtx)
}

type RouterMethodTree

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

type RouterNodeType

type RouterNodeType byte
const (
	PathSegmentNode  RouterNodeType = 0
	PathVariableNode RouterNodeType = 1
	PathWildcardNode RouterNodeType = 2
)

type RouterPathNode

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

type RouterTree

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

func (*RouterTree) AddRoute

func (tree *RouterTree) AddRoute(path string, method RequestMethod, handlerChain *HandlerChain)

func (*RouterTree) Get

func (tree *RouterTree) Get(ctx *WebRequestContext)

func (*RouterTree) GetMethodTree

func (tree *RouterTree) GetMethodTree(method []byte) *RouterMethodTree

type Server

type Server interface {
	Run() error
	Stop() error
	SetProperties(properties *configure.WebServerProperties)
	GetPort() uint
}

type SimpleHandlerInterceptorRegistry

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

func NewSimpleHandlerInterceptorRegistry

func NewSimpleHandlerInterceptorRegistry() *SimpleHandlerInterceptorRegistry

func (*SimpleHandlerInterceptorRegistry) GetHandlerAfterCompletionInterceptors

func (registry *SimpleHandlerInterceptorRegistry) GetHandlerAfterCompletionInterceptors() []HandlerInterceptor

func (*SimpleHandlerInterceptorRegistry) GetHandlerAfterInterceptors

func (registry *SimpleHandlerInterceptorRegistry) GetHandlerAfterInterceptors() []HandlerInterceptor

func (*SimpleHandlerInterceptorRegistry) GetHandlerBeforeInterceptors

func (registry *SimpleHandlerInterceptorRegistry) GetHandlerBeforeInterceptors() []HandlerInterceptor

func (*SimpleHandlerInterceptorRegistry) RegisterHandlerInterceptor

func (registry *SimpleHandlerInterceptorRegistry) RegisterHandlerInterceptor(interceptor interface{})

type SimpleHandlerRegistry

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

func NewSimpleHandlerRegistry

func NewSimpleHandlerRegistry() SimpleHandlerRegistry

func (SimpleHandlerRegistry) Register

func (registry SimpleHandlerRegistry) Register(info ...RequestHandler)

func (SimpleHandlerRegistry) RegisterGroup

func (registry SimpleHandlerRegistry) RegisterGroup(prefix string, info ...RequestHandler)

type StandardWebEnvironment

type StandardWebEnvironment struct {
	core.StandardEnvironment
}

func NewStandardWebEnvironment

func NewStandardWebEnvironment() *StandardWebEnvironment

type Validator

type Validator interface {
	Validate(val interface{}) error
}

type WebRequestContext

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

func (*WebRequestContext) Accepted

func (ctx *WebRequestContext) Accepted() ResponseBodyBuilder

func (*WebRequestContext) AddResponseHeader

func (ctx *WebRequestContext) AddResponseHeader(key string, value string) ResponseHeaderBuilder

func (*WebRequestContext) BadRequest

func (ctx *WebRequestContext) BadRequest() ResponseBodyBuilder

func (*WebRequestContext) BindRequest

func (ctx *WebRequestContext) BindRequest(request interface{}) error

func (*WebRequestContext) Cancel

func (ctx *WebRequestContext) Cancel()

func (*WebRequestContext) Created

func (ctx *WebRequestContext) Created(location string) ResponseBodyBuilder

func (*WebRequestContext) Get

func (ctx *WebRequestContext) Get(key string) interface{}

func (*WebRequestContext) GetContextId

func (ctx *WebRequestContext) GetContextId() context.ContextId

func (*WebRequestContext) GetHTTPError

func (ctx *WebRequestContext) GetHTTPError() *HTTPError

func (*WebRequestContext) GetInternalError

func (ctx *WebRequestContext) GetInternalError() error

func (*WebRequestContext) GetModel

func (ctx *WebRequestContext) GetModel() interface{}

func (*WebRequestContext) GetPath

func (ctx *WebRequestContext) GetPath() string

func (*WebRequestContext) GetPathVariable

func (ctx *WebRequestContext) GetPathVariable(name string) (string, bool)

func (*WebRequestContext) GetRequestBody

func (ctx *WebRequestContext) GetRequestBody() []byte

func (*WebRequestContext) GetRequestHeader

func (ctx *WebRequestContext) GetRequestHeader(key string) (string, bool)

func (*WebRequestContext) GetRequestParameter

func (ctx *WebRequestContext) GetRequestParameter(name string) (string, bool)

func (*WebRequestContext) GetResponseBody

func (ctx *WebRequestContext) GetResponseBody() []byte

func (*WebRequestContext) GetResponseContentType

func (ctx *WebRequestContext) GetResponseContentType() MediaType

func (*WebRequestContext) GetResponseHeader

func (ctx *WebRequestContext) GetResponseHeader(key string) (string, bool)

func (*WebRequestContext) GetResponseLocation

func (ctx *WebRequestContext) GetResponseLocation() string

func (*WebRequestContext) GetResponseStatus

func (ctx *WebRequestContext) GetResponseStatus() int

func (*WebRequestContext) IsCanceled

func (ctx *WebRequestContext) IsCanceled() bool

func (*WebRequestContext) IsCompleted

func (ctx *WebRequestContext) IsCompleted() bool

func (*WebRequestContext) IsSuccess

func (ctx *WebRequestContext) IsSuccess() bool

func (*WebRequestContext) NoContent

func (ctx *WebRequestContext) NoContent() ResponseHeaderBuilder

func (*WebRequestContext) NotFound

func (ctx *WebRequestContext) NotFound() ResponseHeaderBuilder

func (*WebRequestContext) Ok

func (*WebRequestContext) Put

func (ctx *WebRequestContext) Put(key string, value interface{})

func (*WebRequestContext) SetHTTPError

func (ctx *WebRequestContext) SetHTTPError(err *HTTPError)

func (*WebRequestContext) SetModel

func (ctx *WebRequestContext) SetModel(model interface{}) ResponseBodyBuilder

func (*WebRequestContext) SetResponseContentType

func (ctx *WebRequestContext) SetResponseContentType(mediaType MediaType) ResponseBodyBuilder

func (*WebRequestContext) SetResponseStatus

func (ctx *WebRequestContext) SetResponseStatus(status int) ResponseBodyBuilder

func (*WebRequestContext) ThrowError

func (ctx *WebRequestContext) ThrowError(err error)

func (*WebRequestContext) Validate

func (ctx *WebRequestContext) Validate(val interface{}) error

Jump to

Keyboard shortcuts

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