athena_rs 2.9.1

Database gateway API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

//! Athena RS binary.
//!
//! Starts the Actix Web server, wires endpoints, configures CORS and tracing,
//! and exposes convenience endpoints for Scylla demo queries.
//!
//!
use actix_cors::Cors;
use actix_web::body::MessageBody;
use actix_web::dev::{Service, ServiceResponse};
use actix_web::http::StatusCode;
use actix_web::http::header;
use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers};
use actix_web::{App, HttpServer, web};
use sentry::types::Dsn as SentryDsn;

use actix_web::web::Data;
use clap::Parser;
use dotenv::dotenv;
use sentry::ClientInitGuard;
use std::env;
use std::io::Error;
use std::io::ErrorKind::{AddrInUse, Other};
use std::io::IsTerminal;
use std::io::Result as IoResult;
use std::net::{SocketAddr, TcpListener};
use std::path::PathBuf;
use std::thread::available_parallelism;
use std::time::{Duration, Instant};
use tracing::info;
use tracing::warn;
use tracing_subscriber::EnvFilter;
use tracing_subscriber::fmt::time::ChronoLocal;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;

use socket2::{Domain, Protocol, Socket, TcpKeepalive, Type};

use athena_rs::AppState;
use athena_rs::api::gateway::delete::delete_data;
use athena_rs::api::gateway::fetch::{
    fetch_data_route, gateway_update_route, get_data_route, proxy_fetch_data_route,
};
use athena_rs::api::gateway::insert::insert_data;
use athena_rs::api::gateway::postgrest::{
    postgrest_delete_route, postgrest_get_route, postgrest_patch_route, postgrest_post_route,
};
use athena_rs::api::gateway::query::gateway_query_route;
use athena_rs::api::health::{cluster_health, ping, root};
use athena_rs::api::metrics::prometheus_metrics;
use athena_rs::api::pipelines::{list_pipeline_templates, run_pipeline, simulate_pipeline};
use athena_rs::api::query::sql::sql_query;
use athena_rs::api::registry::{api_registry, api_registry_by_id};
use athena_rs::api::response::{internal_error, not_found};
use athena_rs::api::supabase::ssl_enforcement;
use athena_rs::api::{
    admin, athena_docs, athena_openapi_host, athena_router_registry, athena_wss_openapi_host,
    backup, management, provision, schema, storage,
};
use athena_rs::bootstrap::{Bootstrap, build_shared_state};
use athena_rs::cdc::websocket::websocket_server;
use athena_rs::cli::{self, AthenaCli, Command};
use athena_rs::config::{Config, ConfigLocation, DEFAULT_CONFIG_FILE_NAME};
use athena_rs::daemon::{spawn_connection_monitor, spawn_vacuum_health_collector};
use athena_rs::parser::{parse_secs_or_default, parse_usize};
use athena_rs::utils::pg_tools::ensure_pg_tools;
use athena_rs::wss::gateway_wss_info;

