Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
uses: actions/cache@v3
with:
path: ~/.cargo/bin/crate
key: crate-cli-11.1.0-${{ runner.os }}
key: crate-cli-11.2.0-${{ runner.os }}
- name: Install crate-cli
if: steps.cache_crate_cli.outputs.cache-hit != 'true'
run: |
Expand Down
24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ categories = ["network-programming", "web-programming"]
exclude.workspace = true

[workspace.package]
version = "23.4.0"
version = "23.4.1"
readme = "README.md"
edition = "2024"
authors = ["root@devhub.vip"]
Expand Down Expand Up @@ -86,13 +86,13 @@ members = [
]

[workspace.dependencies]
hyperlane = { path = ".", version = "23.4.0" }
http-type = { path = "type", version = "23.4.0" }
hyperlane-core = { path = "core", version = "23.4.0" }
http-request = { path = "request", version = "23.4.0" }
http-compress = { path = "compress", version = "23.4.0" }
http-constant = { path = "constant", version = "23.4.0" }
hyperlane-macros = { path = "macros", version = "23.4.0" }
hyperlane = { path = ".", version = "23.4.1" }
http-type = { path = "type", version = "23.4.1" }
hyperlane-core = { path = "core", version = "23.4.1" }
http-request = { path = "request", version = "23.4.1" }
http-compress = { path = "compress", version = "23.4.1" }
http-constant = { path = "constant", version = "23.4.1" }
hyperlane-macros = { path = "macros", version = "23.4.1" }

h3 = "0.0.8"
h2 = "0.4.20"
Expand All @@ -110,18 +110,18 @@ futures = "0.3.34"
h3-quinn = "0.0.10"
io-uring = "0.7.16"
twox-hash = "2.1.5"
crate-cli = "11.1.0"
crate-cli = "11.2.0"
inventory = "0.3.25"
thiserror = "2.0.21"
toml_edit = "0.25.20"
serde-xml-rs = "0.8.2"
serde_json = "1.0.154"
color-output = "11.1.0"
color-output = "11.2.0"
proc-macro2 = "1.0.107"
webpki-roots = "1.0.12"
lombok-macros = "11.1.0"
lombok-macros = "11.2.0"
serde_urlencoded = "0.7.1"
tokio-broadcast = "11.1.0"
tokio-broadcast = "11.2.0"
syn = { version = "3.0.10", features = ["full"] }
socket2 = { version = "0.6.5", features = ["all"] }
tokio = { version = "1.54.1", features = ["full"] }
Expand Down
61 changes: 51 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,48 +51,89 @@ cargo add hyperlane
Replace `src/main.rs` with:

```rust
use hyperlane::{Context, Server, ServerConfig, ServerError, ServerHook, Status, Stream, tokio};
use hyperlane::*;

const ADDRESS: &str = "127.0.0.1:3000";
const ROOT_PATH: &str = "/";
const GREET_PATH: &str = "/hello/{name}";
const GREETING: &str = "Hello from Hyperlane!\n";

#[request_middleware]
struct ServerHeader;

impl ServerHook for ServerHeader {
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}

#[prologue_macros(response_header(SERVER => HYPERLANE))]
async fn handle(self, _: &mut Stream, ctx: &mut Context) -> Status {
Status::Continue
}
}

#[route(ROOT_PATH)]
struct Hello;

impl ServerHook for Hello {
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}

#[hyperlane::send]
#[prologue_macros(
is_get_method,
response_status_code(200),
response_header(CONTENT_TYPE => TEXT_PLAIN),
response_body(GREETING)
)]
#[epilogue_macros(try_send)]
async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status {
ctx.get_mut_response().set_body(GREETING);
Status::Reject
}
}

#[route(GREET_PATH)]
struct Greet;

impl ServerHook for Greet {
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}

#[prologue_macros(
is_get_method,
route_param("name" => name),
response_status_code(200),
response_header(CONTENT_TYPE => TEXT_PLAIN),
response_body(&format!("Hello, {name}!\n"))
)]
#[epilogue_macros(try_send)]
async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status {
Status::Reject
}
}

#[hyperlane(server: Server, config: ServerConfig)]
#[tokio::main]
async fn main() -> Result<(), Box<ServerError>> {
let mut config: ServerConfig = ServerConfig::default();
config.set_address(ADDRESS);
server.server_config(config);

let mut server: Server = Server::default();
server.server_config(config).route::<Hello>(ROOT_PATH);

let control = server.run().await?;
let control: ServerControlHook = server.run().await?;
println!("Listening on http://{ADDRESS}");
control.wait().await;
Ok(())
}
```

Run `cargo run` and query the endpoint:
Run `cargo run` and query the endpoints:

```sh
curl -fsS http://127.0.0.1:3000/
curl -fsS http://127.0.0.1:3000/hello/hyperlane
```

`#[hyperlane::send]` serializes and flushes the response built by the hook; `Status::Reject` stops the remaining pipeline so downstream hooks do not send a duplicate response.
`#[request_middleware]` and `#[route]` register hooks automatically via inventory; `#[hyperlane(server: Server, config: ServerConfig)]` initializes `server` with every discovered hook and constructs `config`; `#[prologue_macros(...)]` and `#[epilogue_macros(try_send)]` guard HTTP methods, extract route parameters, populate status/headers/body, and flush the response without manual boilerplate.

---

Expand Down
20 changes: 17 additions & 3 deletions macros/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,35 @@ cargo add hyperlane-macros
## Usage

```rust
use hyperlane::{Context, ServerHook, Status, Stream};
use hyperlane::*;

#[route("/health")]
struct HealthRoute;

impl ServerHook for HealthRoute {
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}

#[hyperlane::send]
#[prologue_macros(
is_get_method,
response_status_code(200),
response_header(CONTENT_TYPE => TEXT_PLAIN),
response_body("ok")
)]
#[epilogue_macros(try_send)]
async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status {
ctx.get_mut_response().set_body("ok");
Status::Reject
}
}

#[hyperlane(server: Server, config: ServerConfig)]
async fn start_server() -> Result<(), Box<ServerError>> {
config.set_address("127.0.0.1:3000");
server.server_config(config);
server.run().await?.wait().await;
Ok(())
}
```

## Verification
Expand Down
Loading