rest

package
v0.0.0-...-cfeb2ff Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2023 License: Apache-2.0 Imports: 18 Imported by: 6

Documentation

Overview

Package rest is port adapter via http/s protocol # This manifest was generated by ymir. DO NOT EDIT.

Package rest is port adapter via http/s protocol # This manifest was generated by ymir. DO NOT EDIT.

Package rest is port adapter via http/s protocol # This manifest was generated by ymir. DO NOT EDIT.

Package rest is port adapter via http/s protocol # This manifest was generated by ymir. DO NOT EDIT.

Package rest is port adapter via http/s protocol # This manifest was generated by ymir. DO NOT EDIT.

Package rest is port adapter via http/s protocol # This manifest was generated by ymir. DO NOT EDIT.

Package rest is port adapter via http/s protocol # This manifest was generated by ymir. DO NOT EDIT.

Package rest is port adapter via http/s protocol # This manifest was generated by ymir. DO NOT EDIT.

Package rest is port adapter via http/s protocol # This manifest was generated by ymir. DO NOT EDIT.

Package rest is port adapter via http/s protocol # This manifest was generated by ymir. DO NOT EDIT.

Package rest is port adapter via http/s protocol # This manifest was generated by ymir. DO NOT EDIT.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrServerNotStarted is define error when server not started.
	ErrServerNotStarted = errors.New("server not started")
	// ErrServerAlreadyStarted is define error when server already started.
	ErrServerAlreadyStarted = errors.New("server already started")
	// ErrServerHandlerNotProvided is define error when server handler not provided.
	ErrServerHandlerNotProvided = errors.New("server handler not provided")
)

Functions

func DecodeForm

func DecodeForm(r *http.Request, v any) error

DecodeForm decodes a given reader into an interface using the form decoderBody.

func DecodeJSON

func DecodeJSON(r io.Reader, v any) error

DecodeJSON decodes a given reader into an interface using the json decoderBody.

func DecodeXML

func DecodeXML(r io.Reader, v any) error

DecodeXML decodes a given reader into an interface using the xml decoderBody.

func ErrBadGateway

func ErrBadGateway(w http.ResponseWriter, r *http.Request, err error) error

ErrBadGateway error http StatusBadGateway.

func ErrBadRequest

func ErrBadRequest(w http.ResponseWriter, r *http.Request, err error) error

ErrBadRequest error http StatusBadRequest.

func ErrForbidden

func ErrForbidden(w http.ResponseWriter, r *http.Request, err error) error

ErrForbidden error http StatusForbidden.

func ErrGatewayTimeout

func ErrGatewayTimeout(w http.ResponseWriter, r *http.Request, err error) error

ErrGatewayTimeout error http StatusGatewayTimeout.

func ErrInternalServerError

func ErrInternalServerError(w http.ResponseWriter, r *http.Request, err error) error

ErrInternalServerError error http StatusInternalServerError.

func ErrMethodNotAllowed

func ErrMethodNotAllowed(w http.ResponseWriter, r *http.Request, err error) error

ErrMethodNotAllowed error http StatusMethodNotAllowed.

func ErrNotAcceptable

func ErrNotAcceptable(w http.ResponseWriter, r *http.Request, err error) error

ErrNotAcceptable error http StatusNotAcceptable.

func ErrNotFound

func ErrNotFound(w http.ResponseWriter, r *http.Request, err error) error

ErrNotFound error http StatusNotFound.

func ErrPaymentRequired

func ErrPaymentRequired(w http.ResponseWriter, r *http.Request, err error) error

ErrPaymentRequired error http StatusPaymentRequired.

func ErrProxyAuthRequired

func ErrProxyAuthRequired(w http.ResponseWriter, r *http.Request, err error) error

ErrProxyAuthRequired error http StatusProxyAuthRequired.

func ErrRequestTimeout

func ErrRequestTimeout(w http.ResponseWriter, r *http.Request, err error) error

ErrRequestTimeout error http StatusRequestTimeout.

func ErrServiceUnavailable

func ErrServiceUnavailable(w http.ResponseWriter, r *http.Request, err error) error

ErrServiceUnavailable error http StatusServiceUnavailable.

func ErrStatusConflict

func ErrStatusConflict(w http.ResponseWriter, r *http.Request, err error) error

ErrStatusConflict error http StatusConflict.

func ErrUnauthorized

func ErrUnauthorized(w http.ResponseWriter, r *http.Request, err error) error