#[actix_web::main]
async fn main() -> IoResult<()> {
    dotenv().ok();
    let sentry_guard: Option<ClientInitGuard> = init_sentry();
    init_tracing(sentry_guard.is_some());
    let _sentry_guard: Option<ClientInitGuard> = sentry_guard;

    let cli: AthenaCli = AthenaCli::parse();
    let config_path: Option<PathBuf> = cli.config_path.clone();
    let pipelines_path: PathBuf = cli.pipelines_path.clone();
    let port_override: Option<u16> = cli.port;
    let command: Option<Command> = cli.command;
    let api_only: bool = cli.api_only;
    #[cfg(feature = "cdc")]
    let cdc_only: bool = cli.cdc_only;

    let config_overridden: bool = config_path.is_some();
    let config_path: PathBuf = config_path
        .clone()
        .unwrap_or_else(|| PathBuf::from(DEFAULT_CONFIG_FILE_NAME));
    let pipelines_path: String = pipelines_path.to_string_lossy().to_string();
    let config: Config = if config_overridden {
        Config::load_from(&config_path).map_err(|err| {
            let attempted_locations: Vec<ConfigLocation> = vec![ConfigLocation::new(
                "explicit config path".to_string(),
                config_path.clone(),
            )];
            Error::new(
                Other,
                format!(
                    "Failed to load config '{}': {}. Looked in:\n{}",
                    config_path.display(),
                    err,
                    format_attempted_locations(&attempted_locations)
                ),
            )
        })?
    } else {
        match Config::load_default() {
            Ok(outcome) => {
                if outcome.seeded_default {
                    info!("Seeded default configuration at {}", outcome.path.display());
                }
                outcome.config
            }
            Err(err) => {
                let attempts: Vec<ConfigLocation> = err.attempted_locations.clone();
                return Err(Error::new(
                    Other,
                    format!(
                        "Failed to load config '{}': {}. Looked in:\n{}",
                        config_path.display(),
                        err,
                        format_attempted_locations(&attempts)
                    ),
                ));
            }
        }
    };

    match command {
        Some(Command::Server) => {
            let bootstrap: Bootstrap = build_shared_state(&config, &pipelines_path)
                .await
                .map_err(|err| Error::new(Other, err.to_string()))?;
            run_server(&config, bootstrap, port_override).await
        }
        Some(Command::Pipeline(args)) => {
            let bootstrap: Bootstrap = build_shared_state(&config, &pipelines_path)
                .await
                .map_err(|err| Error::new(Other, err.to_string()))?;
            cli::run_pipeline_command(&bootstrap, args)
                .await
                .map_err(|err| Error::new(Other, err.to_string()))
        }
        Some(Command::Clients { command }) => {
            cli::run_clients_command(command).map_err(|err| Error::new(Other, err.to_string()))
        }
        Some(Command::Fetch(cmd)) => cli::run_fetch_command(cmd)
            .await
            .map_err(|err| Error::new(Other, err.to_string())),
        Some(Command::Provision(args)) => cli::run_provision_command(args)
            .await
            .map_err(|err| Error::new(Other, err.to_string())),
        #[cfg(feature = "cdc")]
        Some(Command::Cdc { command }) => cli::run_cdc_command(&config, command)
            .await
            .map_err(|err| Error::new(Other, err.to_string())),
        Some(Command::Diag) => {
            cli::run_diag_command().map_err(|err| Error::new(Other, err.to_string()))
        }
        Some(Command::Version) => {
            cli::run_version_command();
            Ok(())
        }
        None => {
            #[cfg(feature = "cdc")]
            {
                if cdc_only {
                    let port: u16 = port_override.unwrap_or(4053);
                    websocket_server(port)
                        .await
                        .map_err(|err| Error::new(Other, err.to_string()))
                } else if api_only {
                    let bootstrap: Bootstrap = build_shared_state(&config, &pipelines_path)
                        .await
                        .map_err(|err| Error::new(Other, err.to_string()))?;
                    run_server(&config, bootstrap, port_override).await
                } else {
                    Err(Error::new(
                        Other,
                        "No command provided; pass --api-only to boot the API, --cdc-only for the CDC WebSocket server, or specify a subcommand.",
                    ))
                }
            }
            #[cfg(not(feature = "cdc"))]
            {
                if api_only {
                    let bootstrap: Bootstrap = build_shared_state(&config, &pipelines_path)
                        .await
                        .map_err(|err| Error::new(Other, err.to_string()))?;
                    run_server(&config, bootstrap, port_override).await
                } else {
                    Err(Error::new(
                        Other,
                        "No command provided; pass --api-only to boot the API or specify a subcommand.",
                    ))
                }
            }
        }
    }
}

