diff options
Diffstat (limited to 'crates/atuin-server/src')
| -rw-r--r-- | crates/atuin-server/src/handlers/history.rs | 8 | ||||
| -rw-r--r-- | crates/atuin-server/src/handlers/mod.rs | 5 | ||||
| -rw-r--r-- | crates/atuin-server/src/handlers/user.rs | 4 | ||||
| -rw-r--r-- | crates/atuin-server/src/handlers/v0/record.rs | 6 | ||||
| -rw-r--r-- | crates/atuin-server/src/handlers/v0/store.rs | 4 | ||||
| -rw-r--r-- | crates/atuin-server/src/lib.rs | 61 | ||||
| -rw-r--r-- | crates/atuin-server/src/metrics.rs | 4 | ||||
| -rw-r--r-- | crates/atuin-server/src/router.rs | 23 | ||||
| -rw-r--r-- | crates/atuin-server/src/settings.rs | 18 |
9 files changed, 42 insertions, 91 deletions
diff --git a/crates/atuin-server/src/handlers/history.rs b/crates/atuin-server/src/handlers/history.rs index 5547a180..bdafcc60 100644 --- a/crates/atuin-server/src/handlers/history.rs +++ b/crates/atuin-server/src/handlers/history.rs @@ -65,7 +65,7 @@ pub async fn list<DB: Database>( if req.sync_ts.unix_timestamp_nanos() < 0 || req.history_ts.unix_timestamp_nanos() < 0 { error!("client asked for history from < epoch 0"); - counter!("atuin_history_epoch_before_zero", 1); + counter!("atuin_history_epoch_before_zero").increment(1); return Err( ErrorResponse::reply("asked for history from before epoch 0") @@ -95,7 +95,7 @@ pub async fn list<DB: Database>( user.id ); - counter!("atuin_history_returned", history.len() as u64); + counter!("atuin_history_returned").increment(history.len() as u64); Ok(Json(SyncHistoryResponse { history })) } @@ -131,7 +131,7 @@ pub async fn add<DB: Database>( let State(AppState { database, settings }) = state; debug!("request to add {} history items", req.len()); - counter!("atuin_history_uploaded", req.len() as u64); + counter!("atuin_history_uploaded").increment(req.len() as u64); let mut history: Vec<NewHistory> = req .into_iter() @@ -151,7 +151,7 @@ pub async fn add<DB: Database>( // Don't return an error here. We want to insert as much of the // history list as we can, so log the error and continue going. if !keep { - counter!("atuin_history_too_long", 1); + counter!("atuin_history_too_long").increment(1); tracing::warn!( "history too long, got length {}, max {}", diff --git a/crates/atuin-server/src/handlers/mod.rs b/crates/atuin-server/src/handlers/mod.rs index 1b9fd162..2176ac5e 100644 --- a/crates/atuin-server/src/handlers/mod.rs +++ b/crates/atuin-server/src/handlers/mod.rs @@ -16,10 +16,6 @@ const VERSION: &str = env!("CARGO_PKG_VERSION"); pub async fn index<DB: Database>(state: State<AppState<DB>>) -> Json<IndexResponse> { let homage = r#""Through the fathomless deeps of space swims the star turtle Great A'Tuin, bearing on its back the four giant elephants who carry on their shoulders the mass of the Discworld." -- Sir Terry Pratchett"#; - // Error with a -1 response - // It's super unlikely this will happen - let count = state.database.total_history().await.unwrap_or(-1); - let version = state .settings .fake_version @@ -28,7 +24,6 @@ pub async fn index<DB: Database>(state: State<AppState<DB>>) -> Json<IndexRespon Json(IndexResponse { homage: homage.to_string(), - total_history: count, version, }) } diff --git a/crates/atuin-server/src/handlers/user.rs b/crates/atuin-server/src/handlers/user.rs index e493e714..4edd1787 100644 --- a/crates/atuin-server/src/handlers/user.rs +++ b/crates/atuin-server/src/handlers/user.rs @@ -146,7 +146,7 @@ pub async fn register<DB: Database>( .await; } - counter!("atuin_users_registered", 1); + counter!("atuin_users_registered").increment(1); match db.add_session(&new_session).await { Ok(_) => Ok(Json(RegisterResponse { session: token })), @@ -173,7 +173,7 @@ pub async fn delete<DB: Database>( .with_status(StatusCode::INTERNAL_SERVER_ERROR)); }; - counter!("atuin_users_deleted", 1); + counter!("atuin_users_deleted").increment(1); Ok(Json(DeleteUserResponse {})) } diff --git a/crates/atuin-server/src/handlers/v0/record.rs b/crates/atuin-server/src/handlers/v0/record.rs index 01b91599..5c57910b 100644 --- a/crates/atuin-server/src/handlers/v0/record.rs +++ b/crates/atuin-server/src/handlers/v0/record.rs @@ -25,14 +25,14 @@ pub async fn post<DB: Database>( "request to add records" ); - counter!("atuin_record_uploaded", records.len() as u64); + counter!("atuin_record_uploaded").increment(records.len() as u64); let keep = records .iter() .all(|r| r.data.data.len() <= settings.max_record_size || settings.max_record_size == 0); if !keep { - counter!("atuin_record_too_large", 1); + counter!("atuin_record_too_large").increment(1); return Err( ErrorResponse::reply("could not add records; record too large") @@ -108,7 +108,7 @@ pub async fn next<DB: Database>( } }; - counter!("atuin_record_downloaded", records.len() as u64); + counter!("atuin_record_downloaded").increment(records.len() as u64); Ok(Json(records)) } diff --git a/crates/atuin-server/src/handlers/v0/store.rs b/crates/atuin-server/src/handlers/v0/store.rs index 941f2487..6ca455d7 100644 --- a/crates/atuin-server/src/handlers/v0/store.rs +++ b/crates/atuin-server/src/handlers/v0/store.rs @@ -24,14 +24,14 @@ pub async fn delete<DB: Database>( }) = state; if let Err(e) = database.delete_store(&user).await { - counter!("atuin_store_delete_failed", 1); + counter!("atuin_store_delete_failed").increment(1); error!("failed to delete store {e:?}"); return Err(ErrorResponse::reply("failed to delete store") .with_status(StatusCode::INTERNAL_SERVER_ERROR)); } - counter!("atuin_store_deleted", 1); + counter!("atuin_store_deleted").increment(1); Ok(()) } diff --git a/crates/atuin-server/src/lib.rs b/crates/atuin-server/src/lib.rs index f1d616f2..fcf5dde6 100644 --- a/crates/atuin-server/src/lib.rs +++ b/crates/atuin-server/src/lib.rs @@ -5,9 +5,7 @@ use std::net::SocketAddr; use atuin_server_database::Database; use axum::{Router, serve}; -use axum_server::Handle; -use axum_server::tls_rustls::RustlsConfig; -use eyre::{Context, Result, eyre}; +use eyre::{Context, Result}; mod handlers; mod metrics; @@ -46,18 +44,14 @@ async fn shutdown_signal() { } pub async fn launch<Db: Database>(settings: Settings, addr: SocketAddr) -> Result<()> { - if settings.tls.enable { - launch_with_tls::<Db>(settings, addr, shutdown_signal()).await - } else { - launch_with_tcp_listener::<Db>( - settings, - TcpListener::bind(addr) - .await - .context("could not connect to socket")?, - shutdown_signal(), - ) - .await - } + launch_with_tcp_listener::<Db>( + settings, + TcpListener::bind(addr) + .await + .context("could not connect to socket")?, + shutdown_signal(), + ) + .await } pub async fn launch_with_tcp_listener<Db: Database>( @@ -74,43 +68,6 @@ pub async fn launch_with_tcp_listener<Db: Database>( Ok(()) } -async fn launch_with_tls<Db: Database>( - settings: Settings, - addr: SocketAddr, - shutdown: impl Future<Output = ()>, -) -> Result<()> { - let crypto_provider = rustls::crypto::ring::default_provider().install_default(); - if crypto_provider.is_err() { - return Err(eyre!("Failed to install default crypto provider")); - } - let rustls_config = RustlsConfig::from_pem_file( - settings.tls.cert_path.clone(), - settings.tls.pkey_path.clone(), - ) - .await; - if rustls_config.is_err() { - return Err(eyre!("Failed to load TLS key and/or certificate")); - } - let rustls_config = rustls_config.unwrap(); - - let r = make_router::<Db>(settings).await?; - - let handle = Handle::new(); - - let server = axum_server::bind_rustls(addr, rustls_config) - .handle(handle.clone()) - .serve(r.into_make_service()); - - tokio::select! { - _ = server => {} - _ = shutdown => { - handle.graceful_shutdown(None); - } - } - - Ok(()) -} - // The separate listener means it's much easier to ensure metrics are not accidentally exposed to // the public. pub async fn launch_metrics_server(host: String, port: u16) -> Result<()> { diff --git a/crates/atuin-server/src/metrics.rs b/crates/atuin-server/src/metrics.rs index ff0fe925..ebd0dd2d 100644 --- a/crates/atuin-server/src/metrics.rs +++ b/crates/atuin-server/src/metrics.rs @@ -48,8 +48,8 @@ pub async fn track_metrics(req: Request, next: Next) -> impl IntoResponse { ("status", status), ]; - metrics::increment_counter!("http_requests_total", &labels); - metrics::histogram!("http_requests_duration_seconds", latency, &labels); + metrics::counter!("http_requests_total", &labels).increment(1); + metrics::histogram!("http_requests_duration_seconds", &labels).record(latency); response } diff --git a/crates/atuin-server/src/router.rs b/crates/atuin-server/src/router.rs index 1118ab29..9d4f7d44 100644 --- a/crates/atuin-server/src/router.rs +++ b/crates/atuin-server/src/router.rs @@ -109,15 +109,22 @@ pub struct AppState<DB: Database> { } pub fn router<DB: Database>(database: DB, settings: Settings) -> Router { - let routes = Router::new() + let mut routes = Router::new() .route("/", get(handlers::index)) - .route("/healthz", get(handlers::health::health_check)) - .route("/sync/count", get(handlers::history::count)) - .route("/sync/history", get(handlers::history::list)) - .route("/sync/calendar/:focus", get(handlers::history::calendar)) - .route("/sync/status", get(handlers::status::status)) - .route("/history", post(handlers::history::add)) - .route("/history", delete(handlers::history::delete)) + .route("/healthz", get(handlers::health::health_check)); + + // Sync v1 routes - can be disabled in favor of record-based sync + if settings.sync_v1_enabled { + routes = routes + .route("/sync/count", get(handlers::history::count)) + .route("/sync/history", get(handlers::history::list)) + .route("/sync/calendar/:focus", get(handlers::history::calendar)) + .route("/sync/status", get(handlers::status::status)) + .route("/history", post(handlers::history::add)) + .route("/history", delete(handlers::history::delete)); + } + + let routes = routes .route("/user/:username", get(handlers::user::get)) .route("/account", delete(handlers::user::delete)) .route("/account/password", patch(handlers::user::change_password)) diff --git a/crates/atuin-server/src/settings.rs b/crates/atuin-server/src/settings.rs index 7221d4dd..98d1d69f 100644 --- a/crates/atuin-server/src/settings.rs +++ b/crates/atuin-server/src/settings.rs @@ -65,9 +65,12 @@ pub struct Settings { pub register_webhook_url: Option<String>, pub register_webhook_username: String, pub metrics: Metrics, - pub tls: Tls, pub mail: Mail, + /// Enable legacy sync v1 routes (history-based sync) + /// Set to false to use only the newer record-based sync + pub sync_v1_enabled: bool, + /// Advertise a version that is not what we are _actually_ running /// Many clients compare their version with api.atuin.sh, and if they differ, notify the user /// that an update is available. @@ -106,9 +109,7 @@ impl Settings { .set_default("metrics.host", "127.0.0.1")? .set_default("metrics.port", 9001)? .set_default("mail.enable", false)? - .set_default("tls.enable", false)? - .set_default("tls.cert_path", "")? - .set_default("tls.pkey_path", "")? + .set_default("sync_v1_enabled", true)? .add_source( Environment::with_prefix("atuin") .prefix_separator("_") @@ -139,12 +140,3 @@ impl Settings { pub fn example_config() -> &'static str { EXAMPLE_CONFIG } - -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub struct Tls { - #[serde(alias = "enabled")] - pub enable: bool, - - pub cert_path: PathBuf, - pub pkey_path: PathBuf, -} |
