leego

package module
v0.0.0-...-74b6a7b Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 33 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HashPasswordByBcrypt

func HashPasswordByBcrypt(password []byte) ([]byte, error)

HashPasswordByBcrypt 加密密码

func ShowLogo()

ShowLogo 显示 logo

func VerifyPasswordByBcrypt

func VerifyPasswordByBcrypt(hashedPassword, originalPassword []byte) bool

VerifyPasswordByBcrypt 验证密码

Types

type Context

type Context struct {
	Request  *http.Request   // 原始请求
	Response response.Writer // 原始响应
	// contains filtered or unexported fields
}

Context 定义上下文的结构

func (*Context) AddError

func (own *Context) AddError(err error)

AddError 添加错误信息

func (*Context) BindJSON

func (own *Context) BindJSON(data any) error

BindJSON 绑定 JSON 格式的数据到指定对象

func (*Context) BindXML

func (own *Context) BindXML(obj any) error

BindXML 绑定 XML 格式的数据到指定对象

func (*Context) GetClientIP

func (own *Context) GetClientIP() string

GetClientIP 获取客户端 IP

func (*Context) GetContentType

func (own *Context) GetContentType() string

GetContentType 获取内容的类型

func (*Context) GetContextValue

func (own *Context) GetContextValue(key any) (any, bool)

GetContextValue 获取上下文附带的键值对参数

func (*Context) GetCookieValue

func (own *Context) GetCookieValue(name string) (string, error)

GetCookieValue 获取 cookie 值

func (*Context) GetErrors

func (own *Context) GetErrors() []error

GetErrors 获取错误信息

func (*Context) GetFormFile

func (own *Context) GetFormFile(filename string) (multipart.File, *multipart.FileHeader, error)

GetFormFile 获取上传的文件信息

func (*Context) GetFormValue

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

GetFormValue 获取表单信息, 可查询 URL 参数和 x-www-form-urlencoded 格式的 body

func (*Context) GetJWTClaims

func (own *Context) GetJWTClaims() (any, bool)

GetJWTClaims 获取 JWT 声明

func (*Context) GetMultipartFormValue

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

GetMultipartFormValue 仅支持查询 multipart/form-data 格式的 body

func (*Context) GetPathValue

func (own *Context) GetPathValue(name string) string

GetPathValue 获取动态路由参数

func (*Context) GetPostFormValue

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

GetPostFormValue 仅支持查询 x-www-form-urlencoded 格式的 body

func (*Context) GetQueryValue

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

GetQueryValue 获取查询参数

func (*Context) GetRemoteIP

func (own *Context) GetRemoteIP() string

GetRemoteIP 获取请求端 IP

func (*Context) GetRequestBody

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

GetRequestBody 获取请求体数据

func (*Context) GetRequestHeader

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

GetRequestHeader 获取请求头字段信息

func (*Context) GetResponseHeader

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

GetResponseHeader 获取响应头字段信息

func (*Context) GetServiceName

func (own *Context) GetServiceName() string

GetServiceName 获取服务名称

func (*Context) GetStartTime

func (own *Context) GetStartTime() time.Time

GetStartTime 获取上下文创建时间

func (*Context) GetStatusCode

func (own *Context) GetStatusCode() int

GetStatusCode 获取响应状态码

func (*Context) GetURL

func (own *Context) GetURL() *url.URL

GetURL 获取 URL

func (*Context) IsWebsocket

func (own *Context) IsWebsocket() bool

IsWebsocket 判断是否为 WebSocket 请求

func (*Context) ResponseBytes

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

ResponseBytes 响应字节数据

func (*Context) ResponseError

func (own *Context) ResponseError(code int, error string)

ResponseError 响应错误信息

func (*Context) ResponseFileAttachment

func (own *Context) ResponseFileAttachment(filepath string)

ResponseFileAttachment 响应文件附件(下载文件)

func (*Context) ResponseFileAttachmentFromFS

func (own *Context) ResponseFileAttachmentFromFS(filepath string, fs http.FileSystem)

ResponseFileAttachmentFromFS 从指定的文件系统响应文件附件(下载文件)

func (*Context) ResponseFileContent

func (own *Context) ResponseFileContent(filepath string)

ResponseFileContent 响应文件内容(页面显示)

func (*Context) ResponseFileContentFromFS

func (own *Context) ResponseFileContentFromFS(filepath string, fs http.FileSystem)

ResponseFileContentFromFS 从指定的文件系统响应文件内容(页面显示)

func (*Context) ResponseHTML

