68c7aad179
The code is divided in three crates: - plume-common, for the ActivityPub module, and some common utils - plume-models, for the models and database-related code - plume, the app itself This new organization will allow to test it more easily, but also to create other tools that only reuse a little part of the code (for instance a Wordpress import tool, that would just use the plume-models crate)
38 lines
1.2 KiB
Rust
38 lines
1.2 KiB
Rust
use diesel::{
|
|
pg::PgConnection,
|
|
r2d2::{ConnectionManager, Pool, PooledConnection}
|
|
};
|
|
use rocket::{Request, State, Outcome, http::Status, request::{self, FromRequest}};
|
|
use std::ops::Deref;
|
|
|
|
pub type PgPool = Pool<ConnectionManager<PgConnection>>;
|
|
|
|
// From rocket documentation
|
|
|
|
// Connection request guard type: a wrapper around an r2d2 pooled connection.
|
|
pub struct DbConn(pub PooledConnection<ConnectionManager<PgConnection>>);
|
|
|
|
/// Attempts to retrieve a single connection from the managed database pool. If
|
|
/// no pool is currently managed, fails with an `InternalServerError` status. If
|
|
/// no connections are available, fails with a `ServiceUnavailable` status.
|
|
impl<'a, 'r> FromRequest<'a, 'r> for DbConn {
|
|
type Error = ();
|
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
|
let pool = request.guard::<State<PgPool>>()?;
|
|
match pool.get() {
|
|
Ok(conn) => Outcome::Success(DbConn(conn)),
|
|
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ()))
|
|
}
|
|
}
|
|
}
|
|
|
|
// For the convenience of using an &DbConn as an &PgConnection.
|
|
impl Deref for DbConn {
|
|
type Target = PgConnection;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.0
|
|
}
|
|
}
|