https://github.com/tobyapi/craft
https://github.com/tobyapi/craft
Last synced: 13 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/tobyapi/craft
- Owner: tobyapi
- Created: 2019-09-02T22:57:23.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-09-04T22:24:19.000Z (over 6 years ago)
- Last Synced: 2025-04-18T01:36:57.134Z (10 months ago)
- Language: Rust
- Size: 2.93 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Craft
Craft is a lightweight HTTP framework.
*Status:* **proof-of-concept**
## Installation
Add this to your application's `Cargo.toml`.
```sh
[dependencies]
craft = "0.1.0"
```
## Usage
```rust
use craft;
use hyper::*;
fn hello(_request: &Request) -> Response {
Response::new(Body::from("Hello, World!"))
}
#[runtime::main]
async fn main() {
let config = craft::Config::new("127.0.0.1", 3000);
let mut handler_stack = craft::Stack::empty();
handler_stack.set(hello);
craft::get("/hello", handler_stack);
craft::start(&config).await;
}
```
Craft takes a modular-first approach to middlewares
```rust
use craft;
use hyper::*;
fn first_handler(_request: &Request, next: &dyn Fn() -> Response) -> Response {
println!("begin first_handler");
let response = next();
println!("end first_handler");
response
}
fn second_handler(_request: &Request, next: &dyn Fn() -> Response) -> Response {
println!("begin second_handler");
let response = next();
println!("end second_handler");
response
}
fn hello(_request: &Request) -> Response {
Response::new(Body::from("Hello, World!"))
}
#[runtime::main]
async fn main() {
let config = craft::Config::new("127.0.0.1", 3000);
let mut handler_stack = craft::Stack::empty();
handler_stack.push(first_handler);
handler_stack.push(second_handler);
handler_stack.set(hello);
craft::get("/hello", handler_stack);
craft::start(&config).await;
}
```
## Development
```sh
$ cargo +nightly run --example hello
$ cargo +nightly run --example multi_handler
$ curl -X GET localhost:3000/hello
Hello, World!
```