func (own *Context) ResponseHTML(filename, tmplName string, data any) error

ResponseHTML 响应 HTML 内容

func (*Context) ResponseJSON

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

ResponseJSON 响应 JSON 格式的数据

func (*Context) ResponsePlain

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

ResponsePlain 响应纯文本内容

func (*Context) ResponseRedirect

func (own *Context) ResponseRedirect(code int, location string)

ResponseRedirect 响应重定向

func (*Context) ResponseSSEvent

func (own *Context) ResponseSSEvent(event func(ctx context.Context) <-chan *sse.Message) error

ResponseSSEvent 发起 SSE 事件响应

func (*Context) ResponseXML

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

ResponseXML 响应 XML 格式的数据

func (*Context) SaveUploadedFile

func (own *Context) SaveUploadedFile(file *multipart.FileHeader, target string, perm ...fs.FileMode) error

SaveUploadedFile 将上传的表单文件保存到指定的路径

func (*Context) SetCookie

func (own *Context) SetCookie(cookie *http.Cookie)

SetCookie 设置 cookie

func (*Context) SetCookieData

func (own *Context) SetCookieData(name, value string, maxAge int, path, domain string, secure, httpOnly bool)

SetCookieData 设置 cookie

func (*Context) SetJWTClaims

func (own *Context) SetJWTClaims(claims any)

SetJWTClaims 设置 JWT 声明信息

func (*Context) SetRequestHeader

func (own *Context) SetRequestHeader(key, value string)

SetRequestHeader 设置请求头

func (*Context) SetResponseHeader

func (own *Context) SetResponseHeader(key, value string)

SetResponseHeader 设置响应头

func (*Context) SetSameSite

func (own *Context) SetSameSite(sameSite http.SameSite)

SetSameSite 设置 cookie

func (*Context) SetStatusCode

func (own *Context) SetStatusCode(code int)

SetStatusCode 设置响应状态码

func (*Context) WithContext

func (own *Context) WithContext(ctx context.Context) *Context

WithContext 返回一个携带指定上下文的请求

type Engine

type Engine struct {

	// Name 引擎名称
	Name string
	// MaxMultipartMemory 解析 multipart/form-data 多部分表单时最大使用的内存
	MaxMultipartMemory int64
	// UseH2C 是否使用 HTTP2
	UseH2C bool
	// contains filtered or unexported fields
}

Engine 定义引擎的结构

func NewEngine

func NewEngine() *Engine

NewEngine 新建一个引擎

func (*Engine) AddURLBlacklist

func (own *Engine) AddURLBlacklist(urls ...string)

AddURLBlacklist 添加路由黑名单

func (Engine) DELETE

func (own Engine) DELETE(pattern string, handle HandleFunc, tags ...string)

DELETE 处理 DELETE 请求

func (Engine) GET

func (own Engine) GET(pattern string, handle HandleFunc, tags ...string)

GET 处理 GET 请求

func (*Engine) GetAllHandlerCollectors

func (own *Engine) GetAllHandlerCollectors() []*collector.Collector

GetAllHandlerCollectors 获取所有处理器的指标采集器

func (*Engine) GetHTTPHandler

func (own *Engine) GetHTTPHandler() http.Handler

GetHTTPHandler 获取 http.Handler

func (*Engine) GetHandler

func (own *Engine) GetHandler(method, pattern string) (Handler, bool)

GetHandler 获取指定的处理器

func (Engine) Group

func (own Engine) Group(prefix string) Group

Group 在当前路由组的基础上创建一个新的路由组

func (Engine) HEAD

func (own Engine) HEAD(pattern string, handle HandleFunc, tags ...string)

HEAD 处理 HEAD 请求

func (Engine) OPTIONS

func (own Engine) OPTIONS(pattern string, handle HandleFunc, tags ...string)

OPTIONS 处理 OPTIONS 请求

func (Engine) PATCH

func (own Engine) PATCH(pattern string, handle HandleFunc, tags ...string)

PATCH 处理 PATCH 请求

func (Engine) POST

func (own Engine) POST(pattern string, handle HandleFunc, tags ...string)

POST 处理 POST 请求

func (Engine) PUT

func (own Engine) PUT(pattern string, handle HandleFunc, tags ...string)

PUT 处理 PUT 请求

func (*Engine) Run

func (own *Engine) Run(addr string) error

Run 通过指定端口监听和服务 HTTP 请求

func (*Engine) RunFd

func (own *Engine) RunFd(fd int) (err error)

RunFd 通过指定的文件描述符监听和服务 HTTP 请求

