apis

package
v1.0.23 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2020 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GET tMethod = iota
	POST
	HEAD
	PUT
	PATCH
	DELETE
	CONNECT
	OPTIONS
	TRACE
)

const of tMethod type values

View Source
const (
	JSONParams      = "JSONparams"
	MultiPartParams = "MultiPartParams"
	ChildRoutePath  = "lastSegment"
)

const of values ctx parameters names

Variables

View Source
var (
	ErrUnAuthorized   = errors.New("user is UnAuthorized")
	ErrRouteForbidden = errors.New("not allow permission")

	ErrPathAlreadyExists = errors.New("this path already exists")
	ErrWrongParamsList   = errors.New("wrong params list: %+v")
)

errors declaration

View Source
var HEADERS = map[string]string{
	"Content-Type":     "text/html; charset=utf-8",
	"author":           "ruslanBik4",
	"Server":           "HTTPGO/0.9 (CentOS) Go 1.129",
	"Content-Language": "en, ru",
}

HEADERS - list standart header for html page - noinspection GoInvalidConstType

Functions

func AddFieldToJSON

func AddFieldToJSON(stream *jsoniter.Stream, field string, s string)

func AddObjectToJSON

func AddObjectToJSON(stream *jsoniter.Stream, field string, s interface{})

func FirstFieldToJSON

func FirstFieldToJSON(stream *jsoniter.Stream, field string, s string)

func WriteHeaders

func WriteHeaders(ctx *fasthttp.RequestCtx)

WriteHeaders выдаем стандартные заголовки страницы

func WriteJSON

func WriteJSON(ctx *fasthttp.RequestCtx, r interface{}) (err error)

WriteJSON write JSON to response

func WriteJSONHeaders

func WriteJSONHeaders(ctx *fasthttp.RequestCtx)

WriteJSONHeaders return standart headers for JSON

Types

type APIRouteParamsType

type APIRouteParamsType interface {
	fmt.Stringer
	CheckType(ctx *fasthttp.RequestCtx, value string) bool
	ConvertValue(ctx *fasthttp.RequestCtx, value string) (interface{}, error)
	ConvertSlice(ctx *fasthttp.RequestCtx, values []string) (interface{}, error)
	IsSlice() bool
}

APIRouteParamsType encapsulates types operation for apis parameters

type ApiRoute

type ApiRoute struct {
	Desc                                      string          `json:"descriptor"`
	DTO                                       RouteDTO        `json:"dto"`
	Fnc                                       ApiRouteHandler `json:"-"`
	FncAuth                                   FncAuth         `json:"-"`
	TestFncAuth                               FncAuth         `json:"-"`
	Method                                    tMethod         `json:"method,string"`
	Multipart, NeedAuth, OnlyAdmin, OnlyLocal bool
	Params                                    []InParam   `json:"parameters,omitempty"`
	Resp                                      interface{} `json:"response,omitempty"`
	// contains filtered or unexported fields
}

ApiRoute implement endpoint info & handler on request

func NewAPIRoute

func NewAPIRoute(desc string, method tMethod, params []InParam, needAuth bool, fnc ApiRouteHandler,
	resp interface{}, Options ...BuildRouteOptions) *ApiRoute

NewAPIRoute create customizing ApiRoute

func (*ApiRoute) CheckAndRun

func (route *ApiRoute) CheckAndRun(ctx *fasthttp.RequestCtx, fncAuth FncAuth) (resp interface{}, err error)

CheckAndRun check & run route handler

func (*ApiRoute) CheckParams

func (route *ApiRoute) CheckParams(ctx *fasthttp.RequestCtx) (badParams []string)

CheckParams check param of request

type ApiRouteFuncAuth

type ApiRouteFuncAuth func(ctx *fasthttp.RequestCtx) error

type ApiRouteHandler

type ApiRouteHandler func(ctx *fasthttp.RequestCtx) (interface{}, error)

type ApiRoutes

type ApiRoutes map[string]*ApiRoute

ApiRoutes is hair of APIROute

func NewAPIRoutes

func NewAPIRoutes() ApiRoutes

