A web framework for Rust with a focus on removing boilerplate.
Tower Web is:
#[macro_use]
extern crate tower_web;
extern crate tokio;
use tower_web::ServiceBuilder;
use tokio::prelude::*;
/// This type will be part of the web service as a resource.
#[derive(Clone, Debug)]
struct HelloWorld;
/// This will be the JSON response
#[derive(Response)]
struct HelloResponse {
message: &'static str,
}
impl_web! {
impl HelloWorld {
#[get("/")]
#[content_type("json")]
fn hello_world(&self) -> Result<HelloResponse, ()> {
Ok(HelloResponse {
message: "hello world",
})
}
}
}
pub fn main() {
let addr = "127.0.0.1:8080".parse().expect("Invalid address");
println!("Listening on http://{}", addr);
ServiceBuilder::new()
.resource(HelloWorld)
.run(&addr)
.unwrap();
}
Tower Web aims to decouple all HTTP concepts from the application logic. You define a "plain old Rust method" (PORM?). This method takes only the data it needs to complete and returns a struct representing the response. Tower Web does the rest.
The impl_web
macro looks at the definition and generates the glue code,
allowing the method to respond to HTTP requests.
The best way to get started is to read the examples and API docs.
This project is licensed under the MIT license.
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in tower-web
by you, shall be licensed as MIT, without any
additional terms or conditions.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。