restserver

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2025 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

This section is empty.

Functions

func AddRoutes

func AddRoutes(routes []Route)

AddRoutes add a list of routes in the webrest server

func CustomAuthMiddleware

func CustomAuthMiddleware(fn CustomAuthenticationMiddleware)

CustomAuthMiddleware add custom authentication middleware to the web server

func ListenAndServe

func ListenAndServe()

ListenAndServe initialize, configure and expose the web rest server

func Use

func Use(m CustomMiddleware)

Use add custom middleware to the web server

Types

type CustomAuthenticationMiddleware

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 the 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 specifies the path or endpoint for the route in the HTTP router.
	URI string
	// Method defines the HTTP method (e.g., GET, POST) associated with the route.
	Method string
	// Prefix specifies a common prefix for the routes.
	Prefix RoutePrefix
	// Function defines the handler function to be executed when the route is accessed.
	Function func(ctx WebContext)
	// BeforeEnter is a middleware function executed before entering the route handler; returns MiddlewareError for failures.
	BeforeEnter func(ctx WebContext) *MiddlewareError
}

Route represents an HTTP route with attributes for URI, method, prefix, handler function, and pre-execution middleware logic.

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 gets 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 a key
	RequestHeader(key string) []string
	// RequestHeaders returns a slice of http request header by key
	RequestHeaders() map[string][]string
	// PathParam returns a url path param for a 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 a structured object
	DecodeBody(object any) error
	// DecodeFormData converts http request form data into a structured object
	DecodeFormData(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)
	// FormValue returns the first value for the provided form key.
	FormValue(key string) string

	// AddHeader add header into http response
	AddHeader(key string, value string)
	// AddHeaders add a map of headers into an 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 implement 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