/// Registers default error handlers so 404 and all 5xx responses use the standard
/// API envelope (status, message, error). Ensures every request path can result
/// in at least a 2xx or a 5xx (or 4xx) with a consistent JSON body.
fn default_error_handlers<B>() -> ErrorHandlers<B>
where
    B: MessageBody + 'static,
{
    ErrorHandlers::new()
        .handler(StatusCode::NOT_FOUND, |res: ServiceResponse<B>| {
            let has_json_body: bool = res
                .headers()
                .get(header::CONTENT_TYPE)
                .and_then(|v| v.to_str().ok())
                .is_some_and(|ct| ct.contains("application/json"));

            if has_json_body {
                Ok(ErrorHandlerResponse::Response(
                    res.map_into_boxed_body().map_into_right_body(),
                ))
            } else {
                let (req, _) = res.into_parts();
                let resp: actix_web::HttpResponse =
                    not_found("Not Found", "The requested resource was not found");
                Ok(ErrorHandlerResponse::Response(
                    ServiceResponse::new(req, resp)
                        .map_into_boxed_body()
                        .map_into_right_body(),
                ))
            }
        })
        .default_handler_server(|res: ServiceResponse<B>| {
            let has_json_body: bool = res
                .headers()
                .get(header::CONTENT_TYPE)
                .and_then(|v| v.to_str().ok())
                .is_some_and(|ct| ct.contains("application/json"));

            if has_json_body {
                Ok(ErrorHandlerResponse::Response(
                    res.map_into_boxed_body().map_into_right_body(),
                ))
            } else {
                let (req, _) = res.into_parts();
                let resp: actix_web::HttpResponse =
                    internal_error("Internal Server Error", "An unexpected error occurred");
                Ok(ErrorHandlerResponse::Response(
                    ServiceResponse::new(req, resp)
                        .map_into_boxed_body()
                        .map_into_right_body(),
                ))
            }
        })
}

