httptransport

package
v1.6.4 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: Apache-2.0 Imports: 37 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsRequestOut

func AsRequestOut(ctx context.Context) context.Context

func ContextWithHttpRequest

func ContextWithHttpRequest(ctx context.Context, req *http.Request) context.Context

func ContextWithOperatorFactory

func ContextWithOperatorFactory(ctx context.Context, factory *kit.OperatorFactory) context.Context

func ContextWithRouteMetaID

func ContextWithRouteMetaID(ctx context.Context, id string) context.Context

func ContextWithServiceMeta

func ContextWithServiceMeta(ctx context.Context, meta ServiceMeta) context.Context

func EnableQueryInBodyForGet

func EnableQueryInBodyForGet(ctx context.Context) context.Context

func HttpRequestFromContext

func HttpRequestFromContext(ctx context.Context) *http.Request

func IsRequestOut

func IsRequestOut(ctx context.Context) bool

func OperationIDFromContext

func OperationIDFromContext(ctx context.Context) string

func OperatorFactoryFromContext

func OperatorFactoryFromContext(ctx context.Context) *kit.OperatorFactory

func ShouldQueryInBodyForGet

func ShouldQueryInBodyForGet(ctx context.Context) bool

Types

type BadRequestError

type BadRequestError interface {
	EnableErrTalk()
	SetMsg(msg string)
	AddErr(err error, nameOrIdx ...interface{})
}

type BasePathDescriber

type BasePathDescriber interface {
	BasePath() string
}

type HttpMiddleware

type HttpMiddleware func(http.Handler) http.Handler

func MiddlewareChain

func MiddlewareChain(mw ...HttpMiddleware) HttpMiddleware

type HttpRouteMeta

type HttpRouteMeta struct {
	Route *kit.Route
	Metas []*OperatorFactoryWithRouteMeta
}
Example
os.Setenv(consts.EnvProjectName, "service-example")
os.Setenv(consts.EnvProjectVersion, "1.0.0")

color.NoColor = true

routes := routes.RootRouter.Routes()

sort.Slice(routes, func(i, j int) bool {
	return NewHttpRouteMeta(routes[i]).Key() <
		NewHttpRouteMeta(routes[j]).Key()
})

for i := range routes {
	rm := NewHttpRouteMeta(routes[i])
	fmt.Println(rm.String())
}
Output:

GET /demo openapi.OpenAPI
GET /demo/binary/files routes.DownloadFile
GET /demo/binary/images routes.ShowImage
POS /demo/binary/protobuf routes.Protobuf
POS /demo/cookie routes.Cookie
POS /demo/forms/multipart routes.FormMultipartWithFile
POS /demo/forms/multipart-with-files routes.FormMultipartWithFiles
POS /demo/forms/urlencoded routes.FormURLEncoded
GET /demo/proxy routes.Proxy
GET /demo/redirect routes.Redirect
POS /demo/redirect routes.RedirectWhenError
POS /demo/restful routes.Create
HEA /demo/restful routes.HealthCheck
GET /demo/restful/{id} routes.DataProvider routes.GetByID
DEL /demo/restful/{id} routes.DataProvider routes.RemoveByID
PUT /demo/restful/{id} routes.DataProvider routes.UpdateByID
GET /demo/v2/proxy routes.ProxyV2

func NewHttpRouteMeta

func NewHttpRouteMeta(route *kit.Route) *HttpRouteMeta

func (*HttpRouteMeta) Key

func (hr *HttpRouteMeta) Key() string

func (*HttpRouteMeta) Log

func (hr *HttpRouteMeta) Log()

func (*HttpRouteMeta) Method

func (hr *HttpRouteMeta) Method() string

func (*HttpRouteMeta) OperatorNames

func (hr *HttpRouteMeta) OperatorNames() string

func (*HttpRouteMeta) Path

func (hr *HttpRouteMeta) Path() string

func (*HttpRouteMeta) String

func (hr *HttpRouteMeta) String() string

type HttpTransport

type HttpTransport struct {
	ServiceMeta
	Protocol    string
	Addr        string
	Port        int
	Modifiers   []ServerModifier    // for modifying http.Server
	Middlewares []HttpMiddleware    // Middlewares https://github.com/gorilla/handlers
	Vldt        validator.Factory   // Vldt validator factory
	Tsfm        transformer.Factory // transformer mgr for parameter transforming
	CertFile    string
	KeyFile     string
	// contains filtered or unexported fields
}

func NewHttpTransport

func NewHttpTransport(modifiers ...ServerModifier) *HttpTransport

func (*HttpTransport) IsTLS added in v1.6.1

func (t *HttpTransport) IsTLS() bool

func (*HttpTransport) Serve

func (t *HttpTransport) Serve(router *kit.Router) error

func (*HttpTransport) ServeContext

func (t *HttpTransport) ServeContext(ctx context.Context, router *kit.Router) error

func (*HttpTransport) ServeHTTP

func (t *HttpTransport) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*HttpTransport) SetDefault

func (t *HttpTransport) SetDefault()

func (*HttpTransport) Shutdown added in v1.2.2

func (t *HttpTransport) Shutdown(ctx context.Context) error

