In today’s digital era, building high-performance and reliable web services is the goal of every developer. Today, we sincerely recommend Hyperlane, a lightweight and high-performance Rust HTTP server framework designed to simplify your network service development.
Project Overview
Hyperlane is built entirely in pure Rust using the standard library, offering true cross-platform compatibility across Windows, Linux, and macOS—with a consistent API experience on all platforms. It leverages Tokio’s asynchronous runtime for seamless network communication without requiring platform-specific dependencies.
Key Features:
- Supports HTTP request parsing, response building, and TCP communication.
- Includes middleware support for both requests and responses, as well as WebSocket and Server-Sent Events (SSE) for flexible and efficient real-time communication.
- A simple and user-friendly API design enables developers to quickly get started and build modern web services.
Quick Start
To begin using Hyperlane, simply add it to your project with the following command:
cargo add hyperlane
We also provide a quick-start project, hyperlane-quick-start, to help you quickly set up and run your first Hyperlane application.
Clone the project:
git clone https://github.com/eastspire/hyperlane-quick-start.git
Navigate to the project directory:
cd hyperlane-quick-start
Run the project:
cargo run
You can also run, stop, or restart the service in the background:
# Run in the background
cargo run -d
# Stop the service
cargo run stop
# Restart the service
cargo run restart
# Restart in the background
cargo run restart -d
Usage Example
The following example demonstrates how to set up middleware, routing, and WebSocket support in a simple Hyperlane application:
use hyperlane::*;
async fn request_middleware(ctx: Context) {
let socket_addr: String = ctx.get_socket_addr_or_default_string().await;
ctx.set_response_header(SERVER, HYPERLANE)
.await
.set_response_header(CONNECTION, CONNECTION_KEEP_ALIVE)
.await
.set_response_header(CONTENT_TYPE, content_type_charset(TEXT_PLAIN, UTF8))
.await
.set_response_header(DATE, gmt())
.await
.set_response_header("SocketAddr", socket_addr)
.await;
}
async fn response_middleware(ctx: Context) {
let _ = ctx.send().await;
let request: String = ctx.get_request_string().await;
let response: String = ctx.get_response_string().await;
ctx.log_info(&request, log_handler)
.await
.log_info(&response, log_handler)
.await;
}
async fn root_route(ctx: Context) {
ctx.set_response_status_code(200)
.await
.set_response_body("Hello hyperlane => /")
.await;
}
async fn websocket_route(ctx: Context) {
let request_body: Vec<u8> = ctx.get_request_body().await;
let _ = ctx.send_response_body(request_body).await;
}
#[tokio::main]
async fn main() {
let server: Server = Server::new();
server.host("0.0.0.0").await;
server.port(60000).await;
server.enable_nodelay().await;
server.log_dir("./logs").await;
server.enable_inner_log().await;
server.enable_inner_print().await;
server.log_size(100_024_000).await;
server.http_line_buffer_size(4096).await;
server.websocket_buffer_size(4096).await;
server.request_middleware(request_middleware).await;
server.response_middleware(response_middleware).await;
server.route("/", root_route).await;
server.route("/websocket", websocket_route).await;
let test_string: String = "Hello hyperlane".to_owned();
server
.route(
"/test/:text",
future_fn!(test_string, |ctx: Context| {
let param: RouteParams = ctx.get_route_params().await;
println_success!(format!("{:?}", param));
println_success!(test_string);
panic!("Test panic");
}),
)
.await;
server.run().await.unwrap();
}
Performance Benchmark
We conducted performance benchmarks on Hyperlane to demonstrate its excellent performance. The results below were obtained using wrk
and ab
tools.
wrk Benchmark
Command:
wrk -c360 -d60s http://127.0.0.1:60000/
Results:
Rank | Framework | QPS |
---|---|---|
1 | Tokio | 340130.92 |
2 | Hyperlane | 324323.71 |
3 | Rocket | 298945.31 |
4 | Rust Stdlib | 291218.96 |
5 | Gin (Go) | 242570.16 |
6 | Go Stdlib | 234178.93 |
7 | Node.js Stdlib | 139412.13 |
ab Benchmark
Command:
ab -n 1000000 -c 1000 -r -k http://127.0.0.1:60000/
Results:
Rank | Framework | QPS |
---|---|---|
1 | Tokio | 308596.26 |
2 | Hyperlane | 307568.90 |
3 | Rocket | 267931.52 |
4 | Rust Stdlib | 260514.56 |
5 | Go Stdlib | 226550.34 |
6 | Gin (Go) | 224296.16 |
7 | Node.js Stdlib | 85357.18 |
License and Contributions
Hyperlane is licensed under the MIT license. For details, please refer to the license file. Community contributions are welcome! You can participate by submitting issues or creating pull requests.
If you have any questions or need further assistance, feel free to contact the author: root@ltpp.vip.
Choose Hyperlane—choose the future of high performance and simplified development. Start your journey today and experience the speed and convenience of Hyperlane!
Top comments (0)