
This crate provides integration with tokio-postgres.
Example
```rust,norun
use roa::{App, Context, throw};
use roa::http::StatusCode;
use roapg::{connect, Client};
use std::sync::Arc;
use std::error::Error;
use roa::query::queryparser;
use roa::preload::*;
use asyncstd::task::spawn;
[derive(Clone)]
struct State {
pg: Arc
}
impl State {
pub async fn new(pgurl: &str) -> Result> {
let (client, conn) = connect(&pgurl.parse()?).await?;
spawn(conn);
Ok(Self {pg: Arc::new(client)})
}
}
async fn query(ctx: &mut Context) -> roa::Result {
let id: u32 = ctx.mustquery("id")?.parse()?;
match ctx.pg.queryopt("SELECT * FROM user WHERE id=$1", &[&id]).await? {
Some(row) => {
let value: String = row.get(0);
ctx.write(value);
Ok(())
}
None => throw!(StatusCode::NOT_FOUND),
}
}
[async_std::main]
async fn main() -> Result<(), Box> {
let url = "postgres://fred:secret@localhost/test";
let state = State::new(url).await?;
App::new(state)
.gate(query_parser)
.end(query)
.listen("127.0.0.1:0", |addr| {
println!("Server is listening on {}", addr)
})?.await?;
Ok(())
}
```