rs

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2022 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentTypeJson = `application/json; charset=UTF-8`
)

Variables

This section is empty.

Functions

func NewCORS

func NewCORS(headers []string) func(ctx Context)

NewCORS 创建个支持跨域的中间件 参数为允许的额外请求头列表

func UseCORS

func UseCORS(ctx Context)

UseCORS 默认的允许跨域中间件,允许的额外请求头:Content-Type,Authorization。 允许更多额外的请求头,请使用NewCORS

Types

type Context

type Context interface {
	context.Context
	// Next 一般用于中间件,表示允许继续执行后续的处理函数
	Next()
	//SetContext 向下传值
	SetContext(key, val interface{})
	//RespRecordStart 开始记录响应内容,只会记录Write开头的写入
	RespRecordStart()
	//RespRecordReader 读取响应内容
	RespRecordReader() io.Reader
	// RespRecordReadAll 读取全部响应内容,会清空已写入的记录
	RespRecordReadAll() []byte

	// ResponseWriter 原生响应处理
	ResponseWriter() http.ResponseWriter
	// Request 原生请求
	Request() *http.Request
	// GetHeaderReq 获取请求头
	GetHeaderReq() *http.Header
	// SetHeaderResp 设置响应头
	SetHeaderResp() http.Header

	// SetStatusCode 设置响应码
	SetStatusCode(code uint)
	// SetContentType 设置响应头content-type
	SetContentType(contentType string)
	// SetContentTypeToJson 设置响应头content-type为json类型
	SetContentTypeToJson()

	// GetRemoteAddr 获取IP地址和端口号,获取真实IP地址,请使用GetFirstIP
	GetRemoteAddr() string
	// GetIP 获取IP地址,假如是反向代理过来的,请使用GetFirstIP获取IP地址
	GetIP() string
	//GetXRealIP 获取请求头X-Real-IP
	GetXRealIP() string
	// GetIPXRealIP 获取相对真实的IP地址,这个是读取的请求头X-Real-IP
	GetIPXRealIP() string
	//GetXForwardedFor 获取请求头X-Forwarded-For
	GetXForwardedFor() string
	// GetIPXForwardedFor 获取反向代理服务器的IP地址,这个是读取的请求头X-Forwarded-For
	GetIPXForwardedFor() string
	// GetFirstIP 获取相对真实的IP地址,获取优先级
	GetFirstIP() string
	// GetPath 获取请求路径
	GetPath() string
	// GetURL 获取请求URL
	GetURL() string

	// ReadBody 读取请求体
	ReadBody() []byte
	// ReadCopyBody 复制一份请求体出来
	ReadCopyBody() []byte
	// ReadBodyJsonBind 读取请求体,json解码到结构体,参数必须是地址类型
	ReadBodyJsonBind(v interface{}) error

	// WriteStd 往响应体里写入数据,按照默认的结构(json编码后)返回
	WriteStd(code int, msg string, data interface{}, other interface{})
	// Write 往响应体里写入数据
	Write(body []byte)
	// WriteString 往响应体里写入数据
	WriteString(s string)
	// WriteJsonMap 往响应体里写入数据,将参数编码为json后再写,自动添加content-type
	WriteJsonMap(mp map[string]interface{})
	// WriteJson 往响应体里写入数据,将参数(必须是地址类型)编码为json后再写,自动添加content-type
	WriteJson(v interface{})

	// FormValue 获取普通表单字段
	FormValue(key string) string
	// FormValueString 获取普通表单字段,如果为空或有错,则赋值参数2
	FormValueString(key string, def ...string) string
	// FormValueInt 获取普通表单字段,如果为空或有错,则赋值参数2
	FormValueInt(key string, def ...int) int
	// FormValueInt64 获取普通表单字段,如果为空或有错,则赋值参数2
	FormValueInt64(key string, def ...int64) int64
	// FormValueFloat64 获取普通表单字段,如果为空或有错,则赋值参数2
	FormValueFloat64(key string, def ...float64) float64
	// FormValueBool 获取普通表单字段,如果为空或有错,则赋值参数2
	FormValueBool(key string, def ...bool) bool
	// FormFile 获取表单上传的文件
	FormFile(key string) (multipart.File, *multipart.FileHeader, error)
	// FormValueBind 将普通表单字段,绑定到结构体,参数为结构体地址类型,结构体成员必须是key-value。
	//如果字段为空或错误,则不对结构体成员赋值,保持原有的默认值。
	//字段名称为结构体成员的tag = json,只支持基本的bool,int全家,uint全家,float全家,string类型。
	FormValueBind(v interface{})

	// FormFileSave 获取表单上传的文件并保存,返回文件名sha256值+扩展名
	FormFileSave(key string, savePath string) (string, error)
	// FormFilesSave 接收多文件并且存储,返回文件名切片,文件名sha256值+扩展名
	FormFilesSave(savePath string) ([]string, error)

	// PostFormValue 获取POST表单字段
	PostFormValue(key string) string
	// PostFormValueString 获取POST表单字段,如果为空或有错,则赋值参数2
	PostFormValueString(key string, def ...string) string
	// PostFormValueInt 获取POST表单字段,如果为空或有错,则赋值参数2
	PostFormValueInt(key string, def ...int) int
	// PostFormValueInt64 获取POST表单字段,如果为空或有错,则赋值参数2
	PostFormValueInt64(key string, def ...int64) int64
	// PostFormValueFloat64 获取POST表单字段,如果为空或有错,则赋值参数2
	PostFormValueFloat64(key string, def ...float64) float64
	// PostFormValueBool 获取POST表单字段,如果为空或有错,则赋值参数2
	PostFormValueBool(key string, def ...bool) bool
	// PostFormValueBind 将POST表单字段,绑定到结构体,参数为结构体地址类型,结构体成员必须是key-value。
	//如果字段为空或错误,则不对结构体成员赋值,保持原有的默认值。
	//字段名称为结构体成员的tag = json,只支持基本的bool,int全家,uint全家,float全家,string类型。
	PostFormValueBind(v interface{})
}

Context 上下文处理

type LogLevel

type LogLevel int
const (
	LevelDebug LogLevel = iota
	LevelInfo
	LevelWarn
	LevelError
	LevelPanic
)

type LogModel

type LogModel int
const (
	ModelStart LogModel = iota
	ModelEnd
)

type Serve

type Serve interface {
	Use(fx func(ctx Context), fxs ...func(ctx Context))
	HandleFunc(urlPath string, hf http.HandlerFunc)
	GET(urlPath string, fx func(ctx Context), fxs ...func(ctx Context))
	POST(urlPath string, fx func(ctx Context), fxs ...func(ctx Context))
	PUT(urlPath string, fx func(ctx Context), fxs ...func(ctx Context))
	DELETE(urlPath string, fx func(ctx Context), fxs ...func(ctx Context))
	PATCH(urlPath string, fx func(ctx Context), fxs ...func(ctx Context))
	HEAD(urlPath string, fx func(ctx Context), fxs ...func(ctx Context))
	OPTIONS(urlPath string, fx func(ctx Context), fxs ...func(ctx Context))
	ANY(urlPath string, fx func(ctx Context), fxs ...func(ctx Context))
	Run(ip string, port uint) error
	RunTLS(ip string, port uint, certFile string, keyFile string) error
}

Serve 服务

func New

func New() Serve

New 创建一个服务,不打印请求日志

func NewDefault

func NewDefault() Serve

NewDefault 创建一个服务,打印日志

Jump to

Keyboard shortcuts

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