func (*Engine) RunListener

func (own *Engine) RunListener(listener net.Listener) (err error)

RunListener 通过指定的网络监听和服务 HTTP 请求

func (*Engine) RunTLS

func (own *Engine) RunTLS(addr, certFile, keyFile string) error

RunTLS 通过指定端口监听和服务 HTTPS 请求

func (*Engine) RunUnix

func (own *Engine) RunUnix(file string) error

RunUnix 使用 Unix 套接字监听和服务 HTTP 请求

func (*Engine) ServeHTTP

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

ServeHTTP 继承 http.Handler 接口

func (Engine) SetStatic

func (own Engine) SetStatic(relativePath, rootPath string, tags ...string)

SetStatic 设置静态文件服务

参数:

  • relativePath:URI 中的相对路径
  • rootPath:静态文件服务的根目录
  • tags:第1个参数为路由名称,后续参数为路由标签

注意:

  • 在处理 index.html 这类默认索引文件时,会尝试重定向到目录路径

func (Engine) SetStaticFS

func (own Engine) SetStaticFS(relativePath string, fs http.FileSystem, tags ...string)

SetStaticFS 设置静态文件服务

参数:

  • relativePath:URI 中的相对路径
  • fs:http.FileSystem 文件系统
  • tags:第1个参数为路由名称,后续参数为路由标签

注意:

  • 在处理 index.html 这类默认索引文件时,会尝试重定向到目录路径

func (Engine) UseMiddlewares

func (own Engine) UseMiddlewares(middlewares ...MiddlewareFunc)

UseMiddlewareFuncs 使用路由组中间件处理函数

func (*Engine) WaitSignalAndShutdown

func (own *Engine) WaitSignalAndShutdown(timeout time.Duration, doBeforeShutdown func() error) error

WaitSignalAndShutdown 优雅关闭服务

type Group

type Group interface {
	// Group 路由分组
	Group(prefix string) Group
	// UseMiddlewares 使用路由中间件函数
	UseMiddlewares(middlewares ...MiddlewareFunc)

	// GET 处理 GET 请求
	GET(pattern string, handle HandleFunc, tags ...string)
	// PUT 处理 PUT 请求
	PUT(pattern string, handle HandleFunc, tags ...string)
	// POST 处理 POST 请求
	POST(pattern string, handle HandleFunc, tags ...string)
	// HEAD 处理 HEAD 请求
	HEAD(pattern string, handle HandleFunc, tags ...string)
	// PATCH 处理 PATCH 请求
	PATCH(pattern string, handle HandleFunc, tags ...string)
	// DELETE 处理 DELETE 请求
	DELETE(pattern string, handle HandleFunc, tags ...string)
	// OPTIONS 处理 OPTIONS 请求
	OPTIONS(pattern string, handle HandleFunc, tags ...string)
	// SetStatic 设置静态文件服务
	SetStatic(relativePath, rootPath string, tags ...string)
	// SetStaticFS 设置静态文件服务
	SetStaticFS(relativePath string, fs http.FileSystem, tags ...string)
}

Group 定义路由组的接口类型

type HandleFunc

type HandleFunc func(ctx *Context)

HandleFunc 定义处理函数的类型

type Handler

type Handler interface {
	// ServeHTTP 处理 HTTP 请求
	ServeHTTP(ctx *Context)
	// Enable 启用处理器
	Enable()
	/*Disable 禁用指定路由
	  - 禁用指定路由后,也不会调用该路由上的任一中间件,且响应状态码为 423
	*/
	Disable()
	// IsEnabled 是否启用该处理器
	IsEnabled() bool
	// GetCollector 获取指标采集器
	GetCollector() *collector.Collector
}

Handler 定义处理器的接口类型

type JSON

type JSON map[string]any

JSON 定义一个简单的 JSON 类型

type MiddlewareFunc

type MiddlewareFunc func(next HandleFunc) HandleFunc

MiddlewareFunc 定义中间件函数的类型

type Router

type Router interface {
	// ServeHTTP 实现 http.Handler 接口
	ServeHTTP(resp http.ResponseWriter, req *http.Request)
	// Insert 插入路由
	Insert(method, pattern string, handler Handler)
	// Search 搜索路由
	Search(method, path string) (Handler, map[string]string, bool)
	// AddBlacklist 添加路由黑名单
	AddBlacklist(urls ...string)
}

Router 定义路由器的接口类型

Directories

Path Synopsis
jwt
sse
test/base command
test
base command
file command
see command
websocket command

Jump to

Keyboard shortcuts

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