apis

package
v1.2.11 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2022 License: GPL-3.0 Imports: 33 Imported by: 5

Documentation

Index

Constants

View Source
const (
	ContentTypeJSON      = "application/json"
	ContentTypeMultiPart = "multipart/form-data"
)

content types

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

const of tMethod type values

View Source
const (
	JSONParams      = "JSONparams"
	MultiPartParams = "MultiPartParams"
	ChildRoutePath  = "lastSegment"
	ApiVersion      = "ACC_VERSION"
	AuthManager     = "auth"
	Database        = "DB"
)

const of values ctx parameters names

View Source
const PARAM_REQUIRED = "is required parameter"

Variables

View Source
var (
	ParamsHTML = InParam{
		Name: "html",
		Desc: "need for get result in html instead JSON",
		Req:  false,
		Type: NewTypeInParam(types.Bool),
	}
	ParamsLang = InParam{
		Name:     "lang",
		Desc:     "need to get result on non-english",
		DefValue: "en",
		Req:      true,
		Type:     NewTypeInParam(types.String),
	}
	ParamsGetFormActions = InParam{
		Name: "is_get_form_actions",
		Desc: "need to get form actions in response",
		Req:  false,
		Type: NewTypeInParam(types.Bool),
	}
	BasicParams = []InParam{
		ParamsHTML,
		ParamsLang,
	}
)
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

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 FirstObjectToJSON added in v1.0.11512

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

func NewSwaggerContent added in v1.1.10120

func NewSwaggerContent(schema map[string]interface{}) map[string]interface{}

func WriteElemValue added in v1.0.11512

func WriteElemValue(ctx *fasthttp.RequestCtx, src []byte, col dbEngine.Column)

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 standard headers for JSON

func WriteMap added in v1.1.10120

func WriteMap(value reflect.Value, stream *jsoniter.Stream, title string) interface{}

func WriteRecordAsJSON added in v1.1.10119

func WriteRecordAsJSON(ctx *fasthttp.RequestCtx, rowComma *string) func(values [][]byte, columns []dbEngine.Column) error

func WriteReflectKind added in v1.1.10120

func WriteReflectKind(kind reflect.Kind, value reflect.Value, stream *jsoniter.Stream, sType, title string) interface{}

func WriteSlice added in v1.1.10120

func WriteSlice(value reflect.Value, stream *jsoniter.Stream, title string) interface{}

func WriteStruct added in v1.1.10120