NewAPIRoutes create APIRotes instance

func (ApiRoutes) AddRoutes

func (r ApiRoutes) AddRoutes(routes ApiRoutes) (badRouting []string)

AddRoutes add ApiRoute into hair onsafe

type Apis

type Apis struct {
	Ctx CtxApis
	// contains filtered or unexported fields
}

Apis encapsulates REST API configuration and endpoints in calling it checks for the presence, validation of parameters and access privileges

func NewApis

func NewApis(ctx CtxApis, routes ApiRoutes, fncAuth FncAuth) *Apis

NewApis create new Apis from list of routes, environment values configuration & authentication method

func (*Apis) AddRoutes

func (a *Apis) AddRoutes(routes ApiRoutes) (badRouting []string)

AddRoutes add routes to Apis for handling service return slice with name routes which not adding

func (*Apis) Handler

func (a *Apis) Handler(ctx *fasthttp.RequestCtx)

Handler find route on request, check & run

type BuildRouteOptions

type BuildRouteOptions func(route *ApiRoute)

BuildRouteOptions implement 'Functional Option' pattern for ApiRoute settings

func DTO

func DTO(dto RouteDTO) BuildRouteOptions

DTO set custom struct on response params

func MultiPartForm

func MultiPartForm() BuildRouteOptions

MultiPartForm set flag of multipart checking

func OnlyLocal

func OnlyLocal() BuildRouteOptions

OnlyLocal set flag of only local response routing

func RouteAuth

func RouteAuth(fncAuth FncAuth) BuildRouteOptions

RouteAuth set custom auth method on ApiRoute

type CtxApis

type CtxApis map[string]interface{}

func NewCtxApis

func NewCtxApis(cap int) CtxApis

func (CtxApis) AddValue

func (c CtxApis) AddValue(key string, val interface{})

func (CtxApis) Deadline

func (c CtxApis) Deadline() (deadline time.Time, ok bool)

func (CtxApis) Done

func (c CtxApis) Done() <-chan struct{}

func (CtxApis) Err

func (c CtxApis) Err() error

func (CtxApis) Value

func (c CtxApis) Value(key interface{}) interface{}

type DefValueCalcFnc

type DefValueCalcFnc = func(ctx *fasthttp.RequestCtx) interface{}

type FncAuth

type FncAuth interface {
	Auth(ctx *fasthttp.RequestCtx) bool
	AdminAuth(ctx *fasthttp.RequestCtx) bool
	String() string
}

type InParam

type InParam struct {
	Name              string
	Desc              string
	Req               bool
	PartReq           []string
	Type              APIRouteParamsType
	DefValue          interface{}
	IncompatibleWiths []string
	TestValue         string
}

InParam implement params on request

type RouteDTO

type RouteDTO interface {
	GetValue() interface{}
	NewValue() interface{}
}

RouteDTO must to help create some types into routing handling

type TypeInParam

type TypeInParam struct {
	types.BasicKind
	// contains filtered or unexported fields
}

TypeInParam has type definition of params ApiRoute

func NewSliceTypeInParam

func NewSliceTypeInParam(bk types.BasicKind) TypeInParam

NewTypeInParam create TypeInParam

func NewTypeInParam

func NewTypeInParam(bk types.BasicKind) TypeInParam

NewTypeInParam create TypeInParam

func (TypeInParam) CheckType

func (t TypeInParam) CheckType(ctx *fasthttp.RequestCtx, value string) bool

CheckType check of value computable with the TypeInParam

func (TypeInParam) ConvertSlice

func (t TypeInParam) ConvertSlice(ctx *fasthttp.RequestCtx, values []string) (interface{}, error)

func (TypeInParam) ConvertValue

func (t TypeInParam) ConvertValue(ctx *fasthttp.RequestCtx, value string) (interface{}, error)

CheckType check of value compatable with the TypeInParam

func (TypeInParam) IsSlice

func (t TypeInParam) IsSlice() bool

func (TypeInParam) String

func (t TypeInParam) String() string

Jump to

Keyboard shortcuts

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