hyperlane-macros
hyperlane macro.
Installation
To use this crate, you can run cmd:
cargo add hyperlane-macros
Available Macros
Server Instance Macros
#[hyperlane(variable_name)]
- Create a new Server instance with the specified variable name at the beginning of the function
HTTP Method Macros
#[methods(method1, method2, ...)]
- Accepts multiple HTTP methods#[get]
- GET method handler#[post]
- POST method handler#[put]
- PUT method handler#[delete]
- DELETE method handler#[patch]
- PATCH method handler#[head]
- HEAD method handler#[options]
- OPTIONS method handler#[connect]
- CONNECT method handler#[trace]
- TRACE method handler
Protocol Check Macros
#[ws]
- WebSocket check, ensures function only executes for WebSocket upgrade requests#[http]
- HTTP check, ensures function only executes for standard HTTP requests#[h2c]
- HTTP/2 Cleartext check, ensures function only executes for HTTP/2 cleartext requests#[http0_9]
- HTTP/0.9 check, ensures function only executes for HTTP/0.9 protocol requests#[http1_0]
- HTTP/1.0 check, ensures function only executes for HTTP/1.0 protocol requests#[http1_1]
- HTTP/1.1 check, ensures function only executes for HTTP/1.1 protocol requests#[http1_1_or_higher]
- HTTP/1.1 or higher version check, ensures function only executes for HTTP/1.1 or newer protocol versions#[http2]
- HTTP/2 check, ensures function only executes for HTTP/2 protocol requests#[http3]
- HTTP/3 check, ensures function only executes for HTTP/3 protocol requests#[tls]
- TLS check, ensures function only executes for TLS-secured connections
Response Setting Macros
#[response_status_code(code)]
- Set response status code (supports literals and global constants)#[response_reason_phrase("phrase")]
- Set response reason phrase (supports literals and global constants)#[response_header("key", "value")]
- Set response header (supports literals and global constants)#[response_body("data")]
- Set response body (supports literals and global constants)
Send Operation Macros
#[send]
- Send complete response (headers and body) after function execution#[send_body]
- Send only response body after function execution#[send_once]
- Send complete response exactly once after function execution#[send_once_body]
- Send response body exactly once after function execution
Flush Macros
#[flush]
- Flush response stream after function execution to ensure immediate data transmission
Aborted Macros
#[aborted]
- Handle aborted requests, providing cleanup logic for prematurely terminated connections
Closed Operation Macros
#[closed]
- Handle closed streams, providing cleanup logic for completed connections
Filter Macros
#[filter_unknown_method]
- Filter unknown HTTP methods, handling requests with non-standard methods#[filter_unknown_upgrade]
- Filter unknown upgrade requests, handling requests with non-standard upgrade protocols#[filter_unknown_version]
- Filter unknown HTTP versions, handling requests with non-standard HTTP protocol versions#[filter_unknown]
- Combined filter for unknown method, upgrade, and version
Request Body Macros
#[request_body(variable_name)]
- Extract raw request body into specified variable with RequestBody type#[request_body_json(variable_name: type)]
- Parse request body as JSON into specified variable and type
Attribute Macros
#[attribute(key => variable_name: type)]
- Extract a specific attribute by key into a typed variable
Attributes Macros
#[attributes(variable_name)]
- Get all attributes as a HashMap for comprehensive attribute access
Route Param Macros
#[route_param(key => variable_name)]
- Extract a specific route parameter by key into a variable
Route Params Macros
#[route_params(variable_name)]
- Get all route parameters as a collection
Request Query Macros
#[request_query(key => variable_name)]
- Extract a specific query parameter by key from the URL query string
Request Querys Macros
#[request_querys(variable_name)]
- Get all query parameters as a collection
Request Header Macros
#[request_header(key => variable_name)]
- Extract a specific HTTP header by name from the request
Request Headers Macros
#[request_headers(variable_name)]
- Get all HTTP headers as a collection
Hook Macros
#[pre_hook(function_name)]
- Execute specified function before the main handler function#[post_hook(function_name)]
- Execute specified function after the main handler function
Response Header Macros
#[response_header(key => value)]
- Set a specific HTTP response header with the given key and value
Response Body Macros
#[response_body(value)]
- Set the HTTP response body with the given value
Best Practice Warning
- Request related macros are mostly query functions, while response related macros are mostly assignment functions.
- When using
pre_hook
orpost_hook
macros, it is not recommended to combine them with other macros (such as#[get]
,#[post]
,#[http]
, etc.) on the same function. These macros should be placed in the hook functions themselves. If you are not clear about how macros are expanded, combining them may lead to problematic code behavior.
Example Usage
use *;
use *;
use ;
const TEST_ATTRIBUTE_KEY: &str = "test_attribute_key";
const CUSTOM_STATUS_CODE: i32 = 200;
const CUSTOM_REASON: &str = "Accepted";
const CUSTOM_HEADER_NAME: &str = "X-Custom-Header";
const CUSTOM_HEADER_VALUE: &str = "custom-value";
const RESPONSE_DATA: &str = "{\"status\": \"success\"}";
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
async
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
Contact
For any inquiries, please reach out to the author at root@ltpp.vip.