func WriteStruct(value reflect.Value, stream *jsoniter.Stream, title string) interface{}

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:"-"`
	FncIsForbidden                                      func(ctx *fasthttp.RequestCtx) bool `json:"-"`
	TestFncAuth                                         FncAuth                             `json:"-"`
	Method                                              tMethod                             `json:"method,string"`
	Multipart, NeedAuth, OnlyAdmin, OnlyLocal, WithCors bool
	Params                                              []InParam   `json:"parameters,omitempty"`
	Resp                                                interface{} `json:"response,omitempty"`
}

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 NewAPIRouteWithDBEngine added in v1.0.11512

func NewAPIRouteWithDBEngine(desc string, method tMethod, needAuth bool, params []InParam,
	sqlOrName string, Options ...BuildRouteOptions) *ApiRoute

NewAPIRouteWithDBEngine create customizing ApiRoute

func NewSimpleGETRoute added in v1.0.11512

func NewSimpleGETRoute(desc string, params []InParam, fnc ApiSimpleHandler,
	Options ...BuildRouteOptions) *ApiRoute

NewSimpleGETRoute create GET ApiRoute with minimal requirements

func NewSimplePOSTRoute added in v1.0.11512

func NewSimplePOSTRoute(desc string, params []InParam, fnc ApiSimpleHandler,
	Options ...BuildRouteOptions) *ApiRoute

NewSimplePOSTRoute create POST ApiRoute with minimal requirements

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 map[string]string) bool

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

type ApiSimpleHandler added in v1.0.11512

type ApiSimpleHandler func() (interface{}, error)

type Apis

type Apis struct {
	*sync.RWMutex
	Ctx CtxApis

	Https bool
	// 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 MapRoutes, 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) DefaultRoutes added in v1.2.1

func (apis Apis) DefaultRoutes() ApiRoutes

func (*Apis) Handler

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

Handler find route on request, check & run

type ApisValues added in v1.0.11512

type ApisValues string

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

func RouteNeedAuth added in v1.0.11512

func RouteNeedAuth() BuildRouteOptions

RouteNeedAuth set auth on ApiRoute

func RouteOnlyAdmin added in v1.0.11512

func RouteOnlyAdmin() BuildRouteOptions

RouteOnlyAdmin set admin access for ApiRoute

type CheckDTO added in v1.0.11512

type CheckDTO interface {
	CheckParams(ctx *fasthttp.RequestCtx, badParams map[string]string) bool
}

type CompoundDTO added in v1.0.11512

type CompoundDTO interface {
	ReadParams(ctx *fasthttp.RequestCtx)
}

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 ErrorResp added in v1.0.11512

type ErrorResp struct {
	FormErrors map[string]string `json:"formErrors"`
}

func NewErrorResp added in v1.0.11512

func NewErrorResp(formErrors map[string]string) *ErrorResp

func NewErrorRespBadDTO added in v1.0.11512

func NewErrorRespBadDTO() *ErrorResp

type FncAuth added in v0.0.4

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

type FncVisit added in v1.0.11512

type FncVisit func([]byte, *fastjson.Value)

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

func (*InParam) Check added in v1.1.10120

func (param *InParam) Check(ctx *fasthttp.RequestCtx, badParams map[string]string)

Check params of ctx

func (*InParam) WithNotRequired added in v1.2.2

func (param *InParam) WithNotRequired() *InParam

type MapRoutes added in v1.0.310

type MapRoutes map[tMethod]map[string]*ApiRoute

func NewMapRoutes added in v1.0.310

func NewMapRoutes() MapRoutes

NewMapRoutes create APIRotes instance

func (MapRoutes) AddRoutes added in v1.0.310

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

AddRoutes add ApiRoute into hair onsafe

func (MapRoutes) GetRoute added in v1.0.310

func (r MapRoutes) GetRoute(ctx *fasthttp.RequestCtx) (*ApiRoute, error)

func (MapRoutes) GetTestRouteSuffix added in v1.0.310

func (r MapRoutes) GetTestRouteSuffix(route *ApiRoute) string

type ReflectType added in v1.1.10120

type ReflectType struct {
	reflect.Type

	Props interface{}
	// contains filtered or unexported fields
}

func NewReflectType added in v1.1.10120

func NewReflectType(value interface{}) *ReflectType

func (ReflectType) CheckType added in v1.1.10120

func (r ReflectType) CheckType(ctx *fasthttp.RequestCtx, value string) bool

func (ReflectType) ConvertSlice added in v1.1.10120

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

func (ReflectType) ConvertValue added in v1.1.10120

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

func (ReflectType) IsSlice added in v1.1.10120

func (r ReflectType) IsSlice() bool

func (*ReflectType) WriteMap added in v1.1.10120

func (r *ReflectType) WriteMap(value reflect.Value, title string) interface{}

func (*ReflectType) WriteReflectKind added in v1.1.10120

func (r *ReflectType) WriteReflectKind(kind reflect.Kind, value reflect.Value, sType, title string) interface{}

func (*ReflectType) WriteSlice added in v1.1.10120

func (r *ReflectType) WriteSlice(value reflect.Value, title string) interface{}

func (*ReflectType) WriteStruct added in v1.1.10120

func (r *ReflectType) WriteStruct(value reflect.Value, title string) interface{}

type RouteDTO

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

RouteDTO must help create some types into routing handling

type SwaggerParam added in v1.1.10120

type SwaggerParam map[string]interface{}

func NewSwaggerArray added in v1.1.10120

func NewSwaggerArray(desc string, props ...interface{}) SwaggerParam

func NewSwaggerArray1 added in v1.1.10120

func NewSwaggerArray1(props interface{}, name string) SwaggerParam

func NewSwaggerObject added in v1.1.10120

func NewSwaggerObject(props interface{}, name string) SwaggerParam

func NewSwaggerObjectRoot added in v1.1.10120

func NewSwaggerObjectRoot(props interface{}) SwaggerParam

func NewSwaggerParam added in v1.1.10120

func NewSwaggerParam(props interface{}, name, typ string) SwaggerParam

type SwaggerUnit added in v1.1.10120

type SwaggerUnit struct {
	Properties []spec.SchemaProps `json:"properties,omitempty"`
	Items      interface{}        `json:"items,omitempty"`
	Type       string
}

type TypeInParam

type TypeInParam struct {
	types.BasicKind

	DTO RouteDTO
	// contains filtered or unexported fields
}

TypeInParam has type definition of params ApiRoute

func NewSliceTypeInParam

func NewSliceTypeInParam(bk types.BasicKind) TypeInParam

NewTypeInParam create TypeInParam

func NewStructInParam added in v1.0.11512

func NewStructInParam(dto RouteDTO) 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)

ConvertValue convert value according to TypeInParam's type

func (TypeInParam) IsSlice

func (t TypeInParam) IsSlice() bool

func (TypeInParam) ReadValue added in v1.0.11512

func (t TypeInParam) ReadValue(s string, v interface{}) (interface{}, error)

func (TypeInParam) String

func (t TypeInParam) String() string

type Visit added in v1.0.11512

type Visit interface {
	Each([]byte, *fastjson.Value)
	Result() (interface{}, error)
}

todo add description

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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