|
@@ -11,12 +11,17 @@ impl FangAction for SetServer {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#[derive(Clone)]
|
|
|
-pub struct UsePostgres(
|
|
|
- Postgres
|
|
|
-);
|
|
|
-impl UsePostgres {
|
|
|
- pub async fn init() -> Self {
|
|
|
+impl Postgres {
|
|
|
+ pub async fn init() -> impl FangAction {
|
|
|
+ #[derive(Clone)]
|
|
|
+ pub struct UsePostgres(Postgres);
|
|
|
+ impl FangAction for UsePostgres {
|
|
|
+ #[inline(always)]
|
|
|
+ async fn fore<'a>(&'a self, req: &'a mut Request) -> Result<(), Response> {
|
|
|
+ Ok(req.memorize(self.0.clone()))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
macro_rules! load_env {
|
|
|
($($name:ident as $t:ty)*) => {$(
|
|
|
#[allow(non_snake_case)]
|
|
@@ -33,64 +38,13 @@ impl UsePostgres {
|
|
|
MIN_CONNECTIONS as u32
|
|
|
DATABASE_URL as String
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
let pool = sqlx::postgres::PgPoolOptions::new()
|
|
|
.max_connections(MAX_CONNECTIONS)
|
|
|
.min_connections(MIN_CONNECTIONS)
|
|
|
.connect(&DATABASE_URL).await
|
|
|
.unwrap();
|
|
|
-
|
|
|
- Self(pool.into())
|
|
|
- }
|
|
|
-}
|
|
|
-impl FangAction for UsePostgres {
|
|
|
- #[inline(always)]
|
|
|
- async fn fore<'a>(&'a self, req: &'a mut Request) -> Result<(), Response> {
|
|
|
- Ok(req.memorize(self.0.clone()))
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-/*
|
|
|
-impl Postgres {
|
|
|
- pub async fn init() -> impl FangAction {
|
|
|
- #[derive(Clone)]
|
|
|
- pub struct UsePostgres(Postgres);
|
|
|
-
|
|
|
- impl FangAction for UsePostgres {
|
|
|
- #[inline(always)]
|
|
|
- async fn fore<'a>(&'a self, req: &'a mut Request) -> Result<(), Response> {
|
|
|
- req.memorize(self.0.clone());
|
|
|
- Ok(())
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- macro_rules! load_env {
|
|
|
- ($($name:ident as $t:ty)*) => {
|
|
|
- $(
|
|
|
- #[allow(non_snake_case)]
|
|
|
- let $name = ::std::env::var(stringify!($name))
|
|
|
- .expect(concat!(
|
|
|
- "Failed to load environment variable ",
|
|
|
- "`", stringify!($name), "`"
|
|
|
- ))
|
|
|
- .parse::<$t>()
|
|
|
- .unwrap();
|
|
|
- )*
|
|
|
- };
|
|
|
- } load_env! {
|
|
|
- MAX_CONNECTIONS as u32
|
|
|
- MIN_CONNECTIONS as u32
|
|
|
- DATABASE_URL as String
|
|
|
- }
|
|
|
-
|
|
|
- UsePostgres(Self(
|
|
|
- sqlx::postgres::PgPoolOptions::new()
|
|
|
- .max_connections(MAX_CONNECTIONS)
|
|
|
- .min_connections(MIN_CONNECTIONS)
|
|
|
- .connect(&DATABASE_URL).await
|
|
|
- .unwrap()
|
|
|
- ))
|
|
|
+
|
|
|
+ UsePostgres(pool.into())
|
|
|
}
|
|
|
}
|
|
|
-*/
|