async fn run_server(
    config: &Config,
    bootstrap: Bootstrap,
    port_override: Option<u16>,
) -> IoResult<()> {
    let port: u16 = if let Some(port) = port_override {
        port
    } else {
        config
            .get_api()
            .ok_or("No API port configured")
            .and_then(|port_str| port_str.parse().map_err(|_| "Invalid port number"))
            .expect("Failed to parse API port")
    };

    let keep_alive: Duration = parse_secs_or_default(config.get_http_keep_alive_secs(), 15);
    let client_disconnect_timeout: Duration =
        parse_secs_or_default(config.get_client_disconnect_timeout_secs(), 60);
    let client_request_timeout =
        parse_secs_or_default(config.get_client_request_timeout_secs(), 60);
    let worker_count: usize = config
        .get_http_workers()
        .and_then(|v| parse_usize(v.as_str()))
        .unwrap_or_else(|| available_parallelism().map(|n| n.get()).unwrap_or(4));
    let max_connections: usize = config
        .get_http_max_connections()
        .and_then(|v| parse_usize(v.as_str()))
        .unwrap_or(10_000);
    let backlog: usize = config
        .get_http_backlog()
        .and_then(|v| parse_usize(v.as_str()))
        .unwrap_or(2_048);
    let tcp_keepalive: Duration = parse_secs_or_default(config.get_tcp_keepalive_secs(), 75);
    let prometheus_metrics_enabled: bool = config.get_prometheus_metrics_enabled();

    let addr: SocketAddr = SocketAddr::from(([0, 0, 0, 0], port));
    let socket: Socket = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))?;
    socket.set_nonblocking(true)?;
    socket.set_reuse_address(true)?;
    socket.set_keepalive(true)?;
    let keepalive_cfg: TcpKeepalive = TcpKeepalive::new().with_time(tcp_keepalive);
    socket.set_tcp_keepalive(&keepalive_cfg)?;
    socket.bind(&addr.into()).map_err(|e| {
        let msg = if e.kind() == AddrInUse {
            format!(
                "Address already in use (port {}). Stop the process using this port or choose a different one (e.g. --port).",
                port
            )
        } else {
            format!("Failed to bind to {}: {}", addr, e)
        };
        Error::new(e.kind(), msg)
    })?;
    let listen_backlog: i32 = backlog.min(i32::MAX as usize) as i32;
    socket
        .listen(listen_backlog)
        .map_err(|e| Error::new(e.kind(), format!("Failed to listen on {}: {}", addr, e)))?;
    let listener: TcpListener = socket.into();

    // Ensure tools exist (version selection happens at backup/restore runtime).
    // This keeps startup cheap while still surfacing missing binaries early.
    ensure_pg_tools()
        .await
        .map_err(|err| Error::new(Other, format!("PostgreSQL tools unavailable: {err}")))?;

    let app_state: Data<AppState> = bootstrap.app_state.clone();
    spawn_connection_monitor(app_state.clone());
    spawn_vacuum_health_collector(app_state.clone());

    let cors_allow_any_origin: bool = config.get_cors_allow_any_origin();
    let cors_allowed_origins: Vec<String> = config.get_cors_allowed_origins();

    if cors_allow_any_origin {
        warn!(
            "CORS is configured with allow_any_origin=true; consider using api.cors_allowed_origins for tighter access control"
        );
    } else if cors_allowed_origins.is_empty() {
        warn!(
            "CORS allow_any_origin is false but no cors_allowed_origins were configured; cross-origin browser requests will be denied"
        );
    }

    HttpServer::new(move || {
        let mut cors: Cors = Cors::default().allow_any_method().allow_any_header();
        if cors_allow_any_origin {
            cors = cors.allow_any_origin();
        } else {
            for origin in &cors_allowed_origins {
                cors = cors.allowed_origin(origin);
            }
        }

        let mut app = App::new()
            .wrap(default_error_handlers())
            .wrap(cors)
            .wrap_fn(|req, srv| {
                let metrics_state: Option<Data<AppState>> =
                    req.app_data::<web::Data<AppState>>().cloned();
                let method: String = req.method().as_str().to_string();
                let route: String = req
                    .match_pattern()
                    .map(|value| value.to_string())
                    .unwrap_or_else(|| req.path().to_string());
                let request_bytes = req
                    .headers()
                    .get(header::CONTENT_LENGTH)
                    .and_then(|value| value.to_str().ok())
                    .and_then(|value| value.parse::<u64>().ok());
                let athena_client = req
                    .headers()
                    .get("X-Athena-Client")
                    .and_then(|value| value.to_str().ok())
                    .map(str::to_string);
                let started: Instant = Instant::now();
                if let Some(state) = &metrics_state {
                    state.metrics_state.begin_http_request(&method, &route);
                }
                let fut = srv.call(req);
                async move {
                    match fut.await {
                        Ok(mut res) => {
                            res.headers_mut()
                                .insert(header::SERVER, "XYLEX/0".parse().unwrap());
                            if let Some(state) = metrics_state {
                                let response_bytes: Option<u64> = res
                                    .headers()
                                    .get(header::CONTENT_LENGTH)
                                    .and_then(|value| value.to_str().ok())
                                    .and_then(|value| value.parse::<u64>().ok());
                                state.metrics_state.finish_http_request(
                                    &method,
                                    &route,
                                    res.status().as_u16(),
                                    started.elapsed().as_secs_f64(),
                                    request_bytes,
                                    response_bytes,
                                    athena_client.as_deref(),
                                );
                            }
                            Ok(res)
                        }

                        Err(err) => {
                            if let Some(state) = metrics_state {
                                state.metrics_state.record_http_handler_error(
                                    &method,
                                    &route,
                                    started.elapsed().as_secs_f64(),
                                    request_bytes,
                                    athena_client.as_deref(),
                                );
                            }
                            Err(err)
                        }
                    }
                }
            })
            .app_data(app_state.clone())
            .service(root)
            .service(ping)
            .service(cluster_health)
            .service(sql_query)
            .service(fetch_data_route)
            .service(get_data_route)
            .service(proxy_fetch_data_route)
            .service(gateway_update_route)
            .service(gateway_query_route)
            .service(insert_data)
            .service(delete_data)
            .service(postgrest_get_route)
            .service(postgrest_post_route)
            .service(postgrest_patch_route)
            .service(postgrest_delete_route)
            .service(run_pipeline)
            .service(simulate_pipeline)
            .service(list_pipeline_templates)
            .service(athena_router_registry)
            .service(athena_openapi_host)
            .service(athena_wss_openapi_host)
            .service(api_registry)
            .service(athena_docs)
            .service(api_registry_by_id)
            .service(gateway_wss_info)
            .configure(admin::services)
            .configure(backup::services)
            .configure(provision::services)
            .configure(management::services)
            .configure(schema::services)
            .configure(storage::services)
            .service(ssl_enforcement);
        if prometheus_metrics_enabled {
            app = app.service(prometheus_metrics);
        }
        app
    })
    .workers(worker_count)
    .keep_alive(keep_alive)
    .client_disconnect_timeout(client_disconnect_timeout)
    .client_request_timeout(client_request_timeout)
    .max_connections(max_connections)
    .backlog(backlog as u32)
    .listen(listener)?
    .run()
    .await
}

