feat: Introduce smart-features Cargo feature to conditionally compile smart note functionalities and their dependencies.
This commit is contained in:
@@ -4,9 +4,25 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
default-run = "notes-api"
|
||||
|
||||
[features]
|
||||
default = ["sqlite", "smart-features"]
|
||||
sqlite = [
|
||||
"notes-infra/sqlite",
|
||||
"tower-sessions-sqlx-store/sqlite",
|
||||
"sqlx/sqlite",
|
||||
]
|
||||
postgres = [
|
||||
"notes-infra/postgres",
|
||||
"tower-sessions-sqlx-store/postgres",
|
||||
"sqlx/postgres",
|
||||
]
|
||||
smart-features = ["notes-infra/smart-features", "dep:async-nats"]
|
||||
|
||||
[dependencies]
|
||||
notes-domain = { path = "../notes-domain" }
|
||||
notes-infra = { path = "../notes-infra", features = ["sqlite"] }
|
||||
notes-infra = { path = "../notes-infra", default-features = false, features = [
|
||||
"sqlite",
|
||||
] }
|
||||
|
||||
# Web framework
|
||||
axum = { version = "0.8.8", features = ["macros"] }
|
||||
@@ -20,7 +36,7 @@ tower-sessions-sqlx-store = { version = "0.15", features = ["sqlite"] }
|
||||
password-auth = "1.0"
|
||||
time = "0.3"
|
||||
async-trait = "0.1.89"
|
||||
async-nats = "0.39"
|
||||
async-nats = { version = "0.39", optional = true }
|
||||
|
||||
# Async runtime
|
||||
tokio = { version = "1.48.0", features = ["full"] }
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#[cfg(feature = "smart-features")]
|
||||
use notes_infra::factory::{EmbeddingProvider, VectorProvider};
|
||||
use std::env;
|
||||
|
||||
@@ -10,7 +11,9 @@ pub struct Config {
|
||||
pub session_secret: String,
|
||||
pub cors_allowed_origins: Vec<String>,
|
||||
pub allow_registration: bool,
|
||||
#[cfg(feature = "smart-features")]
|
||||
pub embedding_provider: EmbeddingProvider,
|
||||
#[cfg(feature = "smart-features")]
|
||||
pub vector_provider: VectorProvider,
|
||||
pub broker_url: String,
|
||||
}
|
||||
@@ -25,7 +28,9 @@ impl Default for Config {
|
||||
.to_string(),
|
||||
cors_allowed_origins: vec!["http://localhost:5173".to_string()],
|
||||
allow_registration: true,
|
||||
#[cfg(feature = "smart-features")]
|
||||
embedding_provider: EmbeddingProvider::FastEmbed,
|
||||
#[cfg(feature = "smart-features")]
|
||||
vector_provider: VectorProvider::Qdrant {
|
||||
url: "http://localhost:6334".to_string(),
|
||||
collection: "notes".to_string(),
|
||||
@@ -66,11 +71,13 @@ impl Config {
|
||||
.map(|s| s.to_lowercase() == "true")
|
||||
.unwrap_or(true);
|
||||
|
||||
#[cfg(feature = "smart-features")]
|
||||
let embedding_provider = match env::var("EMBEDDING_PROVIDER").unwrap_or_default().as_str() {
|
||||
// Future: "ollama" => EmbeddingProvider::Ollama(...),
|
||||
_ => EmbeddingProvider::FastEmbed,
|
||||
};
|
||||
|
||||
#[cfg(feature = "smart-features")]
|
||||
let vector_provider = match env::var("VECTOR_PROVIDER").unwrap_or_default().as_str() {
|
||||
// Future: "postgres" => ...
|
||||
_ => VectorProvider::Qdrant {
|
||||
@@ -89,7 +96,9 @@ impl Config {
|
||||
session_secret,
|
||||
cors_allowed_origins,
|
||||
allow_registration,
|
||||
#[cfg(feature = "smart-features")]
|
||||
embedding_provider,
|
||||
#[cfg(feature = "smart-features")]
|
||||
vector_provider,
|
||||
broker_url,
|
||||
}
|
||||
|
||||
@@ -43,9 +43,11 @@ async fn main() -> anyhow::Result<()> {
|
||||
tracing::info!("Connecting to database: {}", config.database_url);
|
||||
let db_config = DatabaseConfig::new(&config.database_url);
|
||||
|
||||
#[cfg(feature = "smart-features")]
|
||||
use notes_infra::factory::build_link_repository;
|
||||
use notes_infra::factory::{
|
||||
build_database_pool, build_link_repository, build_note_repository, build_session_store,
|
||||
build_tag_repository, build_user_repository,
|
||||
build_database_pool, build_note_repository, build_session_store, build_tag_repository,
|
||||
build_user_repository,
|
||||
};
|
||||
let pool = build_database_pool(&db_config)
|
||||
.await
|
||||
@@ -73,6 +75,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
let user_repo = build_user_repository(&pool)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!(e))?;
|
||||
#[cfg(feature = "smart-features")]
|
||||
let link_repo = build_link_repository(&pool)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!(e))?;
|
||||
@@ -84,20 +87,26 @@ async fn main() -> anyhow::Result<()> {
|
||||
let user_service = Arc::new(UserService::new(user_repo.clone()));
|
||||
|
||||
// Connect to NATS
|
||||
tracing::info!("Connecting to NATS: {}", config.broker_url);
|
||||
let nats_client = async_nats::connect(&config.broker_url)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("NATS connection failed: {}", e))?;
|
||||
// Connect to NATS
|
||||
#[cfg(feature = "smart-features")]
|
||||
let nats_client = {
|
||||
tracing::info!("Connecting to NATS: {}", config.broker_url);
|
||||
async_nats::connect(&config.broker_url)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("NATS connection failed: {}", e))?
|
||||
};
|
||||
|
||||
// Create application state
|
||||
let state = AppState::new(
|
||||
note_repo,
|
||||
tag_repo,
|
||||
user_repo.clone(),
|
||||
#[cfg(feature = "smart-features")]
|
||||
link_repo,
|
||||
note_service,
|
||||
tag_service,
|
||||
user_service,
|
||||
#[cfg(feature = "smart-features")]
|
||||
nats_client,
|
||||
config.clone(),
|
||||
);
|
||||
|
||||
@@ -15,7 +15,7 @@ use crate::state::AppState;
|
||||
|
||||
/// Create the API v1 router
|
||||
pub fn api_v1_router() -> Router<AppState> {
|
||||
Router::new()
|
||||
let router = Router::new()
|
||||
// Auth routes
|
||||
.route("/auth/register", post(auth::register))
|
||||
.route("/auth/login", post(auth::login))
|
||||
@@ -29,8 +29,12 @@ pub fn api_v1_router() -> Router<AppState> {
|
||||
.patch(notes::update_note)
|
||||
.delete(notes::delete_note),
|
||||
)
|
||||
.route("/notes/{id}/versions", get(notes::list_note_versions))
|
||||
.route("/notes/{id}/related", get(notes::get_related_notes))
|
||||
.route("/notes/{id}/versions", get(notes::list_note_versions));
|
||||
|
||||
#[cfg(feature = "smart-features")]
|
||||
let router = router.route("/notes/{id}/related", get(notes::get_related_notes));
|
||||
|
||||
router
|
||||
// Search route
|
||||
.route("/search", get(notes::search_notes))
|
||||
// Import/Export routes
|
||||
|
||||
@@ -83,15 +83,18 @@ pub async fn create_note(
|
||||
let note = state.note_service.create_note(domain_req).await?;
|
||||
|
||||
// Publish event
|
||||
let payload = serde_json::to_vec(¬e).unwrap_or_default();
|
||||
if let Err(e) = state
|
||||
.nats_client
|
||||
.publish("notes.updated", payload.into())
|
||||
.await
|
||||
#[cfg(feature = "smart-features")]
|
||||
{
|
||||
tracing::error!("Failed to publish notes.updated event: {}", e);
|
||||
} else {
|
||||
tracing::info!("Published notes.updated event for note {}", note.id);
|
||||
let payload = serde_json::to_vec(¬e).unwrap_or_default();
|
||||
if let Err(e) = state
|
||||
.nats_client
|
||||
.publish("notes.updated", payload.into())
|
||||
.await
|
||||
{
|
||||
tracing::error!("Failed to publish notes.updated event: {}", e);
|
||||
} else {
|
||||
tracing::info!("Published notes.updated event for note {}", note.id);
|
||||
}
|
||||
}
|
||||
|
||||
Ok((StatusCode::CREATED, Json(NoteResponse::from(note))))
|
||||
@@ -150,15 +153,18 @@ pub async fn update_note(
|
||||
let note = state.note_service.update_note(domain_req).await?;
|
||||
|
||||
// Publish event
|
||||
let payload = serde_json::to_vec(¬e).unwrap_or_default();
|
||||
if let Err(e) = state
|
||||
.nats_client
|
||||
.publish("notes.updated", payload.into())
|
||||
.await
|
||||
#[cfg(feature = "smart-features")]
|
||||
{
|
||||
tracing::error!("Failed to publish notes.updated event: {}", e);
|
||||
} else {
|
||||
tracing::info!("Published notes.updated event for note {}", note.id);
|
||||
let payload = serde_json::to_vec(¬e).unwrap_or_default();
|
||||
if let Err(e) = state
|
||||
.nats_client
|
||||
.publish("notes.updated", payload.into())
|
||||
.await
|
||||
{
|
||||
tracing::error!("Failed to publish notes.updated event: {}", e);
|
||||
} else {
|
||||
tracing::info!("Published notes.updated event for note {}", note.id);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Json(NoteResponse::from(note)))
|
||||
@@ -228,6 +234,9 @@ pub async fn list_note_versions(
|
||||
|
||||
/// Get related notes
|
||||
/// GET /api/v1/notes/:id/related
|
||||
/// Get related notes
|
||||
/// GET /api/v1/notes/:id/related
|
||||
#[cfg(feature = "smart-features")]
|
||||
pub async fn get_related_notes(
|
||||
State(state): State<AppState>,
|
||||
auth: AuthSession<AuthBackend>,
|
||||
|
||||
@@ -11,10 +11,12 @@ pub struct AppState {
|
||||
pub note_repo: Arc<dyn NoteRepository>,
|
||||
pub tag_repo: Arc<dyn TagRepository>,
|
||||
pub user_repo: Arc<dyn UserRepository>,
|
||||
#[cfg(feature = "smart-features")]
|
||||
pub link_repo: Arc<dyn notes_domain::ports::LinkRepository>,
|
||||
pub note_service: Arc<NoteService>,
|
||||
pub tag_service: Arc<TagService>,
|
||||
pub user_service: Arc<UserService>,
|
||||
#[cfg(feature = "smart-features")]
|
||||
pub nats_client: async_nats::Client,
|
||||
pub config: Config,
|
||||
}
|
||||
@@ -24,21 +26,23 @@ impl AppState {
|
||||
note_repo: Arc<dyn NoteRepository>,
|
||||
tag_repo: Arc<dyn TagRepository>,
|
||||
user_repo: Arc<dyn UserRepository>,
|
||||
link_repo: Arc<dyn notes_domain::ports::LinkRepository>,
|
||||
#[cfg(feature = "smart-features")] link_repo: Arc<dyn notes_domain::ports::LinkRepository>,
|
||||
note_service: Arc<NoteService>,
|
||||
tag_service: Arc<TagService>,
|
||||
user_service: Arc<UserService>,
|
||||
nats_client: async_nats::Client,
|
||||
#[cfg(feature = "smart-features")] nats_client: async_nats::Client,
|
||||
config: Config,
|
||||
) -> Self {
|
||||
Self {
|
||||
note_repo,
|
||||
tag_repo,
|
||||
user_repo,
|
||||
#[cfg(feature = "smart-features")]
|
||||
link_repo,
|
||||
note_service,
|
||||
tag_service,
|
||||
user_service,
|
||||
#[cfg(feature = "smart-features")]
|
||||
nats_client,
|
||||
config,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user