init
This commit is contained in:
21
src/db.rs
Normal file
21
src/db.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
#[cfg(feature = "db-sqlx")]
|
||||
pub async fn connect_sqlite(url: &str) -> Result<Pool<Sqlite>, sqlx::Error> {
|
||||
sqlx::sqlite::SqlitePoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect(url)
|
||||
.await
|
||||
}
|
||||
|
||||
// Future expansion for Postgres
|
||||
#[cfg(feature = "postgres")]
|
||||
use sqlx::Postgres;
|
||||
|
||||
#[cfg(feature = "postgres")]
|
||||
pub async fn connect_postgres(url: &str) -> Result<Pool<Postgres>, sqlx::Error> {
|
||||
sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect(url)
|
||||
.await
|
||||
}
|
||||
17
src/error.rs
Normal file
17
src/error.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum AppError {
|
||||
#[cfg(feature = "db-sqlx")]
|
||||
#[error("Database error: {0}")]
|
||||
Database(#[from] sqlx::Error),
|
||||
|
||||
#[error("Not found")]
|
||||
NotFound,
|
||||
|
||||
#[error("Validation error: {0}")]
|
||||
Validation(String),
|
||||
|
||||
#[error("Internal server error")]
|
||||
Internal,
|
||||
}
|
||||
7
src/lib.rs
Normal file
7
src/lib.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
#[cfg(feature = "logging")]
|
||||
pub mod logging;
|
||||
|
||||
#[cfg(feature = "db-sqlx")]
|
||||
pub mod db;
|
||||
|
||||
pub mod error;
|
||||
11
src/logging.rs
Normal file
11
src/logging.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
pub fn init(service_name: &str) {
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| format!("{}=debug,tower_http=debug", service_name).into()),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
}
|
||||
Reference in New Issue
Block a user