init
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
2325
Cargo.lock
generated
Normal file
2325
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
45
Cargo.toml
Normal file
45
Cargo.toml
Normal file
@@ -0,0 +1,45 @@
|
||||
[package]
|
||||
name = "k-core"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[features]
|
||||
default = ["logging", "db-sqlx", "auth"]
|
||||
logging = ["dep:tracing", "dep:tracing-subscriber"]
|
||||
db-sqlx = ["dep:sqlx"]
|
||||
postgres = ["sqlx/postgres"]
|
||||
auth = ["dep:tower-sessions"]
|
||||
|
||||
[dependencies]
|
||||
# Error handling
|
||||
thiserror = "2.0.17"
|
||||
anyhow = "1.0.100"
|
||||
|
||||
# Async
|
||||
tokio = { version = "1.48.0", features = ["full"], optional = true }
|
||||
|
||||
# Database
|
||||
sqlx = { version = "0.8.6", features = [
|
||||
"runtime-tokio",
|
||||
"macros",
|
||||
"sqlite",
|
||||
"postgres",
|
||||
"chrono",
|
||||
"uuid",
|
||||
], optional = true }
|
||||
sqlx-core = { version = "0.8.6", optional = true }
|
||||
|
||||
# Logging
|
||||
tracing = { version = "0.1", optional = true }
|
||||
tracing-subscriber = { version = "0.3.22", features = [
|
||||
"env-filter",
|
||||
"fmt",
|
||||
], optional = true }
|
||||
|
||||
# Auth
|
||||
tower-sessions = { version = "0.14.0", optional = true }
|
||||
|
||||
# Utils
|
||||
chrono = { version = "0.4.42", features = ["serde"] }
|
||||
uuid = { version = "1.19.0", features = ["v4", "serde"] }
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
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