restserver

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 25 Imported by: 2

Documentation

Index

Constants

View Source
const (
	DefaultTestUserId   = "5e859dae-c879-11eb-b8bc-0242ac130003"
	DefaultTestTenantId = "5e859dae-c879-11eb-b8bc-0242ac130004"
)

Variables

View Source
var (
	UserUnauthenticatedError = errors.New("user not authenticated")
)

Functions

func AddRoutes

func AddRoutes(routes []Route)

AddRoutes add list of routes in the webrest server

func CustomAuthMiddleware added in v0.0.3

func CustomAuthMiddleware(fn CustomAuthenticationMiddleware)

func ListenAndServe

func ListenAndServe()

ListenAndServe initialize, configure and expose the web rest server

func Use

func Use(m CustomMiddleware)

Types

type CustomAuthenticationMiddleware added in v0.0.3

type CustomAuthenticationMiddleware interface {
	Apply(ctx WebContext) (*security.AuthenticationContext, error)
}

type CustomMiddleware

type CustomMiddleware interface {
	Apply(ctx WebContext) *MiddlewareError
}

type Error

type Error struct {
	Error string `json:"error"`
}

Error is the structure from default error response

type MiddlewareError

type MiddlewareError struct {
	Err        error `json:"error"`
	StatusCode int   `json:"statusCode"`
}

func NewMiddlewareError

func NewMiddlewareError(statusCode int, err error) *MiddlewareError

func (MiddlewareError) Error

func (e MiddlewareError) Error() string

type RequestTest

type RequestTest struct {
	// Method http method
	Method string
	// Url to call in test
	Url string
	// Path to register endpoint
	Path string
	// Request headers
	Headers map[string]string
	// Body request
	Body string
	// UploadFile file to upload in multipart form file
	UploadFile *os.File
	// FormFileField field name
	FormFileField string
	// FormFileName file name
	FormFileName string
}

RequestTest is a contract to test http requests

type Route

type Route struct {
	URI         string
	Method      string
	Prefix      RoutePrefix
	Function    func(ctx WebContext)
	BeforeEnter func(ctx WebContext) *MiddlewareError
}

Route is the structure from inject the routes in the http router

type RoutePrefix

type RoutePrefix string

RoutePrefix is the type from default's routes

const (
	PublicApi        RoutePrefix = "/public/"
	PrivateApi       RoutePrefix = "/private/"
	AuthenticatedApi RoutePrefix = "/api/"
	NoPrefix         RoutePrefix = "/"
)

type Server

type Server interface {
	// contains filtered or unexported methods
}

Server is the contract to http server implementation

type TestResponse

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

TestResponse is a contract to test http requests responses

func NewRequestTest

func NewRequestTest(request *RequestTest, handlerFn func(ctx WebContext)) *TestResponse

NewRequestTest returns a TestResponse with result of test execution

func (*TestResponse) DecodeBody

func (r *TestResponse) DecodeBody(object any) error

DecodeBody returns the result body decoded

func (*TestResponse) Header

func (r *TestResponse) Header(key string) string

Header get header by key

func (*TestResponse) Headers

func (r *TestResponse) Headers() http.Header

Headers returns response headers

func (*TestResponse) RawBody

func (r *TestResponse) RawBody() io.ReadCloser

RawBody returns raw body

func (*TestResponse) StatusCode

func (r *TestResponse) StatusCode() int

StatusCode returns the result status code

func (*TestResponse) StringBody

func (r *TestResponse) StringBody() (string, error)

StringBody return a string body

type WebContext

type WebContext interface {
	// Context returns the call context
	Context() context.Context
	// AuthenticationContext returns a pointer of authentication context if exists
	AuthenticationContext() *security.AuthenticationContext

	//RequestHeader returns a slice of http request header for key
	RequestHeader(key string) []string
	// RequestHeaders returns a slice of http request header by key
	RequestHeaders() map[string][]string
	// PathParam returns an url path param for key
	PathParam(key string) string
	// QueryParam returns a url query param for key
	QueryParam(key string) string
	// QueryArrayParam returns a url query array param for key
	QueryArrayParam(key string) []string
	// DecodeQueryParams converts all url query params into structured object
	DecodeQueryParams(object any) error
	// DecodeBody converts http request body into structured object
	DecodeBody(object any) error
	// StringBody return request raw body
	StringBody() (string, error)
	// Path return request path
	Path() string
	// FormFile returns the first file for the provided form key.
	FormFile(key string) (multipart.File, *multipart.FileHeader, error)

	// AddHeader add header into http response
	AddHeader(key string, value string)
	// AddHeaders add map of headers into http response
	AddHeaders(headers map[string]string)
	// Redirect http response with redirect headers
	Redirect(url string, statusCode int)
	// ServeFile serve a file into http response
	ServeFile(path string)
	// JsonResponse converts any object on json http response body
	JsonResponse(statusCode int, body any)
	// ErrorResponse converts any error on structured error http json response body
	ErrorResponse(statusCode int, err error)
	// EmptyResponse returns empty http json response body
	EmptyResponse(statusCode int)
}

WebContext is the contract to implements the web context in the webrest server

Jump to

Keyboard shortcuts

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