refactor(service): migrate auth, git service, agent from slog to tracing
- Remove all use slog::* imports and slog::Logger fields/parameters - Replace slog::info!/warn!/error! with tracing::info!/warn!/error! - AppService: remove pub logs: slog::Logger field, update callers of AppEmail::init(), MessageProducer::new(), RoomService::new(), start_email_worker(), start_room_workers() - auth/: captcha, email, login, logout, password, register, rsa, totp - git/: archive, blame, blob, branch, commit, contributors, diff, refs, star, tag, tree, watch - agent/: billing (ai_usage_recorded), code_review, pr_summary, sync - project/activity.rs, workspace/alert.rs
This commit is contained in:
parent
b4024aa690
commit
773da34fab
@ -20,7 +20,6 @@ use models::workspaces::workspace_billing_history;
|
|||||||
use rust_decimal::Decimal;
|
use rust_decimal::Decimal;
|
||||||
use sea_orm::*;
|
use sea_orm::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use slog::info;
|
|
||||||
use utoipa::ToSchema;
|
use utoipa::ToSchema;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@ -137,14 +136,15 @@ impl AppService {
|
|||||||
updated.updated_at = Set(now);
|
updated.updated_at = Set(now);
|
||||||
updated.update(&self.db).await?;
|
updated.update(&self.db).await?;
|
||||||
|
|
||||||
info!(self.logs, "ai_usage_recorded";
|
tracing::info!(
|
||||||
"project_id" => %project_uid,
|
project_id = %project_uid,
|
||||||
"model_id" => %model_id,
|
model_id = %model_id,
|
||||||
"input_tokens" => input_tokens,
|
input_tokens = input_tokens,
|
||||||
"output_tokens" => output_tokens,
|
output_tokens = output_tokens,
|
||||||
"cost" => total_cost,
|
cost = %total_cost,
|
||||||
"currency" => %currency,
|
currency = %currency,
|
||||||
"workspace_id" => %workspace_id.to_string()
|
workspace_id = %workspace_id.to_string(),
|
||||||
|
"ai_usage_recorded"
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(BillingRecord {
|
Ok(BillingRecord {
|
||||||
@ -197,13 +197,14 @@ impl AppService {
|
|||||||
updated.balance = Set(new_balance);
|
updated.balance = Set(new_balance);
|
||||||
updated.update(&self.db).await?;
|
updated.update(&self.db).await?;
|
||||||
|
|
||||||
info!(self.logs, "ai_usage_recorded";
|
tracing::info!(
|
||||||
"project_id" => %project_uid,
|
project_id = %project_uid,
|
||||||
"model_id" => %model_id,
|
model_id = %model_id,
|
||||||
"input_tokens" => input_tokens,
|
input_tokens = input_tokens,
|
||||||
"output_tokens" => output_tokens,
|
output_tokens = output_tokens,
|
||||||
"cost" => total_cost,
|
cost = %total_cost,
|
||||||
"currency" => %currency
|
currency = %currency,
|
||||||
|
"ai_usage_recorded"
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(BillingRecord {
|
Ok(BillingRecord {
|
||||||
|
|||||||
@ -149,7 +149,7 @@ impl AppService {
|
|||||||
|
|
||||||
let prompt = build_code_review_prompt(&pr, &diff);
|
let prompt = build_code_review_prompt(&pr, &diff);
|
||||||
|
|
||||||
let ai_response = call_ai_model(&model.name, &prompt, &self.config, self.logs.clone()).await?;
|
let ai_response = call_ai_model(&model.name, &prompt, &self.config).await?;
|
||||||
|
|
||||||
// Record billing (non-fatal — log warning but don't fail the review).
|
// Record billing (non-fatal — log warning but don't fail the review).
|
||||||
let billing = self
|
let billing = self
|
||||||
@ -161,11 +161,10 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.inspect_err(|e| {
|
.inspect_err(|e| {
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
self.logs,
|
project = %repo.project,
|
||||||
"failed to record AI billing for code review";
|
error = ?e,
|
||||||
"project" => %repo.project,
|
"failed to record AI billing for code review"
|
||||||
"error" => ?e
|
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
@ -226,11 +225,10 @@ impl AppService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
self.logs,
|
path = %comment.path,
|
||||||
"failed to create AI review comment";
|
error = ?e,
|
||||||
"path" => %comment.path,
|
"failed to create AI review comment"
|
||||||
"error" => ?e
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -392,7 +390,6 @@ async fn call_ai_model(
|
|||||||
model_name: &str,
|
model_name: &str,
|
||||||
prompt: &str,
|
prompt: &str,
|
||||||
app_config: &config::AppConfig,
|
app_config: &config::AppConfig,
|
||||||
logger: slog::Logger,
|
|
||||||
) -> Result<agent::AiCallResponse, AppError> {
|
) -> Result<agent::AiCallResponse, AppError> {
|
||||||
let api_key = app_config
|
let api_key = app_config
|
||||||
.ai_api_key()
|
.ai_api_key()
|
||||||
@ -402,7 +399,7 @@ async fn call_ai_model(
|
|||||||
.ai_basic_url()
|
.ai_basic_url()
|
||||||
.unwrap_or_else(|_| "https://api.openai.com".into());
|
.unwrap_or_else(|_| "https://api.openai.com".into());
|
||||||
|
|
||||||
let client_config = agent::AiClientConfig::new(api_key, logger).with_base_url(base_url);
|
let client_config = agent::AiClientConfig::new(api_key).with_base_url(base_url);
|
||||||
|
|
||||||
let messages = vec![
|
let messages = vec![
|
||||||
async_openai::types::chat::ChatCompletionRequestMessage::User(
|
async_openai::types::chat::ChatCompletionRequestMessage::User(
|
||||||
|
|||||||
@ -125,7 +125,6 @@ async fn call_ai_model_for_description(
|
|||||||
model_name: &str,
|
model_name: &str,
|
||||||
prompt: &str,
|
prompt: &str,
|
||||||
app_config: &config::AppConfig,
|
app_config: &config::AppConfig,
|
||||||
logger: slog::Logger,
|
|
||||||
) -> Result<agent::AiCallResponse, AppError> {
|
) -> Result<agent::AiCallResponse, AppError> {
|
||||||
let api_key = app_config
|
let api_key = app_config
|
||||||
.ai_api_key()
|
.ai_api_key()
|
||||||
@ -135,7 +134,7 @@ async fn call_ai_model_for_description(
|
|||||||
.ai_basic_url()
|
.ai_basic_url()
|
||||||
.unwrap_or_else(|_| "https://api.openai.com".into());
|
.unwrap_or_else(|_| "https://api.openai.com".into());
|
||||||
|
|
||||||
let client_config = agent::AiClientConfig::new(api_key, logger).with_base_url(base_url);
|
let client_config = agent::AiClientConfig::new(api_key).with_base_url(base_url);
|
||||||
|
|
||||||
let messages = vec![
|
let messages = vec![
|
||||||
async_openai::types::chat::ChatCompletionRequestMessage::User(
|
async_openai::types::chat::ChatCompletionRequestMessage::User(
|
||||||
@ -240,7 +239,7 @@ impl AppService {
|
|||||||
|
|
||||||
// Build prompt and call AI
|
// Build prompt and call AI
|
||||||
let prompt = build_description_prompt(&pr.title, pr.body.as_deref(), &diff);
|
let prompt = build_description_prompt(&pr.title, pr.body.as_deref(), &diff);
|
||||||
let ai_response = call_ai_model_for_description(&model.name, &prompt, &self.config, self.logs.clone()).await?;
|
let ai_response = call_ai_model_for_description(&model.name, &prompt, &self.config).await?;
|
||||||
|
|
||||||
// Record billing (non-fatal).
|
// Record billing (non-fatal).
|
||||||
let billing = self
|
let billing = self
|
||||||
@ -252,11 +251,10 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.inspect_err(|e| {
|
.inspect_err(|e| {
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
self.logs,
|
project = %repo.project,
|
||||||
"failed to record AI billing for PR description";
|
error = ?e,
|
||||||
"project" => %repo.project,
|
"failed to record AI billing for PR description"
|
||||||
"error" => ?e
|
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
|
|||||||
@ -15,7 +15,6 @@
|
|||||||
use async_openai::Client;
|
use async_openai::Client;
|
||||||
use async_openai::config::OpenAIConfig;
|
use async_openai::config::OpenAIConfig;
|
||||||
use async_openai::types::models::Model as OpenAiModel;
|
use async_openai::types::models::Model as OpenAiModel;
|
||||||
use slog::Logger;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::task::JoinHandle;
|
use tokio::task::JoinHandle;
|
||||||
use tokio::time::interval;
|
use tokio::time::interval;
|
||||||
@ -524,16 +523,12 @@ async fn upsert_parameter_profile(
|
|||||||
/// This handles non-OpenRouter endpoints (e.g. Bailian, MiniMax) gracefully.
|
/// This handles non-OpenRouter endpoints (e.g. Bailian, MiniMax) gracefully.
|
||||||
async fn sync_models_direct(
|
async fn sync_models_direct(
|
||||||
db: &AppDatabase,
|
db: &AppDatabase,
|
||||||
log: &Logger,
|
|
||||||
available_ids: &std::collections::HashSet<String>,
|
available_ids: &std::collections::HashSet<String>,
|
||||||
) -> SyncModelsResponse {
|
) -> SyncModelsResponse {
|
||||||
slog::info!(
|
tracing::info!(
|
||||||
log,
|
model_count = available_ids.len(),
|
||||||
"{}",
|
|
||||||
format!(
|
|
||||||
"sync_models_direct: {} models from endpoint (no OpenRouter metadata)",
|
"sync_models_direct: {} models from endpoint (no OpenRouter metadata)",
|
||||||
available_ids.len()
|
available_ids.len()
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut models_created = 0i64;
|
let mut models_created = 0i64;
|
||||||
@ -548,13 +543,10 @@ async fn sync_models_direct(
|
|||||||
let provider = match upsert_provider(db, provider_slug).await {
|
let provider = match upsert_provider(db, provider_slug).await {
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
log,
|
provider = %provider_slug,
|
||||||
"{}",
|
error = ?e,
|
||||||
format!(
|
"sync_models_direct: upsert_provider error"
|
||||||
"sync_models_direct: upsert_provider error provider={} {:?}",
|
|
||||||
provider_slug, e
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -571,13 +563,10 @@ async fn sync_models_direct(
|
|||||||
(m, n)
|
(m, n)
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
log,
|
model = %model_id,
|
||||||
"{}",
|
error = ?e,
|
||||||
format!(
|
"sync_models_direct: upsert_model_direct error"
|
||||||
"sync_models_direct: upsert_model_direct error model={} {:?}",
|
|
||||||
model_id, e
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -587,13 +576,10 @@ async fn sync_models_direct(
|
|||||||
match upsert_version(db, model_record.id).await {
|
match upsert_version(db, model_record.id).await {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
log,
|
model = %model_id,
|
||||||
"{}",
|
error = ?e,
|
||||||
format!(
|
"sync_models_direct: upsert_version error"
|
||||||
"sync_models_direct: upsert_version error model={} {:?}",
|
|
||||||
model_id, e
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -617,20 +603,15 @@ async fn sync_models_direct(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
slog::info!(
|
tracing::info!(
|
||||||
log,
|
matched = available_ids.len(),
|
||||||
"{}",
|
|
||||||
format!(
|
|
||||||
"sync_models_direct complete: matched={} created={} updated={} \
|
|
||||||
versions={} pricing={} capabilities={} profiles={}",
|
|
||||||
available_ids.len(),
|
|
||||||
models_created,
|
models_created,
|
||||||
models_updated,
|
models_updated,
|
||||||
versions_created,
|
versions_created,
|
||||||
pricing_created,
|
pricing_created,
|
||||||
capabilities_created,
|
capabilities_created,
|
||||||
profiles_created
|
profiles_created,
|
||||||
)
|
"sync_models_direct complete"
|
||||||
);
|
);
|
||||||
|
|
||||||
SyncModelsResponse {
|
SyncModelsResponse {
|
||||||
@ -647,7 +628,6 @@ async fn sync_models_direct(
|
|||||||
|
|
||||||
async fn fetch_openrouter_models(
|
async fn fetch_openrouter_models(
|
||||||
client: &reqwest::Client,
|
client: &reqwest::Client,
|
||||||
log: &Logger,
|
|
||||||
) -> Result<OpenRouterResponse, String> {
|
) -> Result<OpenRouterResponse, String> {
|
||||||
const MAX_RETRIES: u32 = 3;
|
const MAX_RETRIES: u32 = 3;
|
||||||
const BASE_DELAY_MS: u64 = 1_000;
|
const BASE_DELAY_MS: u64 = 1_000;
|
||||||
@ -695,13 +675,14 @@ async fn fetch_openrouter_models(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
let delay_ms = BASE_DELAY_MS * (1 << (attempt - 1));
|
let delay_ms = BASE_DELAY_MS * (1 << (attempt - 1));
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
log,
|
attempt = attempt,
|
||||||
"{}",
|
max_retries = MAX_RETRIES,
|
||||||
format!(
|
kind = %kind,
|
||||||
"OpenRouter connection attempt {}/{} failed: [{}] url={} error={:?}. retrying in {}ms",
|
url = %url,
|
||||||
attempt, MAX_RETRIES, kind, url, e, delay_ms
|
error = ?e,
|
||||||
)
|
retry_delay_ms = delay_ms,
|
||||||
|
"OpenRouter connection attempt failed, retrying"
|
||||||
);
|
);
|
||||||
sleep(Duration::from_millis(delay_ms)).await;
|
sleep(Duration::from_millis(delay_ms)).await;
|
||||||
}
|
}
|
||||||
@ -754,18 +735,15 @@ impl AppService {
|
|||||||
.map(|m: OpenAiModel| m.id)
|
.map(|m: OpenAiModel| m.id)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
slog::info!(
|
tracing::info!(
|
||||||
self.logs,
|
model_count = available_ids.len(),
|
||||||
"{}",
|
|
||||||
format!(
|
|
||||||
"sync_upstream_models: {} accessible models found",
|
"sync_upstream_models: {} accessible models found",
|
||||||
available_ids.len()
|
available_ids.len()
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Step 2: fetch OpenRouter metadata.
|
// Step 2: fetch OpenRouter metadata.
|
||||||
let http_client = reqwest::Client::new();
|
let http_client = reqwest::Client::new();
|
||||||
let or_resp: OpenRouterResponse = fetch_openrouter_models(&http_client, &self.logs)
|
let or_resp: OpenRouterResponse = fetch_openrouter_models(&http_client)
|
||||||
.await
|
.await
|
||||||
.map_err(AppError::InternalServerError)?;
|
.map_err(AppError::InternalServerError)?;
|
||||||
|
|
||||||
@ -782,15 +760,11 @@ impl AppService {
|
|||||||
// Fallback: if no OpenRouter metadata matches, sync models directly from
|
// Fallback: if no OpenRouter metadata matches, sync models directly from
|
||||||
// the user's endpoint (handles Bailian/MiniMax and other non-OpenRouter providers).
|
// the user's endpoint (handles Bailian/MiniMax and other non-OpenRouter providers).
|
||||||
if filtered_count == 0 && !available_ids.is_empty() {
|
if filtered_count == 0 && !available_ids.is_empty() {
|
||||||
slog::info!(
|
tracing::info!(
|
||||||
self.logs,
|
model_count = available_ids.len(),
|
||||||
"{}",
|
"sync_upstream_models: no OpenRouter matches, falling back to direct sync"
|
||||||
format!(
|
|
||||||
"sync_upstream_models: no OpenRouter matches, falling back to direct sync for {} models",
|
|
||||||
available_ids.len()
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
return Ok(sync_models_direct(&self.db, &self.logs, &available_ids).await);
|
return Ok(sync_models_direct(&self.db, &available_ids).await);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut models_created = 0i64;
|
let mut models_created = 0i64;
|
||||||
@ -805,13 +779,10 @@ impl AppService {
|
|||||||
let provider = match upsert_provider(&self.db, provider_slug).await {
|
let provider = match upsert_provider(&self.db, provider_slug).await {
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
self.logs,
|
provider = %provider_slug,
|
||||||
"{}",
|
error = ?e,
|
||||||
format!(
|
"sync_upstream_models: upsert_provider error"
|
||||||
"sync_upstream_models: upsert_provider error provider={} {:?}",
|
|
||||||
provider_slug, e
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -828,13 +799,10 @@ impl AppService {
|
|||||||
(m, n)
|
(m, n)
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
self.logs,
|
model = %or_model.id,
|
||||||
"{}",
|
error = ?e,
|
||||||
format!(
|
"sync_upstream_models: upsert_model error"
|
||||||
"sync_upstream_models: upsert_model error model={} {:?}",
|
|
||||||
or_model.id, e
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -844,13 +812,10 @@ impl AppService {
|
|||||||
match upsert_version(&self.db, model_record.id).await {
|
match upsert_version(&self.db, model_record.id).await {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
self.logs,
|
model = %or_model.id,
|
||||||
"{}",
|
error = ?e,
|
||||||
format!(
|
"sync_upstream_models: upsert_version error"
|
||||||
"sync_upstream_models: upsert_version error model={} {:?}",
|
|
||||||
or_model.id, e
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -862,13 +827,10 @@ impl AppService {
|
|||||||
if let Err(e) =
|
if let Err(e) =
|
||||||
upsert_pricing(&self.db, version_record.id, or_model.pricing.as_ref()).await
|
upsert_pricing(&self.db, version_record.id, or_model.pricing.as_ref()).await
|
||||||
{
|
{
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
self.logs,
|
model = %or_model.id,
|
||||||
"{}",
|
error = ?e,
|
||||||
format!(
|
"sync_upstream_models: upsert_pricing error"
|
||||||
"sync_upstream_models: upsert_pricing error model={} {:?}",
|
|
||||||
or_model.id, e
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
pricing_created += 1;
|
pricing_created += 1;
|
||||||
@ -887,15 +849,12 @@ impl AppService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
slog::info!(
|
tracing::info!(
|
||||||
self.logs,
|
|
||||||
"{}",
|
|
||||||
format!(
|
|
||||||
"sync_upstream_models: synced {} accessible models ({}/{} new/updated)",
|
|
||||||
filtered_count,
|
filtered_count,
|
||||||
models_created,
|
models_created,
|
||||||
models_updated
|
models_updated,
|
||||||
)
|
"sync_upstream_models: synced {} accessible models",
|
||||||
|
filtered_count
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(SyncModelsResponse {
|
Ok(SyncModelsResponse {
|
||||||
@ -914,18 +873,17 @@ impl AppService {
|
|||||||
/// Failures are logged but do not stop the task — it keeps retrying.
|
/// Failures are logged but do not stop the task — it keeps retrying.
|
||||||
pub fn start_sync_task(self) -> JoinHandle<()> {
|
pub fn start_sync_task(self) -> JoinHandle<()> {
|
||||||
let db = self.db.clone();
|
let db = self.db.clone();
|
||||||
let log = self.logs.clone();
|
|
||||||
let ai_api_key = self.config.ai_api_key().ok();
|
let ai_api_key = self.config.ai_api_key().ok();
|
||||||
let ai_base_url = self.config.ai_basic_url().ok();
|
let ai_base_url = self.config.ai_basic_url().ok();
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
// Run once immediately on startup before taking traffic.
|
// Run once immediately on startup before taking traffic.
|
||||||
Self::sync_once(&db, &log, ai_api_key.clone(), ai_base_url.clone()).await;
|
Self::sync_once(&db, ai_api_key.clone(), ai_base_url.clone()).await;
|
||||||
|
|
||||||
let mut tick = interval(Duration::from_secs(60 * 10));
|
let mut tick = interval(Duration::from_secs(60 * 10));
|
||||||
loop {
|
loop {
|
||||||
tick.tick().await;
|
tick.tick().await;
|
||||||
Self::sync_once(&db, &log, ai_api_key.clone(), ai_base_url.clone()).await;
|
Self::sync_once(&db, ai_api_key.clone(), ai_base_url.clone()).await;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -934,7 +892,6 @@ impl AppService {
|
|||||||
/// so the periodic task never stops.
|
/// so the periodic task never stops.
|
||||||
async fn sync_once(
|
async fn sync_once(
|
||||||
db: &AppDatabase,
|
db: &AppDatabase,
|
||||||
log: &Logger,
|
|
||||||
ai_api_key: Option<String>,
|
ai_api_key: Option<String>,
|
||||||
ai_base_url: Option<String>,
|
ai_base_url: Option<String>,
|
||||||
) {
|
) {
|
||||||
@ -942,7 +899,7 @@ impl AppService {
|
|||||||
let ai_client = match build_ai_client_from_parts(ai_api_key, ai_base_url) {
|
let ai_client = match build_ai_client_from_parts(ai_api_key, ai_base_url) {
|
||||||
Ok(c) => c,
|
Ok(c) => c,
|
||||||
Err(msg) => {
|
Err(msg) => {
|
||||||
slog::warn!(log, "{}", format!("OpenRouter model sync: {}", msg));
|
tracing::warn!(error = %msg, "OpenRouter model sync");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -950,20 +907,16 @@ impl AppService {
|
|||||||
let available_ids: std::collections::HashSet<String> = match ai_client.models().list().await {
|
let available_ids: std::collections::HashSet<String> = match ai_client.models().list().await {
|
||||||
Ok(resp) => resp.data.into_iter().map(|m: OpenAiModel| m.id).collect(),
|
Ok(resp) => resp.data.into_iter().map(|m: OpenAiModel| m.id).collect(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "OpenRouter model sync: failed to list available models");
|
||||||
log,
|
|
||||||
"{}",
|
|
||||||
format!("OpenRouter model sync: failed to list available models: {}", e)
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let http_client = reqwest::Client::new();
|
let http_client = reqwest::Client::new();
|
||||||
let or_resp = match fetch_openrouter_models(&http_client, log).await {
|
let or_resp = match fetch_openrouter_models(&http_client).await {
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(msg) => {
|
Err(msg) => {
|
||||||
slog::warn!(log, "{}", format!("OpenRouter model sync: {}", msg));
|
tracing::warn!(error = %msg, "OpenRouter model sync");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -980,15 +933,11 @@ impl AppService {
|
|||||||
// Fallback: if no OpenRouter metadata matches, sync models directly from
|
// Fallback: if no OpenRouter metadata matches, sync models directly from
|
||||||
// the user's endpoint (handles Bailian/MiniMax and other non-OpenRouter providers).
|
// the user's endpoint (handles Bailian/MiniMax and other non-OpenRouter providers).
|
||||||
if filtered_count == 0 && !available_ids.is_empty() {
|
if filtered_count == 0 && !available_ids.is_empty() {
|
||||||
slog::info!(
|
tracing::info!(
|
||||||
log,
|
model_count = available_ids.len(),
|
||||||
"{}",
|
"OpenRouter model sync: no matches, falling back to direct sync"
|
||||||
format!(
|
|
||||||
"OpenRouter model sync: no matches, falling back to direct sync for {} models",
|
|
||||||
available_ids.len()
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
sync_models_direct(db, log, &available_ids).await;
|
sync_models_direct(db, &available_ids).await;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1004,13 +953,10 @@ impl AppService {
|
|||||||
let provider = match upsert_provider(db, provider_slug).await {
|
let provider = match upsert_provider(db, provider_slug).await {
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
log,
|
provider = %provider_slug,
|
||||||
"{}",
|
error = ?e,
|
||||||
format!(
|
"OpenRouter model sync: upsert_provider error"
|
||||||
"OpenRouter model sync: upsert_provider error provider={} {:?}",
|
|
||||||
provider_slug, e
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -1027,13 +973,10 @@ impl AppService {
|
|||||||
(m, false)
|
(m, false)
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
log,
|
model = %or_model.id,
|
||||||
"{}",
|
error = ?e,
|
||||||
format!(
|
"OpenRouter model sync: upsert_model error"
|
||||||
"OpenRouter model sync: upsert_model error model={} {:?}",
|
|
||||||
or_model.id, e
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -1043,13 +986,10 @@ impl AppService {
|
|||||||
match upsert_version(db, model_record.id).await {
|
match upsert_version(db, model_record.id).await {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(
|
||||||
log,
|
model = %or_model.id,
|
||||||
"{}",
|
error = ?e,
|
||||||
format!(
|
"OpenRouter model sync: upsert_version error"
|
||||||
"OpenRouter model sync: upsert_version error model={} {:?}",
|
|
||||||
or_model.id, e
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -1078,20 +1018,15 @@ impl AppService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
slog::info!(
|
tracing::info!(
|
||||||
log,
|
matched = filtered_count,
|
||||||
"{}",
|
|
||||||
format!(
|
|
||||||
"OpenRouter model sync complete: matched={} created={} updated={} \
|
|
||||||
versions={} pricing={} capabilities={} profiles={}",
|
|
||||||
filtered_count,
|
|
||||||
models_created,
|
models_created,
|
||||||
models_updated,
|
models_updated,
|
||||||
versions_created,
|
versions_created,
|
||||||
pricing_created,
|
pricing_created,
|
||||||
capabilities_created,
|
capabilities_created,
|
||||||
profiles_created
|
profiles_created,
|
||||||
)
|
"OpenRouter model sync complete"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -58,7 +58,7 @@ impl AppService {
|
|||||||
.ok_or(AppError::CaptchaError)?;
|
.ok_or(AppError::CaptchaError)?;
|
||||||
if text.to_lowercase() != captcha.to_lowercase() {
|
if text.to_lowercase() != captcha.to_lowercase() {
|
||||||
context.remove(Self::CAPTCHA_KEY);
|
context.remove(Self::CAPTCHA_KEY);
|
||||||
slog::warn!(self.logs, "Captcha verification failed"; "ip" => context.ip_address());
|
tracing::warn!(ip = ?context.ip_address(), "Captcha verification failed");
|
||||||
return Err(AppError::CaptchaError);
|
return Err(AppError::CaptchaError);
|
||||||
}
|
}
|
||||||
context.remove(Self::CAPTCHA_KEY);
|
context.remove(Self::CAPTCHA_KEY);
|
||||||
|
|||||||
@ -125,7 +125,7 @@ impl AppService {
|
|||||||
.await
|
.await
|
||||||
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
.map_err(|e| AppError::InternalServerError(e.to_string()))?;
|
||||||
|
|
||||||
slog::info!(self.logs, "Email change verification queued"; "new_email" => %params.new_email, "user_uid" => %user_uid);
|
tracing::info!(new_email = %params.new_email, user_uid = %user_uid, "Email change verification queued");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +179,7 @@ impl AppService {
|
|||||||
.await
|
.await
|
||||||
.map_err(|e| AppError::DatabaseError(e.to_string()))?;
|
.map_err(|e| AppError::DatabaseError(e.to_string()))?;
|
||||||
|
|
||||||
slog::info!(self.logs, "Email changed successfully"; "new_email" => %new_email, "user_uid" => %user_uid);
|
tracing::info!(new_email = %new_email, user_uid = %user_uid, "Email changed successfully");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -43,7 +43,7 @@ impl AppService {
|
|||||||
PasswordHash::new(&user_password.password_hash).map_err(|_| AppError::UserNotFound)?;
|
PasswordHash::new(&user_password.password_hash).map_err(|_| AppError::UserNotFound)?;
|
||||||
|
|
||||||
if let Err(_e) = Argon2::default().verify_password(password.as_bytes(), &password_hash) {
|
if let Err(_e) = Argon2::default().verify_password(password.as_bytes(), &password_hash) {
|
||||||
slog::warn!(self.logs, "Login failed: invalid password"; "username" => ¶ms.username, "ip" => context.ip_address());
|
tracing::warn!(username = %params.username, ip = ?context.ip_address(), "Login failed: invalid password");
|
||||||
return Err(AppError::UserNotFound);
|
return Err(AppError::UserNotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ impl AppService {
|
|||||||
// if needs_totp_verification {
|
// if needs_totp_verification {
|
||||||
// if let Some(ref totp_code) = params.totp_code {
|
// if let Some(ref totp_code) = params.totp_code {
|
||||||
// if !self.auth_2fa_verify_login(&context, totp_code).await? {
|
// if !self.auth_2fa_verify_login(&context, totp_code).await? {
|
||||||
// slog::warn!(self.logs, "Login failed: invalid 2FA code"; "username" => ¶ms.username, "ip" => context.ip_address());
|
// tracing::warn!(username = %params.username, ip = %context.ip_address(), "Login failed: invalid 2FA code");
|
||||||
// return Err(AppError::InvalidTwoFactorCode);
|
// return Err(AppError::InvalidTwoFactorCode);
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
@ -87,7 +87,7 @@ impl AppService {
|
|||||||
// .await
|
// .await
|
||||||
// .ok();
|
// .ok();
|
||||||
// }
|
// }
|
||||||
// slog::info!(self.logs, "Login 2FA triggered for new 2FA user"; "username" => ¶ms.username, "ip" => context.ip_address());
|
// tracing::info!(username = %params.username, ip = %context.ip_address(), "Login 2FA triggered for new 2FA user");
|
||||||
// return Err(AppError::TwoFactorRequired);
|
// return Err(AppError::TwoFactorRequired);
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
@ -117,7 +117,7 @@ impl AppService {
|
|||||||
context.set_user(user.uid);
|
context.set_user(user.uid);
|
||||||
context.remove(Self::RSA_PRIVATE_KEY);
|
context.remove(Self::RSA_PRIVATE_KEY);
|
||||||
context.remove(Self::RSA_PUBLIC_KEY);
|
context.remove(Self::RSA_PUBLIC_KEY);
|
||||||
slog::info!(self.logs, "User logged in successfully"; "user_uid" => %user.uid, "username" => &user.username, "ip" => context.ip_address());
|
tracing::info!(user_uid = %user.uid, username = %user.username, ip = ?context.ip_address(), "User logged in successfully");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,7 @@ use session::Session;
|
|||||||
impl AppService {
|
impl AppService {
|
||||||
pub async fn auth_logout(&self, context: &Session) -> Result<(), AppError> {
|
pub async fn auth_logout(&self, context: &Session) -> Result<(), AppError> {
|
||||||
if let Some(user_uid) = context.user() {
|
if let Some(user_uid) = context.user() {
|
||||||
slog::info!(self.logs, "User logged out"; "user_uid" => %user_uid, "ip" => context.ip_address());
|
tracing::info!(user_uid = %user_uid, ip = ?context.ip_address(), "User logged out");
|
||||||
let _ = user_activity_log::ActiveModel {
|
let _ = user_activity_log::ActiveModel {
|
||||||
user_uid: Set(Option::from(user_uid)),
|
user_uid: Set(Option::from(user_uid)),
|
||||||
action: Set("logout".to_string()),
|
action: Set("logout".to_string()),
|
||||||
|
|||||||
@ -69,7 +69,7 @@ impl AppService {
|
|||||||
.await
|
.await
|
||||||
.map_err(|_| AppError::UserNotFound)?;
|
.map_err(|_| AppError::UserNotFound)?;
|
||||||
|
|
||||||
slog::info!(self.logs, "Password changed"; "user_uid" => %user_uid, "ip" => context.ip_address());
|
tracing::info!(user_uid = %user_uid, ip = ?context.ip_address(), "Password changed");
|
||||||
let _ = user_activity_log::ActiveModel {
|
let _ = user_activity_log::ActiveModel {
|
||||||
user_uid: Set(Option::from(user_uid)),
|
user_uid: Set(Option::from(user_uid)),
|
||||||
action: Set("password_change".to_string()),
|
action: Set("password_change".to_string()),
|
||||||
@ -146,7 +146,7 @@ impl AppService {
|
|||||||
.await
|
.await
|
||||||
.map_err(|_| AppError::UserNotFound)?;
|
.map_err(|_| AppError::UserNotFound)?;
|
||||||
|
|
||||||
slog::info!(self.logs, "Password reset email queued"; "email" => email_address);
|
tracing::info!(email = %email_address, "Password reset email queued");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn validate_password_strength(password: &str) -> Result<(), AppError> {
|
pub fn validate_password_strength(password: &str) -> Result<(), AppError> {
|
||||||
@ -232,7 +232,7 @@ impl AppService {
|
|||||||
|
|
||||||
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
||||||
|
|
||||||
slog::info!(self.logs, "Password reset confirmed"; "user_uid" => %reset_token.user_uid);
|
tracing::info!(user_uid = %reset_token.user_uid, "Password reset confirmed");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,7 +245,7 @@ impl AppService {
|
|||||||
.await
|
.await
|
||||||
.map_err(|_| AppError::UserNotFound)?;
|
.map_err(|_| AppError::UserNotFound)?;
|
||||||
|
|
||||||
slog::info!(self.logs, "Expired password reset tokens cleaned up"; "count" => result.rows_affected);
|
tracing::info!(count = result.rows_affected, "Expired password reset tokens cleaned up");
|
||||||
Ok(result.rows_affected)
|
Ok(result.rows_affected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,7 +30,7 @@ impl AppService {
|
|||||||
.await
|
.await
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
slog::warn!(self.logs, "Registration failed: username already exists"; "username" => ¶ms.username);
|
tracing::warn!(username = %params.username, "Registration failed: username already exists");
|
||||||
return Err(AppError::UserNameExists);
|
return Err(AppError::UserNameExists);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ impl AppService {
|
|||||||
.await
|
.await
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
slog::warn!(self.logs, "Registration failed: email already exists"; "email" => ¶ms.email);
|
tracing::warn!(email = %params.email, "Registration failed: email already exists");
|
||||||
return Err(AppError::EmailExists);
|
return Err(AppError::EmailExists);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +60,7 @@ impl AppService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let user = user_model.insert(&txn).await.map_err(|e| {
|
let user = user_model.insert(&txn).await.map_err(|e| {
|
||||||
slog::error!(self.logs, "{}", format!("Failed to insert user: {:?}", e));
|
tracing::error!(error = ?e, "Failed to insert user");
|
||||||
AppError::UserNotFound
|
AppError::UserNotFound
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@ -71,11 +71,7 @@ impl AppService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
user_email_model.insert(&txn).await.map_err(|e| {
|
user_email_model.insert(&txn).await.map_err(|e| {
|
||||||
slog::error!(
|
tracing::error!(error = ?e, "Failed to insert user email");
|
||||||
self.logs,
|
|
||||||
"{}",
|
|
||||||
format!("Failed to insert user email: {:?}", e)
|
|
||||||
);
|
|
||||||
AppError::UserNotFound
|
AppError::UserNotFound
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@ -83,7 +79,7 @@ impl AppService {
|
|||||||
let password_hash = Argon2::default()
|
let password_hash = Argon2::default()
|
||||||
.hash_password(password.as_bytes(), Salt::from_b64(&*salt.to_string())?)
|
.hash_password(password.as_bytes(), Salt::from_b64(&*salt.to_string())?)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
slog::error!(self.logs, "{}", format!("Failed to hash password: {:?}", e));
|
tracing::error!(error = ?e, "Failed to hash password");
|
||||||
AppError::UserNotFound
|
AppError::UserNotFound
|
||||||
})?
|
})?
|
||||||
.to_string();
|
.to_string();
|
||||||
@ -97,11 +93,7 @@ impl AppService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
user_password_model.insert(&txn).await.map_err(|e| {
|
user_password_model.insert(&txn).await.map_err(|e| {
|
||||||
slog::error!(
|
tracing::error!(error = ?e, "Failed to insert user password");
|
||||||
self.logs,
|
|
||||||
"{}",
|
|
||||||
format!("Failed to insert user password: {:?}", e)
|
|
||||||
);
|
|
||||||
AppError::UserNotFound
|
AppError::UserNotFound
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@ -139,11 +131,7 @@ impl AppService {
|
|||||||
updated_at: Set(now),
|
updated_at: Set(now),
|
||||||
};
|
};
|
||||||
let ws = ws.insert(&txn).await.map_err(|e| {
|
let ws = ws.insert(&txn).await.map_err(|e| {
|
||||||
slog::error!(
|
tracing::error!(error = ?e, "Failed to insert personal workspace");
|
||||||
self.logs,
|
|
||||||
"{}",
|
|
||||||
format!("Failed to insert personal workspace: {:?}", e)
|
|
||||||
);
|
|
||||||
AppError::UserNotFound
|
AppError::UserNotFound
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
@ -165,7 +153,7 @@ impl AppService {
|
|||||||
context.set_current_workspace_id(ws.id);
|
context.set_current_workspace_id(ws.id);
|
||||||
context.remove(Self::RSA_PRIVATE_KEY);
|
context.remove(Self::RSA_PRIVATE_KEY);
|
||||||
context.remove(Self::RSA_PUBLIC_KEY);
|
context.remove(Self::RSA_PUBLIC_KEY);
|
||||||
slog::info!(self.logs, "User registered successfully"; "user_uid" => %user_uid, "username" => &user.username);
|
tracing::info!(user_uid = %user_uid, username = %user.username, "User registered successfully");
|
||||||
Ok(user)
|
Ok(user)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,7 +19,7 @@ impl AppService {
|
|||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
let mut rng = rsa::rand_core::OsRng::default();
|
let mut rng = rsa::rand_core::OsRng::default();
|
||||||
let Ok(priv_key) = RsaPrivateKey::new(&mut rng, Self::RSA_BIT_SIZE) else {
|
let Ok(priv_key) = RsaPrivateKey::new(&mut rng, Self::RSA_BIT_SIZE) else {
|
||||||
slog::error!(self.logs, "RSA key generation failed");
|
tracing::error!("RSA key generation failed");
|
||||||
return Err(AppError::RsaGenerationError);
|
return Err(AppError::RsaGenerationError);
|
||||||
};
|
};
|
||||||
let pub_key = RsaPublicKey::from(&priv_key);
|
let pub_key = RsaPublicKey::from(&priv_key);
|
||||||
@ -51,14 +51,14 @@ impl AppService {
|
|||||||
.map_err(|_| AppError::RsaDecodeError)?
|
.map_err(|_| AppError::RsaDecodeError)?
|
||||||
.ok_or(AppError::RsaDecodeError)?;
|
.ok_or(AppError::RsaDecodeError)?;
|
||||||
let Ok(priv_key) = RsaPrivateKey::from_pkcs1_pem(&priv_key) else {
|
let Ok(priv_key) = RsaPrivateKey::from_pkcs1_pem(&priv_key) else {
|
||||||
slog::warn!(self.logs, "RSA decode failed: invalid private key"; "ip" => context.ip_address());
|
tracing::warn!(ip = ?context.ip_address(), "RSA decode failed: invalid private key");
|
||||||
return Err(AppError::RsaDecodeError);
|
return Err(AppError::RsaDecodeError);
|
||||||
};
|
};
|
||||||
let cipher = base64::engine::general_purpose::STANDARD
|
let cipher = base64::engine::general_purpose::STANDARD
|
||||||
.decode(&data)
|
.decode(&data)
|
||||||
.map_err(|_| AppError::RsaDecodeError)?;
|
.map_err(|_| AppError::RsaDecodeError)?;
|
||||||
let Ok(decrypted) = priv_key.decrypt(Pkcs1v15Encrypt, &cipher) else {
|
let Ok(decrypted) = priv_key.decrypt(Pkcs1v15Encrypt, &cipher) else {
|
||||||
slog::warn!(self.logs, "RSA decrypt failed"; "ip" => context.ip_address());
|
tracing::warn!(ip = ?context.ip_address(), "RSA decrypt failed");
|
||||||
return Err(AppError::RsaDecodeError);
|
return Err(AppError::RsaDecodeError);
|
||||||
};
|
};
|
||||||
Ok(String::from_utf8_lossy(&decrypted).to_string())
|
Ok(String::from_utf8_lossy(&decrypted).to_string())
|
||||||
|
|||||||
@ -75,7 +75,7 @@ impl AppService {
|
|||||||
model.insert(&self.db).await?;
|
model.insert(&self.db).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
slog::info!(self.logs, "2FA setup initiated"; "user_uid" => %user_uid);
|
tracing::info!(user_uid = %user_uid, "2FA setup initiated");
|
||||||
Ok(Enable2FAResponse {
|
Ok(Enable2FAResponse {
|
||||||
secret,
|
secret,
|
||||||
qr_code: qr_data,
|
qr_code: qr_data,
|
||||||
@ -102,7 +102,7 @@ impl AppService {
|
|||||||
let secret = two_fa.secret.as_ref().ok_or(AppError::TwoFactorNotSetup)?;
|
let secret = two_fa.secret.as_ref().ok_or(AppError::TwoFactorNotSetup)?;
|
||||||
|
|
||||||
if !self.verify_totp_code(secret, ¶ms.code)? {
|
if !self.verify_totp_code(secret, ¶ms.code)? {
|
||||||
slog::warn!(self.logs, "2FA verification failed during setup"; "user_uid" => %user_uid, "ip" => context.ip_address());
|
tracing::warn!(user_uid = %user_uid, ip = ?context.ip_address(), "2FA verification failed during setup");
|
||||||
return Err(AppError::InvalidTwoFactorCode);
|
return Err(AppError::InvalidTwoFactorCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,7 +111,7 @@ impl AppService {
|
|||||||
active_model.updated_at = Set(chrono::Utc::now());
|
active_model.updated_at = Set(chrono::Utc::now());
|
||||||
active_model.update(&self.db).await?;
|
active_model.update(&self.db).await?;
|
||||||
|
|
||||||
slog::info!(self.logs, "2FA enabled"; "user_uid" => %user_uid, "ip" => context.ip_address());
|
tracing::info!(user_uid = %user_uid, ip = ?context.ip_address(), "2FA enabled");
|
||||||
let _ = user_activity_log::ActiveModel {
|
let _ = user_activity_log::ActiveModel {
|
||||||
user_uid: Set(Option::from(user_uid)),
|
user_uid: Set(Option::from(user_uid)),
|
||||||
action: Set("2fa_enabled".to_string()),
|
action: Set("2fa_enabled".to_string()),
|
||||||
@ -162,7 +162,7 @@ impl AppService {
|
|||||||
.exec(&self.db)
|
.exec(&self.db)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
slog::info!(self.logs, "2FA disabled"; "user_uid" => %user_uid, "ip" => context.ip_address());
|
tracing::info!(user_uid = %user_uid, ip = ?context.ip_address(), "2FA disabled");
|
||||||
let _ = user_activity_log::ActiveModel {
|
let _ = user_activity_log::ActiveModel {
|
||||||
user_uid: Set(Some(user_uid)),
|
user_uid: Set(Some(user_uid)),
|
||||||
action: Set("2fa_disabled".to_string()),
|
action: Set("2fa_disabled".to_string()),
|
||||||
@ -273,10 +273,10 @@ impl AppService {
|
|||||||
let secret = two_fa.secret.as_ref().ok_or(AppError::TwoFactorNotSetup)?;
|
let secret = two_fa.secret.as_ref().ok_or(AppError::TwoFactorNotSetup)?;
|
||||||
if self.verify_totp_code(secret, code)? {
|
if self.verify_totp_code(secret, code)? {
|
||||||
let _: Option<()> = conn.del(totp_key.as_str()).await.ok();
|
let _: Option<()> = conn.del(totp_key.as_str()).await.ok();
|
||||||
slog::info!(self.logs, "2FA verification succeeded during login"; "user_uid" => %user_uid, "ip" => context.ip_address());
|
tracing::info!(user_uid = %user_uid, ip = ?context.ip_address(), "2FA verification succeeded during login");
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
} else {
|
} else {
|
||||||
slog::warn!(self.logs, "2FA verification failed during login"; "user_uid" => %user_uid, "ip" => context.ip_address());
|
tracing::warn!(user_uid = %user_uid, ip = ?context.ip_address(), "2FA verification failed during login");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -324,7 +324,7 @@ impl AppService {
|
|||||||
.insert(&self.db)
|
.insert(&self.db)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
slog::info!(self.logs, "2FA backup codes regenerated"; "user_uid" => %user_uid, "ip" => context.ip_address());
|
tracing::info!(user_uid = %user_uid, ip = ?context.ip_address(), "2FA backup codes regenerated");
|
||||||
Ok(backup_codes)
|
Ok(backup_codes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -167,7 +167,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(response)
|
Ok(response)
|
||||||
|
|||||||
@ -144,7 +144,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -210,7 +210,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -427,7 +427,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -567,11 +567,7 @@ impl AppService {
|
|||||||
Ok(Some(r)) => r.project,
|
Ok(Some(r)) => r.project,
|
||||||
Ok(None) => Uuid::nil(),
|
Ok(None) => Uuid::nil(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "failed to look up project_id for activity log");
|
||||||
self.logs,
|
|
||||||
"failed to look up project_id for activity log: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
Uuid::nil()
|
Uuid::nil()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -618,11 +614,7 @@ impl AppService {
|
|||||||
Ok(Some(r)) => r.project,
|
Ok(Some(r)) => r.project,
|
||||||
Ok(None) => Uuid::nil(),
|
Ok(None) => Uuid::nil(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "failed to look up project_id for activity log");
|
||||||
self.logs,
|
|
||||||
"failed to look up project_id for activity log: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
Uuid::nil()
|
Uuid::nil()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -698,11 +690,7 @@ impl AppService {
|
|||||||
Ok(Some(r)) => r.project,
|
Ok(Some(r)) => r.project,
|
||||||
Ok(None) => Uuid::nil(),
|
Ok(None) => Uuid::nil(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "failed to look up project_id for activity log");
|
||||||
self.logs,
|
|
||||||
"failed to look up project_id for activity log: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
Uuid::nil()
|
Uuid::nil()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -762,11 +750,7 @@ impl AppService {
|
|||||||
Ok(Some(r)) => r.project,
|
Ok(Some(r)) => r.project,
|
||||||
Ok(None) => Uuid::nil(),
|
Ok(None) => Uuid::nil(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "failed to look up project_id for activity log");
|
||||||
self.logs,
|
|
||||||
"failed to look up project_id for activity log: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
Uuid::nil()
|
Uuid::nil()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -510,7 +510,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -882,7 +882,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1097,7 +1097,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1274,7 +1274,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1350,7 +1350,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1400,7 +1400,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1450,7 +1450,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -129,7 +129,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -341,7 +341,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -390,7 +390,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -442,7 +442,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -623,7 +623,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -151,7 +151,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,7 +204,7 @@ impl AppService {
|
|||||||
if let Ok(mut conn) = self.cache.conn().await {
|
if let Ok(mut conn) = self.cache.conn().await {
|
||||||
let key = format!("git:ref:list:{}:{}:*", namespace, repo_name);
|
let key = format!("git:ref:list:{}:{}:*", namespace, repo_name);
|
||||||
if let Err(e) = conn.del::<String, ()>(key).await {
|
if let Err(e) = conn.del::<String, ()>(key).await {
|
||||||
slog::debug!(self.logs, "cache del failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache del failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,7 +237,7 @@ impl AppService {
|
|||||||
if let Ok(mut conn) = self.cache.conn().await {
|
if let Ok(mut conn) = self.cache.conn().await {
|
||||||
let key = format!("git:ref:list:{}:{}:*", namespace, repo_name);
|
let key = format!("git:ref:list:{}:{}:*", namespace, repo_name);
|
||||||
if let Err(e) = conn.del::<String, ()>(key).await {
|
if let Err(e) = conn.del::<String, ()>(key).await {
|
||||||
slog::debug!(self.logs, "cache del failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache del failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,7 +272,7 @@ impl AppService {
|
|||||||
if let Ok(mut conn) = self.cache.conn().await {
|
if let Ok(mut conn) = self.cache.conn().await {
|
||||||
let key = format!("git:ref:list:{}:{}:*", namespace, repo_name);
|
let key = format!("git:ref:list:{}:{}:*", namespace, repo_name);
|
||||||
if let Err(e) = conn.del::<String, ()>(key).await {
|
if let Err(e) = conn.del::<String, ()>(key).await {
|
||||||
slog::debug!(self.logs, "cache del failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache del failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -55,11 +55,7 @@ impl AppService {
|
|||||||
Ok(Some(r)) => r.project,
|
Ok(Some(r)) => r.project,
|
||||||
Ok(None) => Uuid::nil(),
|
Ok(None) => Uuid::nil(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "failed to look up project_id for activity log");
|
||||||
self.logs,
|
|
||||||
"failed to look up project_id for activity log: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
Uuid::nil()
|
Uuid::nil()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -107,11 +103,7 @@ impl AppService {
|
|||||||
Ok(Some(r)) => r.project,
|
Ok(Some(r)) => r.project,
|
||||||
Ok(None) => Uuid::nil(),
|
Ok(None) => Uuid::nil(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "failed to look up project_id for activity log");
|
||||||
self.logs,
|
|
||||||
"failed to look up project_id for activity log: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
Uuid::nil()
|
Uuid::nil()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -161,7 +161,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(tags)
|
Ok(tags)
|
||||||
@ -263,7 +263,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(response)
|
Ok(response)
|
||||||
@ -407,18 +407,14 @@ impl AppService {
|
|||||||
if let Ok(mut conn) = self.cache.conn().await {
|
if let Ok(mut conn) = self.cache.conn().await {
|
||||||
let key = format!("git:tag:list:{}:{}", namespace, repo_name);
|
let key = format!("git:tag:list:{}:{}", namespace, repo_name);
|
||||||
if let Err(e) = conn.del::<String, ()>(key).await {
|
if let Err(e) = conn.del::<String, ()>(key).await {
|
||||||
slog::debug!(self.logs, "cache del failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache del failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let project_id = match repo_model::Entity::find_by_id(repo_id).one(&self.db).await {
|
let project_id = match repo_model::Entity::find_by_id(repo_id).one(&self.db).await {
|
||||||
Ok(Some(r)) => r.project,
|
Ok(Some(r)) => r.project,
|
||||||
Ok(None) => Uuid::nil(),
|
Ok(None) => Uuid::nil(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "failed to look up project_id for activity log");
|
||||||
self.logs,
|
|
||||||
"failed to look up project_id for activity log: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
Uuid::nil()
|
Uuid::nil()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -459,18 +455,14 @@ impl AppService {
|
|||||||
if let Ok(mut conn) = self.cache.conn().await {
|
if let Ok(mut conn) = self.cache.conn().await {
|
||||||
let key = format!("git:tag:list:{}:{}", namespace, repo_name);
|
let key = format!("git:tag:list:{}:{}", namespace, repo_name);
|
||||||
if let Err(e) = conn.del::<String, ()>(key).await {
|
if let Err(e) = conn.del::<String, ()>(key).await {
|
||||||
slog::debug!(self.logs, "cache del failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache del failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let project_id = match repo_model::Entity::find_by_id(repo_id).one(&self.db).await {
|
let project_id = match repo_model::Entity::find_by_id(repo_id).one(&self.db).await {
|
||||||
Ok(Some(r)) => r.project,
|
Ok(Some(r)) => r.project,
|
||||||
Ok(None) => Uuid::nil(),
|
Ok(None) => Uuid::nil(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "failed to look up project_id for activity log");
|
||||||
self.logs,
|
|
||||||
"failed to look up project_id for activity log: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
Uuid::nil()
|
Uuid::nil()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -511,21 +503,17 @@ impl AppService {
|
|||||||
let list_key = format!("git:tag:list:{}:{}", namespace, repo_name);
|
let list_key = format!("git:tag:list:{}:{}", namespace, repo_name);
|
||||||
let get_key = format!("git:tag:get:{}:{}:{}", namespace, repo_name, query.name);
|
let get_key = format!("git:tag:get:{}:{}:{}", namespace, repo_name, query.name);
|
||||||
if let Err(e) = conn.del::<String, ()>(list_key).await {
|
if let Err(e) = conn.del::<String, ()>(list_key).await {
|
||||||
slog::debug!(self.logs, "cache del failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache del failed (non-fatal)");
|
||||||
}
|
}
|
||||||
if let Err(e) = conn.del::<String, ()>(get_key).await {
|
if let Err(e) = conn.del::<String, ()>(get_key).await {
|
||||||
slog::debug!(self.logs, "cache del failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache del failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let project_id = match repo_model::Entity::find_by_id(repo_id).one(&self.db).await {
|
let project_id = match repo_model::Entity::find_by_id(repo_id).one(&self.db).await {
|
||||||
Ok(Some(r)) => r.project,
|
Ok(Some(r)) => r.project,
|
||||||
Ok(None) => Uuid::nil(),
|
Ok(None) => Uuid::nil(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "failed to look up project_id for activity log");
|
||||||
self.logs,
|
|
||||||
"failed to look up project_id for activity log: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
Uuid::nil()
|
Uuid::nil()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -567,13 +555,13 @@ impl AppService {
|
|||||||
let old_key = format!("git:tag:get:{}:{}:{}", namespace, repo_name, query.old_name);
|
let old_key = format!("git:tag:get:{}:{}:{}", namespace, repo_name, query.old_name);
|
||||||
let new_key = format!("git:tag:get:{}:{}:{}", namespace, repo_name, query.new_name);
|
let new_key = format!("git:tag:get:{}:{}:{}", namespace, repo_name, query.new_name);
|
||||||
if let Err(e) = conn.del::<String, ()>(list_key).await {
|
if let Err(e) = conn.del::<String, ()>(list_key).await {
|
||||||
slog::debug!(self.logs, "cache del failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache del failed (non-fatal)");
|
||||||
}
|
}
|
||||||
if let Err(e) = conn.del::<String, ()>(old_key).await {
|
if let Err(e) = conn.del::<String, ()>(old_key).await {
|
||||||
slog::debug!(self.logs, "cache del failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache del failed (non-fatal)");
|
||||||
}
|
}
|
||||||
if let Err(e) = conn.del::<String, ()>(new_key).await {
|
if let Err(e) = conn.del::<String, ()>(new_key).await {
|
||||||
slog::debug!(self.logs, "cache del failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache del failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let response = TagInfoResponse::from(info);
|
let response = TagInfoResponse::from(info);
|
||||||
@ -581,11 +569,7 @@ impl AppService {
|
|||||||
Ok(Some(r)) => r.project,
|
Ok(Some(r)) => r.project,
|
||||||
Ok(None) => Uuid::nil(),
|
Ok(None) => Uuid::nil(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "failed to look up project_id for activity log");
|
||||||
self.logs,
|
|
||||||
"failed to look up project_id for activity log: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
Uuid::nil()
|
Uuid::nil()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -637,10 +621,10 @@ impl AppService {
|
|||||||
let list_key = format!("git:tag:list:{}:{}", namespace, repo_name);
|
let list_key = format!("git:tag:list:{}:{}", namespace, repo_name);
|
||||||
let get_key = format!("git:tag:get:{}:{}:{}", namespace, repo_name, request.name);
|
let get_key = format!("git:tag:get:{}:{}:{}", namespace, repo_name, request.name);
|
||||||
if let Err(e) = conn.del::<String, ()>(list_key).await {
|
if let Err(e) = conn.del::<String, ()>(list_key).await {
|
||||||
slog::debug!(self.logs, "cache del failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache del failed (non-fatal)");
|
||||||
}
|
}
|
||||||
if let Err(e) = conn.del::<String, ()>(get_key).await {
|
if let Err(e) = conn.del::<String, ()>(get_key).await {
|
||||||
slog::debug!(self.logs, "cache del failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache del failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(TagInfoResponse::from(info))
|
Ok(TagInfoResponse::from(info))
|
||||||
|
|||||||
@ -193,7 +193,7 @@ impl AppService {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
slog::debug!(self.logs, "cache set failed (non-fatal): {}", e);
|
tracing::debug!(error = ?e, "cache set failed (non-fatal)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -70,11 +70,7 @@ impl AppService {
|
|||||||
Ok(Some(r)) => r.project,
|
Ok(Some(r)) => r.project,
|
||||||
Ok(None) => Uuid::nil(),
|
Ok(None) => Uuid::nil(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "failed to look up project_id for activity log");
|
||||||
self.logs,
|
|
||||||
"failed to look up project_id for activity log: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
Uuid::nil()
|
Uuid::nil()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -120,11 +116,7 @@ impl AppService {
|
|||||||
Ok(Some(r)) => r.project,
|
Ok(Some(r)) => r.project,
|
||||||
Ok(None) => Uuid::nil(),
|
Ok(None) => Uuid::nil(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "failed to look up project_id for activity log");
|
||||||
self.logs,
|
|
||||||
"failed to look up project_id for activity log: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
Uuid::nil()
|
Uuid::nil()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -16,7 +16,6 @@ use queue::{
|
|||||||
use room::metrics::RoomMetrics;
|
use room::metrics::RoomMetrics;
|
||||||
use room::RoomService;
|
use room::RoomService;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use observability::build_logger;
|
|
||||||
use utoipa::ToSchema;
|
use utoipa::ToSchema;
|
||||||
use ws_token::WsTokenService;
|
use ws_token::WsTokenService;
|
||||||
|
|
||||||
@ -31,7 +30,6 @@ pub struct AppService {
|
|||||||
pub config: AppConfig,
|
pub config: AppConfig,
|
||||||
pub cache: AppCache,
|
pub cache: AppCache,
|
||||||
pub email: AppEmail,
|
pub email: AppEmail,
|
||||||
pub logs: slog::Logger,
|
|
||||||
pub avatar: AppAvatar,
|
pub avatar: AppAvatar,
|
||||||
pub room: RoomService,
|
pub room: RoomService,
|
||||||
pub ws_token: Arc<WsTokenService>,
|
pub ws_token: Arc<WsTokenService>,
|
||||||
@ -47,7 +45,6 @@ impl AppService {
|
|||||||
pub fn send_push_to_user(&self, user_id: uuid::Uuid, payload: PushPayload) {
|
pub fn send_push_to_user(&self, user_id: uuid::Uuid, payload: PushPayload) {
|
||||||
let push = self.push.clone();
|
let push = self.push.clone();
|
||||||
let db = self.db.clone();
|
let db = self.db.clone();
|
||||||
let log = self.logs.clone();
|
|
||||||
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
if let Some(push) = push {
|
if let Some(push) = push {
|
||||||
@ -69,11 +66,11 @@ impl AppService {
|
|||||||
let auth = prefs.push_subscription_keys_auth.unwrap();
|
let auth = prefs.push_subscription_keys_auth.unwrap();
|
||||||
|
|
||||||
if let Err(e) = push.send(&endpoint, &p256dh, &auth, &payload).await {
|
if let Err(e) = push.send(&endpoint, &p256dh, &auth, &payload).await {
|
||||||
slog::warn!(log, "WebPush send failed"; "user_id" => %user_id, "error" => %e);
|
tracing::warn!(user_id = %user_id, error = %e, "WebPush send failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if let Err(e) = prefs {
|
} else if let Err(e) = prefs {
|
||||||
slog::warn!(log, "Failed to read push subscription"; "user_id" => %user_id, "error" => %e);
|
tracing::warn!(user_id = %user_id, error = %e, "Failed to read push subscription");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -84,27 +81,23 @@ impl AppService {
|
|||||||
pub async fn start_room_workers(
|
pub async fn start_room_workers(
|
||||||
&self,
|
&self,
|
||||||
shutdown_rx: tokio::sync::broadcast::Receiver<()>,
|
shutdown_rx: tokio::sync::broadcast::Receiver<()>,
|
||||||
log: slog::Logger,
|
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
self.room.start_workers(shutdown_rx, log).await
|
self.room.start_workers(shutdown_rx).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn new(config: AppConfig) -> anyhow::Result<Self> {
|
pub async fn new(config: AppConfig) -> anyhow::Result<Self> {
|
||||||
let db = AppDatabase::init(&config).await?;
|
let db = AppDatabase::init(&config).await?;
|
||||||
let cache = AppCache::init(&config).await?;
|
let cache = AppCache::init(&config).await?;
|
||||||
|
|
||||||
let log_level = config.log_level().unwrap_or_else(|_| "info".to_string());
|
let email = AppEmail::init(&config).await?;
|
||||||
let logs = build_logger(&log_level);
|
|
||||||
|
|
||||||
let email = AppEmail::init(&config, logs.clone()).await?;
|
|
||||||
let avatar = AppAvatar::init(&config).await?;
|
let avatar = AppAvatar::init(&config).await?;
|
||||||
let storage = match AppStorage::new(&config) {
|
let storage = match AppStorage::new(&config) {
|
||||||
Ok(s) => {
|
Ok(s) => {
|
||||||
slog::info!(logs, "Storage initialized at {}", s.base_path.display());
|
tracing::info!(path = %s.base_path.display(), "Storage initialized");
|
||||||
Some(s)
|
Some(s)
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(logs, "Storage not available: {}", e);
|
tracing::warn!(error = %e, "Storage not available");
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -120,17 +113,17 @@ impl AppService {
|
|||||||
config.vapid_sender_email(),
|
config.vapid_sender_email(),
|
||||||
) {
|
) {
|
||||||
Ok(s) => {
|
Ok(s) => {
|
||||||
slog::info!(logs, "WebPush initialized");
|
tracing::info!("WebPush initialized");
|
||||||
Some(s)
|
Some(s)
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(logs, "WebPush not available: {}", e);
|
tracing::warn!(error = %e, "WebPush not available");
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
slog::warn!(logs, "WebPush disabled — VAPID keys not configured");
|
tracing::warn!("WebPush disabled — VAPID keys not configured");
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -150,11 +143,10 @@ impl AppService {
|
|||||||
|
|
||||||
let redis_pubsub = Some(RedisPubSub {
|
let redis_pubsub = Some(RedisPubSub {
|
||||||
get_redis: get_redis.clone(),
|
get_redis: get_redis.clone(),
|
||||||
log: logs.clone(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let message_producer =
|
let message_producer =
|
||||||
MessageProducer::new(get_redis.clone(), redis_pubsub.clone(), 10000, logs.clone());
|
MessageProducer::new(get_redis.clone(), redis_pubsub.clone(), 10000);
|
||||||
|
|
||||||
// Build RoomService
|
// Build RoomService
|
||||||
let task_service = Arc::new(TaskService::new(db.clone()));
|
let task_service = Arc::new(TaskService::new(db.clone()));
|
||||||
@ -173,7 +165,7 @@ impl AppService {
|
|||||||
let chat_service: Option<Arc<ChatService>> =
|
let chat_service: Option<Arc<ChatService>> =
|
||||||
match (config.ai_api_key(), config.ai_basic_url()) {
|
match (config.ai_api_key(), config.ai_basic_url()) {
|
||||||
(Ok(api_key), Ok(base_url)) => {
|
(Ok(api_key), Ok(base_url)) => {
|
||||||
slog::info!(logs, "AI chat enabled — connecting to {}", base_url);
|
tracing::info!(url = %base_url, "AI chat enabled");
|
||||||
let cfg = OpenAIConfig::new()
|
let cfg = OpenAIConfig::new()
|
||||||
.with_api_key(&api_key)
|
.with_api_key(&api_key)
|
||||||
.with_api_base(&base_url);
|
.with_api_base(&base_url);
|
||||||
@ -187,11 +179,11 @@ impl AppService {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
(Err(e), _) => {
|
(Err(e), _) => {
|
||||||
slog::warn!(logs, "AI chat disabled — {}", e);
|
tracing::warn!(error = %e, "AI chat disabled");
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
(_, Err(e)) => {
|
(_, Err(e)) => {
|
||||||
slog::warn!(logs, "AI chat disabled — {}", e);
|
tracing::warn!(error = %e, "AI chat disabled");
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -199,11 +191,9 @@ impl AppService {
|
|||||||
// Build push notification callback for RoomService
|
// Build push notification callback for RoomService
|
||||||
let push_fn: Option<room::PushNotificationFn> = push.clone().map(|push_svc| {
|
let push_fn: Option<room::PushNotificationFn> = push.clone().map(|push_svc| {
|
||||||
let db_clone = db.clone();
|
let db_clone = db.clone();
|
||||||
let log_clone = logs.clone();
|
|
||||||
Arc::new(move |user_id: uuid::Uuid, title: String, body: Option<String>, url: Option<String>| {
|
Arc::new(move |user_id: uuid::Uuid, title: String, body: Option<String>, url: Option<String>| {
|
||||||
let push = push_svc.clone();
|
let push = push_svc.clone();
|
||||||
let db = db_clone.clone();
|
let db = db_clone.clone();
|
||||||
let log = log_clone.clone();
|
|
||||||
let payload = PushPayload {
|
let payload = PushPayload {
|
||||||
title,
|
title,
|
||||||
body: body.unwrap_or_default(),
|
body: body.unwrap_or_default(),
|
||||||
@ -229,7 +219,7 @@ impl AppService {
|
|||||||
let auth = prefs.push_subscription_keys_auth.unwrap();
|
let auth = prefs.push_subscription_keys_auth.unwrap();
|
||||||
|
|
||||||
if let Err(e) = push.send(&endpoint, &p256dh, &auth, &payload).await {
|
if let Err(e) = push.send(&endpoint, &p256dh, &auth, &payload).await {
|
||||||
slog::warn!(log, "WebPush send failed"; "user_id" => %user_id, "error" => %e);
|
tracing::warn!(user_id = %user_id, error = %e, "WebPush send failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -246,7 +236,6 @@ impl AppService {
|
|||||||
redis_url,
|
redis_url,
|
||||||
chat_service,
|
chat_service,
|
||||||
Some(task_service.clone()),
|
Some(task_service.clone()),
|
||||||
logs.clone(),
|
|
||||||
None,
|
None,
|
||||||
push_fn,
|
push_fn,
|
||||||
);
|
);
|
||||||
@ -259,7 +248,6 @@ impl AppService {
|
|||||||
config,
|
config,
|
||||||
cache,
|
cache,
|
||||||
email,
|
email,
|
||||||
logs,
|
|
||||||
avatar,
|
avatar,
|
||||||
room,
|
room,
|
||||||
ws_token,
|
ws_token,
|
||||||
@ -280,10 +268,8 @@ impl AppService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let email = self.email.clone();
|
let email = self.email.clone();
|
||||||
let logs = self.logs.clone();
|
|
||||||
let send_fn: EmailSendFn = Arc::new(move |envelopes: Vec<EmailEnvelope>| -> EmailSendFut {
|
let send_fn: EmailSendFn = Arc::new(move |envelopes: Vec<EmailEnvelope>| -> EmailSendFut {
|
||||||
let email = email.clone();
|
let email = email.clone();
|
||||||
let logs = logs.clone();
|
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
for envelope in envelopes {
|
for envelope in envelopes {
|
||||||
let to = envelope.to.clone();
|
let to = envelope.to.clone();
|
||||||
@ -293,15 +279,14 @@ impl AppService {
|
|||||||
body: envelope.body,
|
body: envelope.body,
|
||||||
};
|
};
|
||||||
if let Err(e) = email.send(msg).await {
|
if let Err(e) = email.send(msg).await {
|
||||||
let err = format!("email send failed to:{} error: {}", to, e);
|
tracing::error!(to = %to, error = %e, "email send failed");
|
||||||
slog::error!(logs, "{}", err);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
start_email_worker(get_redis, send_fn, shutdown_rx, self.logs.clone()).await;
|
start_email_worker(get_redis, send_fn, shutdown_rx).await;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -270,11 +270,7 @@ impl AppService {
|
|||||||
Ok(Some(m)) => m.scope_role().ok(),
|
Ok(Some(m)) => m.scope_role().ok(),
|
||||||
Ok(None) => None,
|
Ok(None) => None,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
slog::warn!(
|
tracing::warn!(error = ?e, "failed to look up project member for activity log");
|
||||||
self.logs,
|
|
||||||
"failed to look up project member for activity log: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -297,18 +297,16 @@ impl AppService {
|
|||||||
/// Runs every ALERT_CHECK_INTERVAL seconds.
|
/// Runs every ALERT_CHECK_INTERVAL seconds.
|
||||||
pub fn start_billing_alert_task(self) -> tokio::task::JoinHandle<()> {
|
pub fn start_billing_alert_task(self) -> tokio::task::JoinHandle<()> {
|
||||||
const ALERT_CHECK_INTERVAL: u64 = 30 * 60; // 30 minutes
|
const ALERT_CHECK_INTERVAL: u64 = 30 * 60; // 30 minutes
|
||||||
let logs = self.logs.clone();
|
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let mut tick = interval(Duration::from_secs(ALERT_CHECK_INTERVAL));
|
let mut tick = interval(Duration::from_secs(ALERT_CHECK_INTERVAL));
|
||||||
loop {
|
loop {
|
||||||
tick.tick().await;
|
tick.tick().await;
|
||||||
let result = self.check_billing_alerts().await;
|
let result = self.check_billing_alerts().await;
|
||||||
if result.alerts_sent > 0 {
|
if result.alerts_sent > 0 {
|
||||||
slog::info!(
|
tracing::info!(
|
||||||
logs,
|
workspaces_checked = result.workspaces_checked,
|
||||||
"billing_alerts_sent";
|
alerts_sent = result.alerts_sent,
|
||||||
"workspaces_checked" => result.workspaces_checked,
|
"billing_alerts_sent"
|
||||||
"alerts_sent" => result.alerts_sent
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user