rest

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2023 License: MIT Imports: 9 Imported by: 0

README

rest package

This package includes definitions and implementation of a web server with the following capabilities:

  • REST server
  • Web Socket server
  • REST utilities
  • Static files server

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultRestHandlerWrapperFunc

func DefaultRestHandlerWrapperFunc(entry RestEntry) http.HandlerFunc

func GetBoolParamValue

func GetBoolParamValue(r *http.Request, paramName string, defaultValue bool) (res bool)

GetBoolParamValue extracts parameter value from query string as bool

func GetEnumParamArray

func GetEnumParamArray(r *http.Request, paramName string, enum interface{}) (res []int)

GetEnumParamArray extracts parameter array values from query string as enums to support multiple values query string e.g. https//some/domain?id=1&id=2&id=3

func GetEnumParamValue

func GetEnumParamValue(r *http.Request, paramName string, enum interface{}, defaultValue int) (res int)

GetEnumParamValue extracts parameter value from query string as enum Enum value can be passed as it's int or string value

func GetFloatParamValue

func GetFloatParamValue(r *http.Request, paramName string, defaultValue float64) (res float64)

GetFloatParamValue extract parameter value from query string as float64

func GetInt64ParamValue

func GetInt64ParamValue(r *http.Request, paramName string, defaultValue int64) (res int64)

GetInt64ParamValue extract parameter value from query string as long

func GetIntParamArray

func GetIntParamArray(r *http.Request, paramName string) (res []int)

GetIntParamArray extract parameter array values from query string to support multiple values query string e.g. https//some/domain?id=1&id=2&id=3

func GetIntParamValue

func GetIntParamValue(r *http.Request, paramName string, defaultValue int) (res int)

GetIntParamValue extract parameter value from query string as int

func GetIntParamValueFromPath

func GetIntParamValueFromPath(r *http.Request, paramName string, defaultValue int) int

GetIntParamValueFromPath extracts parameter value from path params as int

func GetStringParamArray

func GetStringParamArray(r *http.Request, paramName string) (res []string)

GetStringParamArray extracts parameter array values from query string to support multiple values query string e.g. https//some/domain?id=1&id=2&id=3

func GetStringParamValue

func GetStringParamValue(r *http.Request, paramName string, defaultValue string) (res string)

GetStringParamValue extract parameter value from query string as string

func GetStringParamValueFromPath

func GetStringParamValueFromPath(r *http.Request, paramName string, defaultValue string) (res string)

GetStringParamValueFromPath extracts parameter value from path params as string

func GetUInt64ParamArray

func GetUInt64ParamArray(r *http.Request, paramName string) (res []uint64)

GetUInt64ParamArray extract parameter array values from query string This supports multiple values query string e.g. https//some/domain?id=1&id=2&id=3

func GetUint64ParamValue

func GetUint64ParamValue(r *http.Request, paramName string, defaultValue uint64) (res uint64)

GetUint64ParamValue extract parameter value from query string as unsigned-long

func ResolveRemoteIpFromHttpRequest

func ResolveRemoteIpFromHttpRequest(r *http.Request) (ip string)

ResolveRemoteIpFromHttpRequest extracts remote ip from HTTP header X-Forwarded-For

Types

type ActionResponse

type ActionResponse struct {
	BaseRestResponse
	Key  string `json:"key,omitempty"`  // The entity key (Id)
	Data string `json:"data,omitempty"` // Additional data
}

ActionResponse message is returned for any action on entity with no return data (e.d. delete)

func NewActionResponse

func NewActionResponse(key, data string) (er *ActionResponse)

NewActionResponse factory method

func NewActionResponseError

func NewActionResponseError(err error) (res *ActionResponse)

NewActionResponseError with error

type BaseRestResponse

type BaseRestResponse struct {
	Code  int    `json:"code"`            // Error code (0 for success)
	Error string `json:"error,omitempty"` // Error message
}

BaseRestResponse is a common structure for all response types

func (*BaseRestResponse) SetError

func (res *BaseRestResponse) SetError(err error)

type EntitiesResponse

type EntitiesResponse struct {
	BaseRestResponse
	Page     int      `json:"page"`     // Current page (Bulk) number
	PageSize int      `json:"pageSize"` // Size of page (items in bulk)
	Pages    int      `json:"pages"`    // Total number of pages
	Total    int      `json:"total"`    // Total number of items in the query
	List     []Entity `json:"list"`     // List of objects in the current result set
}

EntitiesResponse message is returned for any action returning multiple entities

func NewEntitiesResponse

func NewEntitiesResponse(entities []Entity, page, pageSize, total int) *EntitiesResponse

NewEntitiesResponse factory method

func NewEntitiesResponseError

func NewEntitiesResponseError(err error) *EntitiesResponse

NewEntitiesResponseError with error

type EntityResponse

type EntityResponse struct {
	BaseRestResponse
	Entity Entity `json:"entity"` // The entity
}

EntityResponse message is returned for any create/update action on entity

func NewEntityResponse

func NewEntityResponse(entity Entity) (er *EntityResponse)

NewEntityResponse factory method

func NewEntityResponseError

func NewEntityResponseError(err error) (res *EntityResponse)

NewEntityResponseError with error

type IRestEndpoint

type IRestEndpoint interface {
	Entries() []RestEntry
}

func NewRestEndpoint

func NewRestEndpoint(entries []RestEntry) IRestEndpoint

type IRestResponse

type IRestResponse interface {
	GetErrorCode() int
	GetErrorMessage() string
	SetError(err error)
	GetRawContent() []byte
	SetRawContent(content []byte)
	GetMimeType() string
	SetMimeType(mimeType string)
	GetHeaders() map[string]string
	SetHeader(key string, value string)
}

IRestResponse is an interface that all rest response messages must comply

type IStaticEndpoint

type IStaticEndpoint interface {
	Entries() []StaticFilesEntry
}

func NewStaticEndpoint

func NewStaticEndpoint(entries []StaticFilesEntry) IStaticEndpoint

type RequestWithToken

type RequestWithToken struct {
	*http.Request
	Token          string
	ResponseWriter http.ResponseWriter
}

RequestWithToken includes access to the JWT token

func NewRequestWithToken

func NewRequestWithToken(rw http.ResponseWriter, r *http.Request, token string) *RequestWithToken

NewRequestWithToken is the factory method of RequestWithToken

type RestEndpoint

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

func (RestEndpoint) Entries

func (r RestEndpoint) Entries() []RestEntry

type RestEntries

type RestEntries []*RestEntry

func (RestEntries) Len

func (a RestEntries) Len() int

func (RestEntries) Less

func (a RestEntries) Less(i, j int) bool

func (RestEntries) Swap

func (a RestEntries) Swap(i, j int)

type RestEntry

type RestEntry struct {
	Path    string      // Rest method path
	Method  string      // HTTP method verb
	Handler RestHandler // Handler function (http.HandlerFunc)
}

type RestHandler

type RestHandler func(r *RequestWithToken) (IRestResponse, error)

RestHandler function signature

type RestHandlerAdaptorFunc

type RestHandlerAdaptorFunc func(entry RestEntry) http.HandlerFunc

RestHandlerAdaptorFunc function signature

type StaticEndpoint

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

func (StaticEndpoint) Entries

func (r StaticEndpoint) Entries() []StaticFilesEntry

type StaticFilesEntry

type StaticFilesEntry struct {
	Path   string // Static content path
	Folder string // Static content folder
}

Jump to

Keyboard shortcuts

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