rust - 如何构建超 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),并且正在努力使用借用检查器。
解决方案
推荐阅读
- flutter - 在不滚动的情况下更改 listviewbuilder 中的项目状态
- pyspark - 在 Databricks 笔记本中通过 pyspark 读取 url
- android - 如何获得多个 Spinner 值?
- import - 当从 node_modules 内部导入 carbon-icons-svelte 错误时,无法在 svelte 组件中使用 Jest 测试:Jest 遇到了意外的令牌
- java - 如何为这个看似简单的案例制作正则表达式?
- python - python请求-抓取谷歌搜索会阻碍数据框
- html - Django 如何安全地显示来自用户输入表单的链接
- ruby-on-rails - 将数据库导出为 CSV 并将其导入新数据库会导致问题
- java - 扫描仪不使用 scan.NextInt() 和 .hasNext() 循环关闭
- wordpress - WP Bootstrap 4 带有子下拉菜单的可点击顶级导航