ErrUnauthorized error http StatusUnauthorized.

func ErrUnprocessableEntity

func ErrUnprocessableEntity(w http.ResponseWriter, r *http.Request, err error) error

ErrUnprocessableEntity error http StatusUnprocessableEntity.

func ErrUnsupportedMediaType

func ErrUnsupportedMediaType(w http.ResponseWriter, r *http.Request, err error) error

ErrUnsupportedMediaType error http StatusUnsupportedMediaType.

func GetBind

func GetBind[T any](r *http.Request) (T, error)

GetBind send a Pagination data.

func NotFoundDefault

func NotFoundDefault() http.HandlerFunc

NotFoundDefault sets a custom http.HandlerFunc for routing paths that could not be found. The default 404 handler is `http.NotFound`.

func Paging

func Paging(r *http.Request, p Pagination)

Paging send a Pagination data.

func SemanticVersion

func SemanticVersion(label string, version string) func(next http.Handler) http.Handler

SemanticVersion is middleware handler to semantic versioning.

func StatusAccepted

func StatusAccepted(r *http.Request)

StatusAccepted error http StatusAccepted.

func StatusCreated

func StatusCreated(r *http.Request)

StatusCreated error http StatusCreated.

func StatusMovedPermanently

func StatusMovedPermanently(r *http.Request)

StatusMovedPermanently error http StatusMovedPermanently.

Types

type Adapter

type Adapter[Request RequestConstraint, Response ResponseConstraint] func(w http.ResponseWriter, r *http.Request) (Response, error)

Adapter is wrapper func type for error handler.

type Binder

type Binder[T any] struct {
	// contains filtered or unexported fields
}

func Bind

func Bind[T any](r *http.Request, v *T) (*Binder[T], error)

Bind implements all bind request raw data. Binding is done in following order: 1) path params; 2) query params; 3) request body. Each step COULD override previous step bind values.

func (*Binder[T]) Validate

func (b *Binder[T]) Validate() error

Validate implements value validations for structs and individual fields based on tags.

type ContentType

type ContentType int

ContentType - Custom type to hold value for find and replace on content type.

const (
	UnknownMedia ContentType = iota

	MIMEApplicationJSON
	MIMEApplicationJSONCharsetUTF8
	MIMEApplicationJavaScript
	MIMEApplicationJavaScriptCharsetUTF8
	MIMEApplicationXML
	MIMEApplicationXMLCharsetUTF8
	MIMETextXML
	MIMETextXMLCharsetUTF8
	MIMEApplicationForm
	MIMEApplicationProtobuf
	MIMEApplicationMsgpack
	MIMETextCSV
	MIMETextCSVCharsetUTF8
	MIMETextHTML
	MIMETextHTMLCharsetUTF8
	MIMETextPlain
	MIMETextPlainCharsetUTF8
	MIMETextJavaScript
	MIMETextJavaScriptCharsetUTF8
	MIMEMultipartForm
	MIMEOctetStream
	MIMEImageJPEG
	MIMEImagePNG
)

Declare related constants for each ContentType starting with index 1.

func GetRequestContentType

func GetRequestContentType(r *http.Request) ContentType

GetRequestContentType is a helper function that returns ContentType based on context or request headers.

func (ContentType) Index

func (m ContentType) Index() int

Index - Return index of the Constant.

func (ContentType) String

func (m ContentType) String() string

String - Creating common behavior - give the type a String function.

type DefaultClientHttp

type DefaultClientHttp struct {
	// BaseUrl is the base url to set on requests.
	BaseUrl *url.URL
	// Transport is the underlying HTTP transport.
	// If nil, http.DefaultTransport is used.
	Transport http.RoundTripper
}

DefaultClientHttp is the default Client and is used by Get, Head, and Post.

func (*DefaultClientHttp) RoundTrip

func (c *DefaultClientHttp) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes a single HTTP transaction, returning a Response for the provided Request.

type HeaderType

type HeaderType int

HeaderType - Custom type to hold value for find and replace on header type.

const (
	HeaderAuthorization HeaderType = iota
	HeaderContentDisposition
	HeaderContentEncoding
	HeaderContentLength
	HeaderContentType
	HeaderContentDesc
	HeaderCookie
	HeaderXCSRFToken
	HeaderAccessControlAllowOrigin
	HeaderXTraceId
	HeaderUberTraceId
	HeaderContentTypeOptions
)

