-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
58 lines (47 loc) · 1.45 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use async_std::io;
use async_std::task;
use mobc::{ConnectionManager, runtime::DefaultExecutor, Pool, AnyFuture};
struct FooManager;
struct FooConnection;
impl FooConnection {
async fn query(&self) -> String {
"nori".to_string()
}
}
impl ConnectionManager for FooManager {
type Connection = FooConnection;
type Error = std::io::Error;
type Executor = DefaultExecutor;
fn get_executor(&self) -> Self::Executor {
DefaultExecutor::current()
}
fn connect(&self) -> AnyFuture<Self::Connection, Self::Error> {
Box::pin(futures::future::ok(FooConnection))
}
fn is_valid(&self, conn: Self::Connection) -> AnyFuture<Self::Connection, Self::Error> {
Box::pin(futures::future::ok(conn))
}
fn has_broken(&self, conn: &mut Option<Self::Connection>) -> bool {
false
}
}
/// Shared application state.
#[derive(Debug)]
struct State {
pool: Pool<FooManager>
}
fn main() -> io::Result<()> {
task::block_on(async {
let pool = Pool::new(FooManager).await.unwrap();
let mut app = tide::with_state(State { pool });
app.at("/submit").post(|req: tide::Request<State>| {
async move {
let conn = &req.state().pool.get().await.unwrap();
let name = conn.query().await;
tide::Response::new(200).body_string(name.to_string())
}
});
app.listen("127.0.0.1:8080").await?;
Ok(())
})
}