Crates.io | argan |
lib.rs | argan |
version | 0.0.2 |
source | src |
created_at | 2023-11-19 18:13:52.853264 |
updated_at | 2024-05-15 16:16:48.549943 |
description | A web framework for Rust. |
homepage | |
repository | https://github.com/argan-rs/argan |
max_upload_size | |
id | 1041390 |
size | 599,785 |
A web framework for Rust.
Static, regex, and wildcard URI component patterns.
Resource and handler extensions.
Request data extractors and support for custom guards.
Static file streaming with support for range requests, multipart/byteranges
,
pre-encoded files, and dynamic encoding.
Server-sent events.
WebSockets.
Flexible middleware system compatible with Tower.
Flexible error handling.
Cargo.toml
[dependencies]
argan = { version = "0.0.1", features = ["json", "form"] }
hyper-util = { version = "0.1", features = ["server-auto", "tokio", "service"] }
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["net", "rt-multi-thread", "macros"] }
main.rs
use std::net::SocketAddr;
use argan::{
common::BoxedError,
data::{form::Form, json::Json},
handler::{_get, _post, _wildcard_method},
request::RequestHead,
response::Redirect,
Router,
};
use hyper_util::{
rt::{TokioExecutor, TokioIo},
server::conn::auto as server,
};
use serde::{Deserialize, Serialize};
use tokio::net::TcpListener;
#[tokio::main]
async fn main() -> Result<(), BoxedError> {
let mut router = Router::new();
let root = router.resource_mut("http://www.example.com/");
root.set_handler_for(_get.to(|| async { "Hello, World!" }));
root.subresource_mut("/login").set_handler_for(_post.to(login));
router
.resource_mut("http://example.com/")
.set_handler_for(_wildcard_method.to(Some(|head: RequestHead| async move {
let path = head.uri_ref().path();
Redirect::permanently_to(format!("http://www.example.com{}", path))
})));
let arc_service = router.into_arc_service();
let addr = SocketAddr::from(([127, 0, 0, 1], 8000));
let listener = TcpListener::bind(addr).await?;
loop {
let (stream, _) = listener.accept().await?;
let io = TokioIo::new(stream);
let arc_service = arc_service.clone();
tokio::task::spawn(async move {
let _ = server::Builder::new(TokioExecutor::new())
.serve_connection(io, arc_service)
.await;
});
}
}
async fn login(Form(_credential): Form<Credentials>) -> Json<Token> {
// ...
let token = Token {
jwt: "JWT".to_owned(),
};
Json(token)
}
#[derive(Deserialize)]
struct Credentials {
login: String,
password: String,
}
#[derive(Serialize)]
struct Token {
jwt: String,
}
Pull requests are accepted on the dev
branch.
Any contribution intentionally submitted for inclusion in Argan by you shall be dual licensed under the MIT License and Apache License, Version 2.0, like Argan, without any additional terms or conditions.
Argan is dual-licensed under either the MIT License or Apache License, Version 2.0, at your option.