httpserver

package
v0.0.0-...-d2d3788 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ERR_NOT_FOUND             = "404 Not Found"
	ERR_SERVICE_UNREACHED     = "503 Service Unreached"
	ERR_METHOD_NOT_ALLOWED    = "405 Method Not Allowed"
	ERR_INTERNAL_SERVER_ERROR = "500 Internal Server Error"
)

Variables

This section is empty.

Functions

func SetLittleFileSize

func SetLittleFileSize(size int64)

func SetMaxRequestBodySize

func SetMaxRequestBodySize(size int64)

Types

type Context

type Context struct {
	// 响应状态码
	StatusCode int

	// 请求路由
	Path string
	// 请求方法
	Method string
	// 查询参数
	QueryParams url.Values

	// 原始请求
	Request *http.Request
	// 原始响应
	Response http.ResponseWriter
	// contains filtered or unexported fields
}

Context 定义请求响应上下文的数据结构

func (*Context) GetBasicAuth

func (own *Context) GetBasicAuth() (username, password string)

GetBasicAuth 获取基本认证的账号密码

func (*Context) GetBytesBody

func (own *Context) GetBytesBody() ([]byte, error)

GetBytesBody 获取bytes格式请求体

func (*Context) GetFormValue

func (own *Context) GetFormValue(key string) string

GetFormParam 获取表单参数

func (*Context) GetJsonBody

func (own *Context) GetJsonBody(model any) error

GetJsonBody 获取json格式请求体

func (*Context) GetStringBody

func (own *Context) GetStringBody() (string, error)

GetJsonBody 获取string格式请求体

func (*Context) Query

func (own *Context) Query(key string) string

Query 获取单个查询参数

func (*Context) QueryMore

func (own *Context) QueryMore(keys ...string) map[string]string

QueryMore 获取多个查询参数

func (*Context) ResponseBytes

func (own *Context) ResponseBytes(code int, data []byte) error

ResponseBytes 响应字节数组

func (*Context) ResponseErrorBytes

func (own *Context) ResponseErrorBytes(code int, err []byte) error

ResponseErrorBytes 响应Bytes错误信息

func (*Context) ResponseErrorJson

func (own *Context) ResponseErrorJson(code int, err any) error

ResponseErrorJson 响应Json错误信息

func (*Context) ResponseErrorString

func (own *Context) ResponseErrorString(code int, err string)

ResponseErrorString 响应String错误信息

func (*Context) ResponseFile

func (own *Context) ResponseFile(code int, filepath, filename string) error

ResponseFile 响应单个文件

func (*Context) ResponseHtml

func (own *Context) ResponseHtml(code int, filename string, data any) error

ResponseHtml 响应单个HTML文件

func (*Context) ResponseHtmlString

func (own *Context) ResponseHtmlString(code int, text string, data any) error

ResponseHtmlString 响应HTML字符串

func (*Context) ResponseJson

func (own *Context) ResponseJson(code int, data any) error

ResponseJson 响应Json格式数据

func (*Context) ResponseString

func (own *Context) ResponseString(code int, data string) error

ResponseString 响应字符串

func (*Context) RunNext

func (own *Context) RunNext()

RunNext 执行下一个中间件

func (*Context) SetRequestHeaders

func (own *Context) SetRequestHeaders(headers map[string]string)

SetRequestHeaders 设置请求头

func (*Context) SetResponseHeaders

func (own *Context) SetResponseHeaders(headers map[string]string)

SetResponseHeaders 设置响应头

func (*Context) SetStatusCode

func (own *Context) SetStatusCode(code int)

SetStatusCode 设置响应状态码

type HandleFunc

type HandleFunc func(*Context)

HandleFunc 定义请求处理函数的类型

func AllowCors

func AllowCors(cors cors.CORS) HandleFunc

AllowCors 返回一个跨域配置中间件

func Log

func Log() HandleFunc

Log 请求日志打印中间件

func Receive

func Receive(receiver *receiver.Receiver) HandleFunc

func Recovery

func Recovery() HandleFunc

Recovery 返回一个异常恢复中间件

func Restrict

func Restrict(limt int) HandleFunc

Restrict 返回一个简单限流中间件

type Hookor

type Hookor interface {
	Hook(err any)
}

type HttpServer

type HttpServer struct {
	// 继承路由组,HTTP服务器本身也是个路由组,即根路由组
	*RouteGroup
	// contains filtered or unexported fields
}

HttpServer 定义HTTP服务器的数据结构

func NewDefaultServer

func NewDefaultServer(addr string) *HttpServer

NewDefaultEngine 创建一个默认 HTTP/HTTPS 服务引擎,自带异常恢复和日志记录

