httpsvr

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2022 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	/*
		1: 登陆认证失败
		2:用户请求数据处理失败
		3:服务端处理失败
	*/
	StatusOK           = 0
	StatusInvalidSign  = 1000
	StatusLoginFailed  = 1003
	StatusAccessDenied = 1199
	// 请求参数合法性验证
	StatusNotLegalInput = 1201
	// json请求校验失败
	StatusBodyInvalid          = 1204
	StatusDataProcessingFailed = 3000
	StatusSaveDataFailed       = 3001
	StatusNotFound             = 3010
)
View Source
const (
	TypeJson  = "application/json; charset=UTF-8"
	TypePlain = "text/plain; charset=UTF-8"
	TypeHtml  = "text/html; charset=UTF-8"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type APIInfo

type APIInfo struct {
	Handler Handle
	Name    string
	// Perm 权限
	Perm uint
}

APIInfo api信息

type Context

type Context struct {
	Req         *http.Request
	Writer      *ResponseWriter
	Params      httprouter.Params
	Keys        map[string]interface{}
	Response    interface{}
	Begin       time.Time
	NoLog       bool
	OpLog       *bytes.Buffer
	Body        *bytes.Buffer
	BodyLen     int64
	BodyMap     map[string]interface{}
	ContextType string
	Perm        uint
}

Context http会话期间的session

func (*Context) RemoteAddr

func (ctx *Context) RemoteAddr(xff bool) string

RemoteAddr 获取客户端IP

func (*Context) ReqBody

func (ctx *Context) ReqBody(out interface{}) (ecode int, err error)

ReqBody 获取请求中的Body

func (*Context) Upload

func (ctx *Context) Upload(savedir, allowExt string) (string, string, error)

Upload 文件上传处理。兼容chunked方式。

不是chunk方式上传时,保存的文件名为随机生成。
chunk方式上传时,保存的文件名为提交的文件名。这种方式上传的文件名不能重名。
返回的参数:提交的文件名,保存的文件名,错误信息。当chunk方式上传时,在最后一个chunk请求处理
之前,返回的错误信息是:Chunking,表示chunk未完成。

type Handle

type Handle func(ctx *Context)

Handle 增加全局配置信息

type HttpTest

type HttpTest struct {
	Router   *Router
	OPT      options.GlobalOPT
	IsSimple bool
	Valid    bool
}

func NewTest

func NewTest(conf string) HttpTest

func (HttpTest) HttpApiTest

func (ht HttpTest) HttpApiTest(req *http.Request)

type PrometheusList

type PrometheusList struct {
	RequestsInFlight         prometheus.Gauge
	ConcurrencyLimitExceeded prometheus.Counter
	RequestsTotal            *prometheus.CounterVec
	RequestsURLTotal         *prometheus.CounterVec
}

type RespMsg

type RespMsg struct {
	Code        int         `json:"code"`
	DataList    interface{} `json:"dataList"`
	TotalRecord int         `json:"totalRecord"`
	PageSize    int         `json:"pagesize"`
	PageNo      int         `json:"pageno"`
	Message     string      `json:"message"`
}

RespMsg 响应给客户端的数据结构

func NewRespMsg

func NewRespMsg(code int) *RespMsg

NewRespMsg 创建新的响应信息结构

func (*RespMsg) Bytes

func (rmsg *RespMsg) Bytes() (rstr []byte)

Bytes 转换为byte

func (*RespMsg) SetCode

func (rmsg *RespMsg) SetCode(code int) error

SetCode 设置错误代码及错误信息

func (*RespMsg) Strings

func (rmsg *RespMsg) Strings() (rstr string)

Strings 转换为string

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

ResponseWriter 增加status字段

func NewResponseWriter

func NewResponseWriter(w http.ResponseWriter) *ResponseWriter

NewResponseWriter 创建新实例

func (*ResponseWriter) AbortWithStatus

func (lrw *ResponseWriter) AbortWithStatus(code int)

AbortWithStatus 记录status

func (*ResponseWriter) Size

func (lrw *ResponseWriter) Size() int

Size 获取size

func (*ResponseWriter) Status

func (lrw *ResponseWriter) Status() int

Status 获取status

func (*ResponseWriter) Write

func (lrw *ResponseWriter) Write(data []byte) (n int, err error)

Write 记录status

func (*ResponseWriter) WriteHeader

func (lrw *ResponseWriter) WriteHeader(code int)

WriteHeader 记录status

func (*ResponseWriter) WriteString

func (lrw *ResponseWriter) WriteString(s string) (n int, err error)

WriteString 获取Written

func (*ResponseWriter) Written

func (lrw *ResponseWriter) Written() bool

Written 获取Written

type Router

type Router struct {
	httprouter.Router

	PanicHandler    func(*Context)
	ResponseHandler func(*Context)
	Prefix          string
	ErrLog          *mlog.Logger
	// Registry                 prometheus.Registerer
	RequestsInFlight         prometheus.Gauge
	ConcurrencyLimitExceeded prometheus.Counter

	PrometheusList PrometheusList
	// contains filtered or unexported fields
}

Router 自定义http router

func NewRouter

func NewRouter(sver string, alog *mlog.Logger) *Router

NewRouter 创建一个新的router

func (*Router) AddRoute

func (router *Router) AddRoute(method, path string, perm uint, conf interface{}, handlers ...Handle)

AddRoute 增加route信息

func (*Router) NotFound

func (router *Router) NotFound()

NotFound 增加route信息

func (*Router) PutRouterList

func (router *Router) PutRouterList(rl *RouterList, conf interface{})

PutRouterList 添加routerlist到路由表

func (*Router) ServeHTTP

func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP 增加日志输出

func (*Router) SetupPrometheus

func (router *Router) SetupPrometheus()

type RouterList

type RouterList struct {
	API       map[string]APIInfo
	Privilege Handle
}

RouterList api路由列表

func NewRouteList

func NewRouteList() *RouterList

NewRouteList 初始化路由表

Jump to

Keyboard shortcuts

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