Tower-web — Rust 的快速、无样板 Web 框架

1,032 阅读2分钟
原文链接: click.aliyun.com

Tower-web — Rust 的快速、无样板 Web 框架

技术小能手 2018-10-30 11:08:14 浏览46 评论0
  • 云栖社区
  • web

摘要: Tower-web :Rust 的快速、无样板 Web 框架 Tower Web 介绍: 快速:完全异步,基于Tokio和Hyper构建。 符合人体工程学:Tower-web将HTTP与应用程序逻辑分离,删除所有样板。

Tower-web :Rust 的快速、无样板 Web 框架

Tower Web 介绍:

  • 快速:完全异步,基于TokioHyper构建。

  • 符合人体工程学:Tower-web将HTTP与应用程序逻辑分离,删除所有样板。

  • 适用于Rust stable:稳定。

Tower Web是一个快速的Web框架,旨在删除样板。

目标是将所有HTTP概念与应用程序逻辑分离。使用“普通Rust类型”实现应用程序,Tower Web使用宏来生成必要的粘合剂,以便将应用程序作为HTTP服务提供。 

#[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 {
            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 基于Tokio (Rust并发框架与平台)和Hyper(Rust的HTTP server框架)构建。

Tower Web 框架属于Tokio平台生态重要部分。

本文来自云栖社区合作伙伴“开源中国”

本文作者:h4cd

原文链接

【云栖快讯】诚邀您免费学习【阿里云总监课】!首次集齐12位阿里云技术高管,从理论到实践倾囊相授,还有30元代金券,戳链接马上领!  详情请点击
分享到:

相关文章

网友评论