contextx

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package contextx

@author: xwc1125

Package contextx

@author: xwc1125

Package contextx

@author: xwc1125

Index

Constants

View Source
const (
	BindType_Json          = "Json"
	BindType_Xml           = "Xml"
	BindType_Form          = "Form"
	BindType_Query         = "Query"
	BindType_FormPost      = "FormPost"
	BindType_FormMultipart = "FormMultipart"
	BindType_ProtoBuf      = "ProtoBuf"
	BindType_MsgPack       = "MsgPack"
	BindType_Yaml          = "Yaml"
	BindType_Header        = "Header"
	BindType_Params        = "Params"
)

BindType

View Source
const (
	ContentTypeHeaderKey        = "Content-Type"
	ContentDispositionHeaderKey = "Content-Disposition"
	ContentLengthHeaderKey      = "Content-Length"
	ContentEncodingHeaderKey    = "Content-Encoding"
)

Variables

View Source
var (
	RequestIDKey = "X-Request-Id"
	// MaxInMemoryMultipartSize 32 MB in memory max
	MaxInMemoryMultipartSize = int64(32000000)
)

Functions

func Data

func Data(ctx Context, code int, data []byte)

func FilterUrlParam

func FilterUrlParam(params map[string]string) map[string]interface{}

func GetRequestID

func GetRequestID(request *http.Request) string

func HTML

func HTML(ctx Context, code int, html string)

func JSON

func JSON(ctx Context, code int, obj interface{})

func JSONP

func JSONP(ctx Context, code int, callback string, obj interface{})

func PostForm

func PostForm(req *http.Request, key string) string

func ReadBody

func ReadBody(req *http.Request) *string

ReadBody 获取请求内容

func ReadHeaders

func ReadHeaders(req *http.Request) map[string]string

ReadHeaders 获取头部

func ReadHeadersFromResponse

func ReadHeadersFromResponse(header http.Header) map[string]string

ReadHeadersFromResponse 从响应中读取头部

func ReadMultiPostForm

func ReadMultiPostForm(mpForm *multipart.Form) map[string]string

func ReadPostForm

func ReadPostForm(req *http.Request) map[string]string

func ReadQueryParams

func ReadQueryParams(req *http.Request) map[string]string

func SendFile

func SendFile(ctx Context, filename string, destinationName string) error

func ServeContent

func ServeContent(ctx Context, content io.ReadSeeker, filename string, modtime time.Time, gzipCompression bool) error

func ServeFile

func ServeFile(ctx Context, filename string, gzipCompression bool) error

func Text

func Text(ctx Context, code int, format string, values ...interface{})

func UrlParamMap

func UrlParamMap(ctx Context) map[string]string

func UrlParamMapWithoutPage

func UrlParamMapWithoutPage(ctx Context) map[string]interface{}

Types

type Context

type Context interface {
	context.Context

	Request() *http.Request
	ResponseWriter() http.ResponseWriter
	RequestId() string

	Next()
	Abort() // 终止。如果授权失败(例如:密码不匹配),请调用Abort以确保不调用此请求的其余处理程序
	AbortWithStatus(code int)
	AbortWithStatusJSON(code int, jsonObj interface{})
	IsAborted() bool // 判断当前context是否已终止

	Param(key string) string
	Query(key string) string
	PostForm(key string) string
	FormFile(name string) (*multipart.FileHeader, error)
	MultipartForm() (*multipart.Form, error)
	SaveUploadedFile(file *multipart.FileHeader, dst string) error
	Bind(obj interface{}, bindType ...string) error
	ContentType() string

	Set(key string, value interface{})
	Get(key string) (value interface{}, exists bool)
	GetString(key string) (s string)
	GetBool(key string) (b bool)
	GetInt64(key string) (i64 int64)

	Status(code int)
	GetStatus() int
	Header(key, value string)
	GetHeader(key string) string
	SetCookie(cookie *http.Cookie)
	GetCookie(name string) (string, error)
	Redirect(code int, location string)
}

type Handler

type Handler func(Context)

type Reader

type Reader struct {
	ContentType   string
	ContentLength int64
	Reader        io.Reader
	Headers       map[string]string
}

Reader contains the IO reader and its length, and custom ContentType and other headers.

func (Reader) Render

func (r Reader) Render(w http.ResponseWriter) (err error)

Render (Reader) writes data with custom ContentType and headers.

func (Reader) WriteContentType

func (r Reader) WriteContentType(w http.ResponseWriter)

WriteContentType (Reader) writes custom ContentType.

type Render

type Render interface {
	// Render writes data with custom ContentType.
	Render(http.ResponseWriter) error
	// WriteContentType writes custom ContentType.
	WriteContentType(w http.ResponseWriter)
}

Render interface is to be implemented by JSON, XML, HTML, YAML and so on.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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