hyperlane-macros
2025年6月17日大约 3 分钟hyperlane-macros
hyperlane 宏
安装方式
你可以通过命令行添加依赖:
cargo add hyperlane-macros
可用宏列表
HTTP 方法宏
#[methods(method1, method2, ...)]
- 支持多个HTTP
方法处理#[get]
- 处理GET
方法请求#[post]
- 处理POST
方法请求#[put]
- 处理PUT
方法请求#[delete]
- 处理DELETE
方法请求#[patch]
- 处理PATCH
方法请求#[head]
- 处理HEAD
方法请求#[options]
- 处理OPTIONS
方法请求#[connect]
- 处理CONNECT
方法请求#[trace]
- 处理TRACE
方法请求
协议检查宏
#[ws]
-WebSocket
请求检查#[http]
-HTTP
请求检查#[h2c]
-HTTP/2
明文协议检查#[http0_9]
- 检查是否为HTTP/0.9
#[http1_0]
- 检查是否为HTTP/1.0
#[http1_1]
- 检查是否为HTTP/1.1
#[http1_1_or_higher]
- 检查是否为HTTP/1.1
或更高版本#[http2]
- 检查是否为HTTP/2
#[http3]
- 检查是否为HTTP/3
#[tls]
- 检查是否使用TLS
连接
响应设置宏
#[status_code(code)]
- 设置响应状态码#[reason_phrase("phrase")]
- 设置响应原因短语(Reason Phrase
)
发送操作宏
#[send]
- 发送完整响应#[send_body]
- 仅发送响应体#[send_once]
- 仅发送一次完整响应#[send_once_body]
- 仅发送一次响应体
过滤器宏
#[filter_unknown_method]
- 过滤未知HTTP
方法请求#[filter_unknown_upgrade]
- 过滤未知的升级协议请求#[filter_unknown_version]
- 过滤未知的HTTP
协议版本请求#[filter_unknown]
- 综合过滤未知方法、升级、版本的请求
Hook 宏
#[pre_hook(function_name)]
- 执行当前函数前调用指定函数#[post_hook(function_name)]
- 执行当前函数后调用指定函数
最佳实践提示
使用 pre_hook
和 post_hook
时,不建议将这些宏与其它宏(如 #[get]
、#[post]
、#[http]
等)组合在同一个函数上。建议将处理逻辑拆分至 hook 函数中,否则可能由于宏展开顺序导致行为异常。
使用示例
use hyperlane::*;
#[hyperlane_macros::methods(get, post)]
async fn get_post(ctx: Context) {
let _ = ctx.set_response_body("get_post").await.send().await;
}
#[hyperlane_macros::get]
async fn get(ctx: Context) {
let _ = ctx.set_response_body("get").await.send().await;
}
#[hyperlane_macros::post]
async fn post(ctx: Context) {
let _ = ctx.set_response_body("post").await.send().await;
}
#[hyperlane_macros::connect]
async fn connect(ctx: Context) {
let _ = ctx.set_response_body("connect").await.send().await;
}
#[hyperlane_macros::delete]
async fn delete(ctx: Context) {
let _ = ctx.set_response_body("delete").await.send().await;
}
#[hyperlane_macros::head]
async fn head(ctx: Context) {
let _ = ctx.set_response_body("head").await.send().await;
}
#[hyperlane_macros::options]
async fn options(ctx: Context) {
let _ = ctx.set_response_body("options").await.send().await;
}
#[hyperlane_macros::patch]
async fn patch(ctx: Context) {
let _ = ctx.set_response_body("patch").await.send().await;
}
#[hyperlane_macros::put]
async fn put(ctx: Context) {
let _ = ctx.set_response_body("put").await.send().await;
}
#[hyperlane_macros::trace]
async fn trace(ctx: Context) {
let _ = ctx.set_response_body("trace").await.send().await;
}
fn error_handler(error: String) {
eprintln!("{}", error);
let _ = std::io::Write::flush(&mut std::io::stderr());
}
#[tokio::main]
async fn main() {
let server: Server = Server::new();
server.host("0.0.0.0").await;
server.port(60000).await;
server.error_handler(error_handler).await;
server.route("/get_post", get_post).await;
server.route("/get", get).await;
server.route("/post", post).await;
server.route("/connect", connect).await;
server.route("/delete", delete).await;
server.route("/head", head).await;
server.route("/options", options).await;
server.route("/patch", patch).await;
server.route("/put", put).await;
server.route("/trace", trace).await;
let test = || async move {
server.run().await.unwrap();
};
let _ = tokio::time::timeout(std::time::Duration::from_secs(60), test()).await;
}
许可证
本项目使用 MIT 协议,详情请参见 LICENSE 文件。
贡献
欢迎贡献代码!请提交 issue 或 pull request。
联系方式
如有任何问题,请联系作者 [email protected]。