/// Configures tracing with chrono timestamps and an environment-configurable filter.
/// ANSI colors are enabled only when stderr is a TTY (or when ATHENA_ANSI=1).
/// Set NO_COLOR or ATHENA_ANSI=0 to disable colors even in a TTY.
fn init_tracing(enable_sentry_layer: bool) {
    let filter: EnvFilter =
        EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
    let no_color: bool = env::var("NO_COLOR").is_ok();
    let ansi: bool = !no_color
        && env::var("ATHENA_ANSI")
            .ok()
            .and_then(|v| match v.as_str() {
                "1" | "true" | "yes" => Some(true),
                "0" | "false" | "no" => Some(false),
                _ => None,
            })
            .unwrap_or_else(|| std::io::stderr().is_terminal());
    let fmt_layer = tracing_subscriber::fmt::layer()
        .with_ansi(ansi)
        .with_level(true)
        .with_target(true)
        .with_timer(ChronoLocal::new("%H:%M:%S%.3f".to_string()));

    let base: tracing_subscriber::layer::Layered<
        tracing_subscriber::fmt::Layer<
            tracing_subscriber::layer::Layered<EnvFilter, tracing_subscriber::Registry>,
            tracing_subscriber::fmt::format::DefaultFields,
            tracing_subscriber::fmt::format::Format<
                tracing_subscriber::fmt::format::Full,
                ChronoLocal,
            >,
        >,
        tracing_subscriber::layer::Layered<EnvFilter, tracing_subscriber::Registry>,
    > = tracing_subscriber::registry().with(filter).with(fmt_layer);

    if enable_sentry_layer {
        base.with(sentry_tracing::layer()).init();
    } else {
        base.init();
    }
}

const BETTERSTACK_SENTRY_DSN: &str =
    "https://mMR1Bs5K6vSzXT8YGYyUxQSE@s1741777.eu-fsn-3.betterstackdata.com/1741777";

fn init_sentry() -> Option<sentry::ClientInitGuard> {
    let dsn_source: String =
        env::var("SENTRY_DSN").unwrap_or_else(|_| BETTERSTACK_SENTRY_DSN.to_string());

    if dsn_source.trim().is_empty() {
        return None;
    }

    let dsn: SentryDsn = match dsn_source.parse::<SentryDsn>() {
        Ok(dsn) => dsn,
        Err(err) => {
            eprintln!("failed to parse Sentry DSN: {err}");
            return None;
        }
    };

    let mut options: sentry::ClientOptions = sentry::ClientOptions {
        dsn: Some(dsn),
        release: Some(env!("CARGO_PKG_VERSION").into()),
        environment: env::var("SENTRY_ENVIRONMENT").ok().map(Into::into),
        attach_stacktrace: true,
        ..Default::default()
    };

    if let Ok(value) = env::var("SENTRY_SAMPLE_RATE")
        && let Ok(parsed) = value.parse::<f32>()
    {
        options.sample_rate = parsed;
    }

    Some(sentry::init(options))
}

fn format_attempted_locations(locations: &[ConfigLocation]) -> String {
    locations
        .iter()
        .map(|location| format!("- {}", location.describe()))
        .collect::<Vec<_>>()
        .join("\n")
}