httpsrv

package
v1.2.2 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// HeaderContentLength the header name of `Content-Length`
	HeaderContentLength = "Content-Length"
	// HeaderContentType the header name of `Content-Type`
	HeaderContentType = "Content-Type"
	// MIMEApplicationJSON the application type for json
	MIMEApplicationJSON = "application/json"
	// MIMEApplicationJSONCharsetUTF8 the application type for json of utf-8 encoding
	MIMEApplicationJSONCharsetUTF8 = "application/json; charset=UTF-8"
)

Variables

View Source
var (
	// ErrSuccess indicates api success
	ErrSuccess        = NewError(errnoSuccess, "成功")
	ErrServerInternal = NewError(errnoInternal, "服务器内部错误")
)

Functions

func EncodeJSON

func EncodeJSON(v interface{}) ([]byte, error)

EncodeJSON is a wrapper of json.Marshal()

func Error

func Error(ctx context.Context, w http.ResponseWriter, err interface{})

Error writes out an error response

func Logging added in v1.2.0

Logging implements the request in/out logging middleware

func OK

OK writes out a success response without data, used typically in an `update` api.

func OKData

func OKData(ctx context.Context, w http.ResponseWriter, data interface{})

OKData writes out a success response with data, used typically in an `get` api.

func Recovery added in v1.2.0

Recovery implements the recovery wrapper middleware

func Start added in v1.2.0

func Start(upgrader *tableflip.Upgrader, logger *zap.Logger)

Start start the http service

func Stop added in v1.2.0

func Stop()

Stop stop the http service

func WriteJSON

func WriteJSON(w http.ResponseWriter, v interface{}) error

WriteJSON writes out an object which is serialized as json.

Types

type BaseHandler

type BaseHandler struct{}

BaseHandler is the enhanced version of ngs.BaseController

func (*BaseHandler) EncodeJSON

func (h *BaseHandler) EncodeJSON(v interface{}) ([]byte, error)

EncodeJSON is a wrapper of json.Marshal()

func (*BaseHandler) Error

func (h *BaseHandler) Error(ctx context.Context, w http.ResponseWriter, err interface{})

Error writes out an error response

func (*BaseHandler) OK

OK writes out a success response without data, used typically in an `update` api.

func (*BaseHandler) OKData

func (h *BaseHandler) OKData(ctx context.Context, w http.ResponseWriter, data interface{})

OKData writes out a success response with data, used typically in an `get` api.

func (*BaseHandler) ParseRequest

func (h *BaseHandler) ParseRequest(ctx context.Context, r *kate.Request, req interface{}) error

ParseRequest parses and validates the api request nolint:lll,gocyclo

func (*BaseHandler) WriteJSON

func (h *BaseHandler) WriteJSON(w http.ResponseWriter, v interface{}) error

WriteJSON writes out an object which is serialized as json.

type ErrorInfo

type ErrorInfo interface {
	error
	Code() int
}

ErrorInfo defines the error type

func ErrBadParam added in v1.2.0

func ErrBadParam(v interface{}) ErrorInfo

ErrBadParam returns a instance of bad param ErrorInfo.

func NewError

func NewError(code int, message string) ErrorInfo

NewError create an errSimple instance

type ErrorInfoWithData

type ErrorInfoWithData interface {
	error
	Code() int
	Data() interface{}
}

ErrorInfoWithData defines the error type with extra data

func NewErrorWithData

func NewErrorWithData(code int, message string, data interface{}) ErrorInfoWithData

NewErrorWithData create a errWithData instance

type HelloHandler

type HelloHandler struct {
	BaseHandler
}

func (*HelloHandler) ServeHTTP

func (h *HelloHandler) ServeHTTP(ctx context.Context, w kate.ResponseWriter, r *kate.Request)

type Result

type Result struct {
	ErrNO  int         `json:"errno"`
	ErrMsg string      `json:"errmsg"`
	Data   interface{} `json:"data,omitempty"`
}

Result define the handle result for http request

Jump to

Keyboard shortcuts

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