-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.rs
More file actions
525 lines (473 loc) · 20.4 KB
/
lib.rs
File metadata and controls
525 lines (473 loc) · 20.4 KB
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
mod account;
mod calendar;
mod error;
mod event;
mod job_schedulers;
mod schedule;
mod service;
mod shared;
mod status;
pub mod telemetry;
mod user;
use std::sync::Arc;
use axum::{Extension, Router, http::header};
use futures::lock::Mutex;
use job_schedulers::{start_reminder_generation_job, start_send_reminders_job};
use nittei_domain::{
Account,
AccountIntegration,
AccountWebhookSettings,
ID,
IntegrationProvider,
PEMKey,
};
use nittei_infra::NitteiContext;
use telemetry::http_logger::metadata_middleware;
use tokio::{
net::TcpListener,
sync::oneshot::{self, Sender},
};
use tower::ServiceBuilder;
use tower_http::{
catch_panic::CatchPanicLayer,
compression::CompressionLayer,
cors::CorsLayer,
decompression::RequestDecompressionLayer,
sensitive_headers::SetSensitiveHeadersLayer,
trace::TraceLayer,
};
use tracing::{error, info, warn};
use utoipa::{
Modify,
OpenApi,
openapi::security::{ApiKey, ApiKeyValue, SecurityScheme},
};
use utoipa_axum::router::OpenApiRouter;
use utoipa_swagger_ui::SwaggerUi;
use crate::{
shared::auth::NITTEI_X_API_KEY_HEADER,
telemetry::{
correlation_layer::CorrelationIdLayer,
http_logger::{NitteiTracingOnFailure, NitteiTracingOnResponse, NitteiTracingSpanBuilder},
},
};
/// Configure the Actix server API
/// Add all the routes to the server
pub fn configure_server_api() -> OpenApiRouter {
OpenApiRouter::new()
.merge(account::configure_routes())
.merge(calendar::configure_routes())
.merge(event::configure_routes())
.merge(schedule::configure_routes())
.merge(service::configure_routes())
.merge(status::configure_routes())
.merge(user::configure_routes())
}
/// Struct for storing the main application state
pub struct Application {
/// The Axum server instance
server: Router,
/// Listener for the server
listener: TcpListener,
/// The application context (database connections, etc.)
context: NitteiContext,
/// Shutdown data
/// Shared state of the server
shared_state: Arc<Mutex<ServerSharedState>>,
}
/// Struct for storing the shared state of the server
/// Mainly useful for sharing the shutdown flag between the binary crate and the status endpoint
pub struct ServerSharedState {
/// Channel to send shutdown signal
pub shutdown_tx: Option<Sender<()>>,
}
impl Application {
pub async fn new(context: NitteiContext) -> anyhow::Result<Self> {
let shared_state = Arc::new(Mutex::new(ServerSharedState { shutdown_tx: None }));
let (server, listener) =
Application::configure_server(context.clone(), shared_state.clone()).await?;
Application::start_jobs(context.clone());
Ok(Self {
server,
listener,
context,
shared_state,
})
}
pub fn get_port(&self) -> anyhow::Result<u16> {
Ok(self.listener.local_addr()?.port())
}
/// Start the background jobs of the application
/// Note that the jobs are only started if the environment variable NITTEI_REMINDERS_JOB_ENABLED is set to true
fn start_jobs(context: NitteiContext) {
if !nittei_utils::config::APP_CONFIG.disable_reminders {
start_send_reminders_job(context.clone());
start_reminder_generation_job(context);
}
}
/// Configure the Axum server
/// This function creates the server and adds all the routes to it
///
/// This adds the following middleware:
/// - CORS (permissive)
/// - Compression
/// - Tracing logger
/// - Sensitive headers
/// - Compression
/// - Catch panics
async fn configure_server(
context: NitteiContext,
shared_state: Arc<Mutex<ServerSharedState>>,
) -> anyhow::Result<(Router, TcpListener)> {
let api_router = configure_server_api();
let sensitive_headers = vec![
header::AUTHORIZATION,
header::HeaderName::from_static(NITTEI_X_API_KEY_HEADER),
];
let (router, api) = OpenApiRouter::with_openapi(ApiDoc::openapi())
.nest("/api/v1", api_router)
.layer(axum::middleware::from_fn(metadata_middleware))
.layer(
ServiceBuilder::new()
// Mark the `Authorization` header as sensitive so it doesn't show in logs
.layer(SetSensitiveHeadersLayer::new(sensitive_headers))
.layer(CorsLayer::permissive())
.layer(RequestDecompressionLayer::new())
.layer(CompressionLayer::new())
// Catch panics and convert them into responses.
.layer(CatchPanicLayer::new())
.layer(
TraceLayer::new_for_http()
.make_span_with(NitteiTracingSpanBuilder {})
.on_request(())
.on_response(NitteiTracingOnResponse {})
.on_failure(NitteiTracingOnFailure {}),
),
)
.layer(CorrelationIdLayer)
.layer(Extension(context.clone()))
.layer(Extension(shared_state.clone()))
.split_for_parts();
let router =
router.merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", api.clone()));
let port = context.config.port;
let address = nittei_utils::config::APP_CONFIG.http_host.clone();
let address_and_port = format!("{address}:{port}");
let listener = TcpListener::bind(address_and_port).await?;
info!("[server] Will start server on: {}", listener.local_addr()?);
Ok((router, listener))
}
/// Init the default account and start the Actix server
///
/// It also sets up the shutdown handler
pub async fn start(
mut self,
shutdown_channel: tokio::sync::oneshot::Receiver<()>,
) -> anyhow::Result<()> {
self.setup_shutdown_handler(shutdown_channel);
self.init_default_account().await?;
// Create a shutdown signal channel
let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();
self.shared_state.lock().await.shutdown_tx = Some(shutdown_tx);
info!("[server] Server started");
axum::serve(self.listener, self.server)
.with_graceful_shutdown(async {
shutdown_rx.await.unwrap_or_else(|e| {
error!("[server] Failed to listen for shutdown signal: {}", e)
});
info!("[server] Server stopped");
})
.await
.unwrap_or_else(|e| {
error!("[server] Server error: {:?}", e);
// Exit the process with an error code
std::process::exit(1);
});
info!("[server] Server closed");
Ok(())
}
/// Initialize the default account
/// The default account is created if it doesn't exist
async fn init_default_account(&self) -> anyhow::Result<()> {
let secret_api_key_option = nittei_utils::config::APP_CONFIG
.account
.as_ref()
.and_then(|a| a.secret_key.clone());
let secret_api_key = match &secret_api_key_option {
Some(key) => {
info!("[init_default_account] Using provided secret api key");
key.to_owned()
}
None => Account::generate_secret_api_key(),
};
if self
.context
.repos
.accounts
.find_by_apikey(&secret_api_key)
.await?
.is_none()
{
if secret_api_key_option.is_none() {
info!(
"[init_default_account] Creating default account with self-generated secret api key"
);
} else {
warn!(
"[init_default_account] Account not found based on given secret api key - creating default account"
);
}
let mut account = Account::default();
let account_id = nittei_utils::config::APP_CONFIG
.account
.as_ref()
.and_then(|a| a.id.clone())
.unwrap_or_default()
.parse::<ID>()
.unwrap_or_default();
info!("[init_default_account] Using account id: {}", account_id);
account.id = account_id;
account.secret_api_key = secret_api_key.clone();
account.settings.webhook = nittei_utils::config::APP_CONFIG
.account
.as_ref()
.and_then(|a| a.webhook_url.clone())
.map(|url| AccountWebhookSettings {
url,
key: Default::default(),
});
if let Some(mut verification_key) = nittei_utils::config::APP_CONFIG
.account
.as_ref()
.and_then(|a| a.pub_key.clone())
{
verification_key = verification_key.replace("\\n", "\n");
match PEMKey::new(verification_key) {
Ok(k) => account.set_public_jwt_key(Some(k)),
Err(e) => warn!(
"[init_default_account] Invalid ACCOUNT_PUB_KEY provided: {:?}",
e
),
};
}
self.context.repos.accounts.insert(&account).await?;
let account_google_client_id_env = "NITTEI__ACCOUNT__GOOGLE__CLIENT_ID";
let account_google_client_secret_env = "NITTEI__ACCOUNT__GOOGLE__CLIENT_SECRET";
let account_google_redirect_uri_env = "NITTEI__ACCOUNT__GOOGLE__REDIRECT_URI";
let google_config = nittei_utils::config::APP_CONFIG
.account
.as_ref()
.and_then(|a| a.google.as_ref());
if let Some(google_client_id) = google_config.map(|g| g.client_id.clone()) {
let google_client_secret = google_config
.map(|g| g.client_secret.clone())
.unwrap_or_else(|| {
panic!(
"{account_google_client_secret_env} should be specified also when {account_google_client_id_env} is specified."
)
});
let google_redirect_uri = google_config
.map(|g| g.redirect_uri.clone())
.unwrap_or_else(|| {
panic!(
"{account_google_redirect_uri_env} should be specified also when {account_google_client_id_env} is specified."
)
});
self.context
.repos
.account_integrations
.insert(&AccountIntegration {
account_id: account.id.clone(),
client_id: google_client_id,
client_secret: google_client_secret,
redirect_uri: google_redirect_uri,
provider: IntegrationProvider::Google,
})
.await?;
}
let account_outlook_client_id_env = "NITTEI__ACCOUNT__OUTLOOK__CLIENT_ID";
let account_outlook_client_secret_env = "NITTEI__ACCOUNT__OUTLOOK__CLIENT_SECRET";
let account_outlook_redirect_uri_env = "NITTEI__ACCOUNT__OUTLOOK__REDIRECT_URI";
let outlook_config = nittei_utils::config::APP_CONFIG
.account
.as_ref()
.and_then(|a| a.outlook.as_ref());
if let Some(outlook_client_id) = outlook_config.map(|o| o.client_id.clone()) {
let outlook_client_secret = outlook_config
.map(|o| o.client_secret.clone())
.unwrap_or_else(|| {
panic!(
"{account_outlook_client_secret_env} should be specified also when {account_outlook_client_id_env} is specified."
)
});
let outlook_redirect_uri = outlook_config
.map(|o| o.redirect_uri.clone())
.unwrap_or_else(|| {
panic!(
"{account_outlook_redirect_uri_env} should be specified also when {account_outlook_client_id_env} is specified."
)
});
self.context
.repos
.account_integrations
.insert(&AccountIntegration {
account_id: account.id.clone(),
client_id: outlook_client_id,
client_secret: outlook_client_secret,
redirect_uri: outlook_redirect_uri,
provider: IntegrationProvider::Outlook,
})
.await?;
// Check account is created
if let Some(account) = self
.context
.repos
.accounts
.find_by_apikey(&secret_api_key)
.await?
{
info!("[init_default_account] Account created: {:?}", account.id);
} else {
error!(
"[init_default_account] Account not created {:?}",
account.id
);
}
}
};
Ok(())
}
/// Setup the shutdown handler
fn setup_shutdown_handler(&mut self, shutdown_channel: tokio::sync::oneshot::Receiver<()>) {
// Clone shutdown_tx before entering the async block
let shared_state = self.shared_state.clone();
// Listen to shutdown channel
tokio::spawn(async move {
// Wait for the shutdown channel to receive a message
if let Err(e) = shutdown_channel.await {
error!(
"[shutdown_handler] Failed to listen for shutdown channel: {}",
e
);
} else {
info!("[shutdown_handler] Received shutdown signal",);
if cfg!(debug_assertions) {
// In debug mode, stop the server immediately
info!("[shutdown_handler] Stopping server...");
if let Some(server_handle) = shared_state.lock().await.shutdown_tx.take() {
server_handle.send(()).unwrap_or_else(|_| {
error!("[shutdown_handler] Failed to send shutdown signal");
// Exit the process with an error code
std::process::exit(1);
});
}
info!("[shutdown_handler] api crate - shutdown complete");
} else {
// In production, do the whole graceful shutdown process (wait for a timeout before stopping the server)
let duration = nittei_utils::config::APP_CONFIG.server_shutdown_sleep;
info!("[shutdown_handler] Waiting {}s before stopping", duration);
// Wait for the timeout
tokio::time::sleep(std::time::Duration::from_secs(duration)).await;
info!("[shutdown_handler] Stopping server...");
// Shutdown the server
if let Some(server_handle) = shared_state.lock().await.shutdown_tx.take() {
server_handle.send(()).unwrap_or_else(|_| {
error!("[shutdown_handler] Failed to send shutdown signal");
// Exit the process with an error code
std::process::exit(1);
});
}
info!("[shutdown_handler] api crate - shutdown complete");
}
}
});
}
}
#[derive(OpenApi)]
#[openapi(
info(
title = "Nittei API",
version = "1.0.0",
description = "OpenAPI documentation for the Nittei API",
),
tags(
{name = "Account", description = "Account API endpoints"},
{name = "Calendar", description = "Calendar API endpoints"},
{name = "Event", description = "Event API endpoints"},
{name = "Schedule", description = "Schedule API endpoints"},
{name = "Service", description = "Service API endpoints"},
{name = "Status", description = "Status API endpoints"},
{name = "User", description = "User API endpoints"},
),
modifiers(&SecurityAddon),
paths(
// Account
account::account_search_events::account_search_events_controller,
account::create_account::create_account_controller,
account::get_account::get_account_controller,
account::set_account_pub_key::set_account_pub_key_controller,
account::set_account_webhook::set_account_webhook_controller,
account::delete_account_webhook::delete_account_webhook_controller,
account::add_account_integration::add_account_integration_controller,
account::remove_account_integration::remove_account_integration_controller,
// Calendar
calendar::get_calendars::get_calendars_controller,
calendar::get_calendars::get_calendars_admin_controller,
calendar::get_calendars_by_meta::get_calendars_by_meta_controller,
calendar::get_calendar::get_calendar_controller,
calendar::get_calendar::get_calendar_admin_controller,
calendar::delete_calendar::delete_calendar_controller,
calendar::delete_calendar::delete_calendar_admin_controller,
calendar::update_calendar::update_calendar_controller,
calendar::update_calendar::update_calendar_admin_controller,
calendar::get_calendar_events::get_calendar_events_controller,
calendar::get_calendar_events::get_calendar_events_admin_controller,
calendar::get_google_calendars::get_google_calendars_controller,
calendar::get_google_calendars::get_google_calendars_admin_controller,
calendar::get_outlook_calendars::get_outlook_calendars_controller,
calendar::get_outlook_calendars::get_outlook_calendars_admin_controller,
calendar::remove_sync_calendar::remove_sync_calendar_admin_controller,
calendar::add_sync_calendar::add_sync_calendar_admin_controller,
// Event
event::create_event::create_event_controller,
event::create_event::create_event_admin_controller,
event::delete_event::delete_event_controller,
event::delete_event::delete_event_admin_controller,
event::delete_many_events::delete_many_events_admin_controller,
event::get_event::get_event_controller,
event::get_event::get_event_admin_controller,
event::get_event_by_external_id::get_event_by_external_id_admin_controller,
event::get_event_instances::get_event_instances_controller,
event::get_event_instances::get_event_instances_admin_controller,
event::get_events_by_calendars::get_events_by_calendars_controller,
event::get_events_by_meta::get_events_by_meta_controller,
event::search_events::search_events_controller,
event::update_event::update_event_controller,
event::update_event::update_event_admin_controller,
// User
user::create_user::create_user_controller,
user::get_me::get_me_controller,
user::get_user::get_user_controller,
user::get_user_by_external_id::get_user_by_external_id_controller,
user::get_multiple_users_freebusy::get_multiple_freebusy_controller,
user::get_user_freebusy::get_freebusy_controller,
user::update_user::update_user_controller,
user::delete_user::delete_user_controller,
user::oauth_integration::oauth_integration_controller,
user::remove_integration::remove_integration_controller,
user::oauth_integration::oauth_integration_admin_controller,
user::remove_integration::remove_integration_admin_controller,
),
)]
struct ApiDoc;
struct SecurityAddon;
impl Modify for SecurityAddon {
fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
if let Some(components) = openapi.components.as_mut() {
components.add_security_scheme(
"api_key",
SecurityScheme::ApiKey(ApiKey::Header(ApiKeyValue::new("x-api-key"))),
)
}
}
}