type MetaOperator

type MetaOperator struct {
	kit.EmptyOperator
	// contains filtered or unexported fields
}

func BasePath

func BasePath(basePath string) *MetaOperator

func Group

func Group(path string) *MetaOperator
Example
g := Group("/test")
fmt.Println(g.Path())
Output:

/test

func (*MetaOperator) BasePath

func (g *MetaOperator) BasePath() string

func (*MetaOperator) Path

func (g *MetaOperator) Path() string

type MethodDescriber

type MethodDescriber interface {
	Method() string
}

type OperatorFactoryWithRouteMeta

type OperatorFactoryWithRouteMeta struct {
	*kit.OperatorFactory
	RouteMeta
}

func NewOperatorFactoryWithRouteMeta

func NewOperatorFactoryWithRouteMeta(op kit.Operator, last bool) *OperatorFactoryWithRouteMeta

type PathDescriber

type PathDescriber interface {
	Path() string
}

type PostValidator

type PostValidator interface {
	PostValidate(badReqErr BadRequestError)
}

type RequestTsfm

type RequestTsfm struct {
	Type   reflect.Type
	Params map[string][]transformer.ReqParam
}

func (*RequestTsfm) DecodeAndValidate

func (t *RequestTsfm) DecodeAndValidate(ctx context.Context, ri httpx.RequestInfo, v interface{}) error

func (*RequestTsfm) DecodeFromRequestInfo

func (t *RequestTsfm) DecodeFromRequestInfo(ctx context.Context, ri httpx.RequestInfo, v interface{}) error

func (*RequestTsfm) NewReqWithContext

func (t *RequestTsfm) NewReqWithContext(ctx context.Context, method, url string, v interface{}) (*http.Request, error)

func (*RequestTsfm) NewRequest

func (t *RequestTsfm) NewRequest(method, url string, v interface{}) (*http.Request, error)

type RequestTsfmFactory

type RequestTsfmFactory struct {
	Vldt validator.Factory
	Tsfm transformer.Factory
	// contains filtered or unexported fields
}

func NewRequestTsfmFactory

func NewRequestTsfmFactory(tsfm transformer.Factory, vldt validator.Factory) *RequestTsfmFactory
Example
factory := NewRequestTsfmFactory(nil, nil)

type PlainBody struct {
	A   string `json:"a"                         validate:"@string[2,]"`
	Int int    `json:"int,omitempty" default:"0" validate:"@int[0,]"`
}

type Req struct {
	Protocol  types.Protocol `in:"query" name:"protocol,omitempty" default:"HTTP"`
	QString   string         `in:"query" name:"string,omitempty"   default:"s"`
	PlainBody PlainBody      `in:"body"  mime:"plain" validate:"@struct<json>"`
}

req := &Req{}
req.PlainBody.A = "1"

rt, err := factory.NewRequestTsfm(bgctx, reflect.TypeOf(req))
if err != nil {
	panic(err)
}

statusErr := rt.Params["body"][0].Validator.Validate(req.PlainBody)

statusErr.(*vldterr.ErrorSet).Each(func(fieldErr *vldterr.FieldError) {
	fmt.Println(fieldErr.Field, strconv.Quote(fieldErr.Error.Error()))
})
Output:

a "string length should be larger than 2, but got invalid value 1"

func (*RequestTsfmFactory) NewRequest

func (f *RequestTsfmFactory) NewRequest(method, url string, v interface{}) (*http.Request, error)

func (*RequestTsfmFactory) NewRequestTsfm

func (f *RequestTsfmFactory) NewRequestTsfm(ctx context.Context, typ reflect.Type) (*RequestTsfm, error)

func (*RequestTsfmFactory) NewRequestWithContext

func (f *RequestTsfmFactory) NewRequestWithContext(ctx context.Context, method, url string, v interface{}) (*http.Request, error)

func (*RequestTsfmFactory) SetDefault

func (f *RequestTsfmFactory) SetDefault()

type ResponseWithError

type ResponseWithError interface {
	WriteError(err error)
}

type RouteHandler

type RouteHandler struct {
	*RequestTsfmFactory
	*HttpRouteMeta
	// contains filtered or unexported fields
}

func NewRouteHandler

func NewRouteHandler(sm *ServiceMeta, meta *HttpRouteMeta, rtf *RequestTsfmFactory) *RouteHandler

func (*RouteHandler) ServeHTTP

func (hdl *RouteHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request)

ServeHTTP impls http.Handler

type RouteMeta

type RouteMeta struct {
	ID         string // ID operator name
	Method     string // Method http method implement MethodDescriber
	Path       string // Path in tag `path`
	BasePath   string // BasePath base path
	Summary    string // Summary operator's desc
	Deprecated bool
}

type ServerModifier

type ServerModifier func(server *http.Server) error

type ServiceMeta

type ServiceMeta struct {
	Name    string
	Version string
}

func ServiceMetaFromContext

func ServiceMetaFromContext(ctx context.Context) ServiceMeta

func (*ServiceMeta) SetDefault

func (s *ServiceMeta) SetDefault()

func (ServiceMeta) String

func (s ServiceMeta) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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