httpendpoints

package
v1.0.6-alpha Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// ContentTypeJSON Content Type
	ContentTypeJSON = "application/json"
	// ResponseWriter context value key
	ResponseWriter = ContextKey("responseWriter")
	// Base64EncodedRequestBody .
	Base64EncodedRequestBody = "base64-encoded-request-body"
	// TraceID .
	TraceID = ContextKey("dice-trace-id")
)

Variables

This section is empty.

Functions

func WriteData

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

WriteData .

func WriteErr

func WriteErr(w http.ResponseWriter, code, errMsg string)

WriteErr .

func WriteJSON

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

WriteJSON .

func WriteYAML

func WriteYAML(w http.ResponseWriter, v string)

WriteYAML .

Types

type ContextKey

type ContextKey string

ContextKey ...

type Endpoint

type Endpoint struct {
	Path           string
	Method         string
	Handler        func(context.Context, *http.Request, map[string]string) (Responser, error)
	WriterHandler  func(context.Context, http.ResponseWriter, *http.Request, map[string]string) error
	ReverseHandler func(context.Context, *http.Request, map[string]string) error
}

Endpoint contains URL path and corresponding handler

type ErrorResponse

type ErrorResponse struct {
	Code string      `json:"code"`
	Msg  string      `json:"msg"`
	Ctx  interface{} `json:"ctx"`
}

ErrorResponse .

type HTTPResponse

type HTTPResponse struct {
	Error   ierror.IAPIError
	Status  int
	Content interface{}
}

HTTPResponse is a struct contains status code and content body

func (HTTPResponse) GetContent

func (r HTTPResponse) GetContent() interface{}

GetContent returns http content body

func (HTTPResponse) GetLocaledResp

func (r HTTPResponse) GetLocaledResp(locale i18n.LocaleResource) HTTPResponse

GetLocaledResp .

func (HTTPResponse) GetStatus

func (r HTTPResponse) GetStatus() int

GetStatus returns http status code.

type Interface

type Interface interface {
	RegisterEndpoints(endpoints []Endpoint)
	Router() *mux.Router
}

Interface .

type Resp

type Resp struct {
	Success bool          `json:"success"`
	Data    interface{}   `json:"data,omitempty"`
	Err     ErrorResponse `json:"err,omitempty"`
	UserIDs []string      `json:"userIDs,omitempty"`
}

Resp .

type Responser

type Responser interface {
	GetLocaledResp(locale i18n.LocaleResource) HTTPResponse
	GetStatus() int
	GetContent() interface{}
}

Responser is an interface for http response

func ErrResp

func ErrResp(statusCode int, code, errMsg string) (Responser, error)

ErrResp .

func OkResp

func OkResp(data interface{}, userIDs ...[]string) (Responser, error)

OkResp 采用httpserver框架时正常返回结果封装 在 `userIDs` 中设置需要由 openapi 注入的用户信息的 ID 列表

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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