parent
0bec2a0e8c
commit
89ee547111
@ -1,12 +1,30 @@
|
|||||||
mod simple;
|
mod simple;
|
||||||
|
|
||||||
mod http;
|
|
||||||
mod app;
|
mod app;
|
||||||
mod base;
|
mod base;
|
||||||
mod data;
|
mod data;
|
||||||
|
mod http;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), sqlx::Error> {
|
async fn main() -> Result<(), sqlx::Error> {
|
||||||
let db_pool = simple::database::connect_pool().await?;
|
// fetch database configuration from environment variables
|
||||||
|
let db_address = std::env::var("POSTGRES_ADDRESS").expect("No database address specified");
|
||||||
|
|
||||||
|
// build a new shared pool of postgres connections
|
||||||
|
// by wrapping the pool object into an std::Arc
|
||||||
|
// make it so it can be cloned in a shallow manner
|
||||||
|
// and shared between many threads
|
||||||
|
let db_pool = simple::database::make_shared(
|
||||||
|
sqlx::postgres::PgPoolOptions::new()
|
||||||
|
// set a maximum of 5 concurrent active connections
|
||||||
|
// when the limit is reached any additional contexts
|
||||||
|
// will block until a connection is available
|
||||||
|
.max_connections(5)
|
||||||
|
// the address is expected to be a postgres connection string
|
||||||
|
// in the format postgres://user:password@address:port/database
|
||||||
|
.connect(db_address.as_str())
|
||||||
|
.await?,
|
||||||
|
);
|
||||||
|
|
||||||
Ok(http::listen(db_pool, ([127, 0, 0, 1], 8000)).await)
|
Ok(http::listen(db_pool, ([127, 0, 0, 1], 8000)).await)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,7 @@
|
|||||||
pub type PgSharedPool = std::sync::Arc<sqlx::Pool<sqlx::Postgres>>;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub async fn connect_pool() -> Result<PgSharedPool, sqlx::Error> {
|
pub type PgSharedPool = Arc<sqlx::Pool<sqlx::Postgres>>;
|
||||||
Ok(
|
|
||||||
std::sync::Arc::new(
|
pub fn make_shared(conn: sqlx::Pool<sqlx::Postgres>) -> PgSharedPool {
|
||||||
sqlx::postgres::PgPoolOptions::new()
|
Arc::new(conn)
|
||||||
.max_connections(5)
|
|
||||||
.connect("postgresql://postgres:chapuleta@127.0.0.1:5432")
|
|
||||||
.await?
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +0,0 @@
|
|||||||
#[derive(Debug)]
|
|
||||||
pub enum HandlingError {
|
|
||||||
DBError(sqlx::Error),
|
|
||||||
NotFound,
|
|
||||||
InternalError
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<sqlx::Error> for HandlingError {
|
|
||||||
fn from(e : sqlx::Error) -> HandlingError {
|
|
||||||
HandlingError::DBError(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,5 +1,20 @@
|
|||||||
|
use std::future::Future;
|
||||||
|
use std::pin::Pin;
|
||||||
|
|
||||||
pub mod database;
|
pub mod database;
|
||||||
pub mod handling;
|
|
||||||
pub mod init;
|
|
||||||
pub mod http;
|
pub mod http;
|
||||||
|
|
||||||
|
pub type TraitFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum HandlingError {
|
||||||
|
DBError(sqlx::Error),
|
||||||
|
NotFound,
|
||||||
|
InternalError,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<sqlx::Error> for HandlingError {
|
||||||
|
fn from(e: sqlx::Error) -> HandlingError {
|
||||||
|
HandlingError::DBError(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in new issue