xreq

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2021 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind(req *http.Request, data interface{}) error

func BindForm

func BindForm(req *http.Request, data interface{}) error

func BindJSON

func BindJSON(req *http.Request, data interface{}) error

func BindURI

func BindURI(req *http.Request, data interface{}) error

func Convert

func Convert(h Handler) func(req *http.Request) *Result

func ErrorRender

func ErrorRender(err error, w http.ResponseWriter, req *http.Request)

func JSONDeserializer

func JSONDeserializer(req *http.Request, data interface{}) error

func RawConvert

func RawConvert(h Handler) func(req *http.Request) *Result

func Render

func Render(w http.ResponseWriter, req *http.Request, res *Result)

func ValidateData

func ValidateData(data interface{}, lang ut.Translator) error

Types

type Endpoint

type Endpoint struct {
	Path    string
	Method  string
	Handler func(*http.Request) *Result

	RegisterHandler func(*mux.Router) *mux.Route

	Authorizer *iauth.Authorization
}

func (*Endpoint) Register

func (ep *Endpoint) Register(router *mux.Router) *mux.Router

func (*Endpoint) ServeHTTP

func (ep *Endpoint) ServeHTTP(rw http.ResponseWriter, req *http.Request)

func (*Endpoint) String

func (re *Endpoint) String() string

type Handler

type Handler func(req *http.Request) (interface{}, error)

type RequestInfo

type RequestInfo struct {
	StartTime time.Time
	Duration  time.Duration

	URLPath  string
	Method   string
	ClientIP string
	LogID    string

	URLPattern string

	StatusCode int
	RetMsg     string
	ErrDetail  string
}

func GetRequestInfo

func GetRequestInfo(ctx context.Context) *RequestInfo

func InitRequestInfo

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

func (*RequestInfo) String

func (requestInfo *RequestInfo) String() string

type Result

type Result struct {
	OriginErr error `json:"-"`

	Code   int         `json:"ErrNum"`         // http status code
	ErrMsg string      `json:"ErrMsg"`         // return message if failed
	Data   interface{} `json:"Data,omitempty"` // return data if success

	Render func(w http.ResponseWriter, req *http.Request, res *Result) `json:"-"`
}

Result used for result process

func (*Result) Error

func (res *Result) Error() string

Error error interface

func (*Result) IsSucc

func (res *Result) IsSucc() bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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