func (HeaderType) Index

func (h HeaderType) Index() int

Index - Return index of the Constant.

func (HeaderType) String

func (h HeaderType) String() string

String - Creating common behavior - give the type a String function.

type Meta

type Meta struct {
	Code    string `json:"code,omitempty"`
	Message string `json:"error_message,omitempty"`
}

Meta holds the response definition for the Meta entity.

type Option

type Option = func(s *Server) error

Option is server type return func.

func WithHost

func WithHost(host string) Option

WithHost will assign to host field server.

func WithIdleTimeout

func WithIdleTimeout(seconds int) Option

WithIdleTimeout will assign to idle timeout field server.

func WithPort

func WithPort(port string) Option

WithPort will assign to port field server.

func WithReadTimeout

func WithReadTimeout(seconds int) Option

WithReadTimeout will assign to read field server.

func WithShutdownTimeout

func WithShutdownTimeout(seconds int) Option

WithShutdownTimeout will assign to shut down timeout field server.

func WithWriteTimeout

func WithWriteTimeout(seconds int) Option

WithWriteTimeout will assign to write field server.

type Pagination

type Pagination struct {
	Page  int `json:"page,omitempty"`
	Limit int `json:"per_page,omitempty"`
	Size  int `json:"page_count,omitempty"`
	Total int `json:"total_count,omitempty"`
}

Pagination holds the response definition for the Pagination entity.

type RequestConstraint

type RequestConstraint interface {
	any
}

RequestConstraint is custom constraint type adapter request.

type RequestNotFound

type RequestNotFound struct{}

RequestNotFound request data not found.

type RequestType

type RequestType int

RequestType - Custom type to hold value for find and replace on context value request type.

const CtxPayloadRequest RequestType = iota

CtxPayloadRequest Declare related constants for each RequestType starting with index 1.

type Response

type Response[W RequestConstraint, R ResponseConstraint] struct {
	Meta       `json:"meta"`
	Version    `json:"version"`
	Pagination `json:"pagination,omitempty"`
	Data       any `json:"data,omitempty"`
	// contains filtered or unexported fields
}

Response holds the response definition for the Response entity.

func HandlerAdapter

func HandlerAdapter[RequestType RequestConstraint, ResponseType ResponseConstraint](a Adapter[RequestType, ResponseType]) *Response[RequestType, ResponseType]

HandlerAdapter is middleware handler to process error.

func (*Response[W, R]) CSV

func (e *Response[W, R]) CSV(w http.ResponseWriter, r *http.Request)

CSV sends a CSV format response with status code.

func (*Response[W, R]) JSON

func (e *Response[W, R]) JSON(w http.ResponseWriter, r *http.Request)

JSON sends a JSON response with status code.

func (*Response[W, R]) ServeHTTP

func (e *Response[W, R]) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ResponseCSV

type ResponseCSV struct {
	Filename string
	Rows     [][]string
}

ResponseCSV - Custom type to hold value from [][]string type to csv format response.

type ResponseConstraint

type ResponseConstraint interface {
	any
}

ResponseConstraint is custom constraint type adapter response.

type ResponseNotFound

type ResponseNotFound struct{}

ResponseNotFound response data not found.

type ResponseType

type ResponseType int

ResponseType - Custom type to hold value for find and replace on context value response type.

const (
	CtxPagination ResponseType = iota
	CtxVersion
	CtxStatusCode
)

Declare related constants for each ResponseType starting with index 1.

func (ResponseType) Index

func (r ResponseType) Index() int

Index - Return index of the Constant.

func (ResponseType) String

func (r ResponseType) String() string

type Server

type Server struct {
	Host string
	Port string
	// contains filtered or unexported fields
}

Server is the odin http server.

func NewServer

func NewServer(opts ...Option) *Server

NewServer creates a server.

func (*Server) Error

func (s *Server) Error() chan error

Error is return channel for capture error.

func (*Server) Handler

func (s *Server) Handler(h http.Handler)

Handler will assign http handler.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe will run the server.

func (*Server) Quite

func (s *Server) Quite(ctx context.Context) error

Quite will shutdown the server.

func (*Server) Stop

func (s *Server) Stop()

Stop will close the server.

type Version

type Version struct {
	Label  string `json:"label,omitempty"`
	Number string `json:"number,omitempty"`
}

Version holds the response definition for the Version entity.

Jump to

Keyboard shortcuts

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