首页 > 解决方案 > 如何构建超 HTTP 应用程序来处理共享状态?

问题描述

我正在尝试用 Rust 构建一个简单的 API 服务器,以便更好地学习这门语言。这应该接受一些数据PUT /data并存储该数据(当前在内存中以简化问题)。

我写了一个MemDb struct来处理插入和检索数据(有效):

trait HasRecords {
    fn insert(&mut self, record: &Datum) -> ();
    fn exists(&self, record: &Datum) -> bool;
    fn find(&self, opts: &SearchOpts) -> Vec<Datum>;
    fn records(&self) -> Vec<Datum>;
}

pub struct MemDb {
    store: Vec<Datum>, // also has a ::new() method on the struct
}

为了全局保存这个存储,我将创建一个App结构来保存存储,并将一个route_handler函数传递给hyper服务器:

src/app.rs

extern crate hyper;

use db::MemDb;

use futures::future;
use hyper::rt::Future;
use hyper::{Body, Method, Request, Response, StatusCode};
type BoxFut = Box<Future<Item = Response<Body>, Error = hyper::Error> + Send>;

pub struct App {
    storage: MemDb,
}

impl App {
    pub fn new() -> App {
        return App {
            storage: MemDb::new(),
        };
    }
}

pub trait Router {
    fn route_handler(&self) -> Box<Fn(Request<Body>) -> BoxFut>;
}

impl Router for App {
    fn route_handler(&self) -> Box<Fn(Request<Body>) -> BoxFut> {
        return Box::new(|req: Request<Body>| {
            let mut response = Response::new(Body::empty());

            match (req.method(), req.uri().path()) {
                (&Method::GET, "/") => {
                    *response.body_mut() = Body::from("status: ok");
                }
                _ => {
                    *response.status_mut() = StatusCode::NOT_FOUND;
                }
            }

            return Box::new(future::ok(response));
        });
    }
}

然后我App在我的main函数中使用来处理路由:

src/main.rs[提炼]

fn main() {
    println!("Starting server...");
    let addr = ([127, 0, 0, 1], 8080).into();
    let app = App::new();
    let service = || service_fn(app.route_handler().as_ref());

    let server = Server::bind(&addr)
        .serve(service)
        .map_err(|e| eprintln!("server error: {}", e));

    hyper::rt::run(server);

    println!("Server listening on port 8080");
}

我现在收到一个错误,指出传递给的未装箱闭包service_fn无法在线程之间安全共享。

在我直接追查这个错误之前,有没有更好的方法来解决这个问题?我来自更多的动态类型、单线程背景(NodeJS、Python),并且正在努力使用借用检查器。

标签: rustborrow-checkerhyper

解决方案


推荐阅读