func NewServer

func NewServer(addr string) *HttpServer

NewServer 创建一个默认 HTTP/HTTPS 服务引擎

func (*HttpServer) DisableGeneralOptionsHandler

func (own *HttpServer) DisableGeneralOptionsHandler() *HttpServer

func (*HttpServer) ListenAndServe

func (own *HttpServer) ListenAndServe()

ListenAndServe 启动HTTP服务

func (*HttpServer) ListenAndServeTLS

func (own *HttpServer) ListenAndServeTLS(cert, perm string)

ListenAndServe 运行HTTPS服务

func (*HttpServer) LoadHTMLTemplates

func (own *HttpServer) LoadHTMLTemplates(path string)

func (*HttpServer) ServeHTTP

func (own *HttpServer) ServeHTTP(resp http.ResponseWriter, req *http.Request)

ServeHTTP:实现http.Hanlder接口,处理所有的路由请求

func (*HttpServer) SetHTMLFuncs

func (own *HttpServer) SetHTMLFuncs(funcs template.FuncMap)

func (*HttpServer) WithBaseContextFunc

func (own *HttpServer) WithBaseContextFunc(handleFunc func(net.Listener) context.Context) *HttpServer

WithBaseContextFunc 指定一个函数,返回服务器上传入请求的基本上下文

func (*HttpServer) WithConnContextFunc

func (own *HttpServer) WithConnContextFunc(handleFunc func(ctx context.Context, c net.Conn) context.Context) *HttpServer

WithConnContextFunc 指定一个函数来修改用于新连接的上下文。所提供的ctx是从基本上下文派生的,并且具有ServerContextKey值

func (*HttpServer) WithConnStateFunc

func (own *HttpServer) WithConnStateFunc(handleFunc func(net.Conn, http.ConnState)) *HttpServer

WithConnStateFunc 指定了一个可选的回调函数,当客户端连接更改状态时调用该函数

func (*HttpServer) WithIdleTimeout

func (own *HttpServer) WithIdleTimeout(timeout time.Duration) *HttpServer

func (*HttpServer) WithMaxHeaderBytes

func (own *HttpServer) WithMaxHeaderBytes(size int) *HttpServer

func (*HttpServer) WithReadHeaderTimeout

func (own *HttpServer) WithReadHeaderTimeout(timeout time.Duration) *HttpServer

func (*HttpServer) WithReadTimeout

func (own *HttpServer) WithReadTimeout(timeout time.Duration) *HttpServer

func (*HttpServer) WithTLSConfig

func (own *HttpServer) WithTLSConfig(tls *tls.Config) *HttpServer

func (*HttpServer) WithTLSNextProto

func (own *HttpServer) WithTLSNextProto(TLSUpgrade map[string]func(*http.Server, *tls.Conn, http.Handler)) *HttpServer

WithTLSNextProto 指定一个函数,以便在发生ALPN协议升级时接管所提供TLS连接的所有权。如果TLSNextProto不是nil,则不会自动启用HTTP/2支持

func (*HttpServer) WithWriteTimeout

func (own *HttpServer) WithWriteTimeout(timeout time.Duration) *HttpServer

type Json

type Json map[string]any

Json 定义简单的JSON数据类型

type RouteGroup

type RouteGroup struct {
	// contains filtered or unexported fields
}

RouteGroup 定义路由组的数据结构

func (*RouteGroup) AddMiddlewares

func (own *RouteGroup) AddMiddlewares(middlewares ...HandleFunc)

AddMiddlewares 添加中间件到当前路由组

func (*RouteGroup) DELETE

func (own *RouteGroup) DELETE(part string, handler HandleFunc)

DELETE 添加DELETE请求路由到当前路由组

func (*RouteGroup) GET

func (own *RouteGroup) GET(part string, handler HandleFunc)

GET 添加GET请求路由到当前路由组

func (*RouteGroup) NewGroup

func (own *RouteGroup) NewGroup(prefix string) *RouteGroup

NewRouteGroup 新建一个路由组

func (*RouteGroup) OPTIONS

func (own *RouteGroup) OPTIONS(part string, handler HandleFunc)

OPTIONS 添加DELETE请求路由到当前路由组

func (*RouteGroup) POST

func (own *RouteGroup) POST(part string, handler HandleFunc)

POST 添加POST请求路由到当前路由组

func (*RouteGroup) PUT

func (own *RouteGroup) PUT(part string, handler HandleFunc)

PUT 添加PUT请求路由到当前路由组

func (*RouteGroup) SetStatic

func (own *RouteGroup) SetStatic(part string, root string)

SetStatic 设置静态文件服务路由

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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