First working API.

Simple auth by creating sessions and storing in db
This commit is contained in:
Martin Berg Alstad
2024-08-29 16:43:04 +02:00
commit 5d5e6393ac
50 changed files with 6410 additions and 0 deletions

35
src/database.rs Normal file
View File

@ -0,0 +1,35 @@
use crate::config;
use crate::error::AppError;
use axum::async_trait;
use deadpool_diesel::postgres::BuildError;
use deadpool_diesel::Status;
use diesel_async::pooled_connection::deadpool::{Object, Pool};
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
use diesel_async::AsyncPgConnection;
pub type PgPool = Pool<AsyncPgConnection>;
#[async_trait]
pub trait GetConnection: Clone + Send + Sync {
async fn get(&self) -> Result<Object<AsyncPgConnection>, AppError>;
fn status(&self) -> Status;
}
#[async_trait]
impl GetConnection for PgPool {
async fn get(&self) -> Result<Object<AsyncPgConnection>, AppError> {
self.get().await.map_err(Into::into)
}
fn status(&self) -> Status {
self.status()
}
}
pub(crate) fn create_pool() -> Result<PgPool, BuildError> {
create_pool_from_url(config::DATABASE_URL)
}
pub(crate) fn create_pool_from_url(url: impl Into<String>) -> Result<PgPool, BuildError> {
let config = AsyncDieselConnectionManager::<AsyncPgConnection>::new(url);
Pool::builder(config).max_size(config::POOL_SIZE).build()
}