-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split apart some parts of app/mod.rs
- Loading branch information
1 parent
ab6142d
commit 0bf8e61
Showing
17 changed files
with
188 additions
and
160 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use sqlx::{postgres::PgPoolOptions, PgPool}; | ||
use tracing::info; | ||
|
||
use crate::app::App; | ||
|
||
impl App { | ||
pub(super) async fn setup_db() -> color_eyre::Result<PgPool> { | ||
info!("SQLx: Connecting to the database..."); | ||
|
||
let database_url = match std::env::var("DATABASE_PRIVATE_URL") { | ||
Ok(url) => { | ||
info!("SQLx: Using DATABASE_PRIVATE_URL"); | ||
url | ||
} | ||
Err(_) => { | ||
info!("SQLx: Using DATABASE_URL"); | ||
std::env::var("DATABASE_URL")? | ||
} | ||
}; | ||
|
||
let pool = PgPoolOptions::new() | ||
.max_connections(5) | ||
.connect(&database_url) | ||
.await?; | ||
|
||
info!("SQLx: Connected to the database"); | ||
|
||
sqlx::migrate!().run(&pool).await?; | ||
|
||
info!("SQLx: Migrations run"); | ||
|
||
Ok(pool) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use utoipa::{ | ||
openapi::security::{ApiKey, ApiKeyValue, SecurityScheme}, | ||
Modify, OpenApi, | ||
}; | ||
|
||
pub const AUTH_TAG: &str = "Auth"; | ||
pub const MONITORING_TAG: &str = "Monitoring"; | ||
pub const USER_TAG: &str = "User"; | ||
pub const SYSTEM_TAG: &str = "System"; | ||
pub const DATA_TAG: &str = "Data"; | ||
pub const PUBLIC_SYSTEM_TAG: &str = "Public systems"; | ||
|
||
#[derive(OpenApi)] | ||
#[openapi( | ||
modifiers(&ApiDocSecurityAddon), | ||
tags( | ||
(name = AUTH_TAG, description = "Endpoints to authenticate users"), | ||
(name = MONITORING_TAG, description = "Endpoints to monitor the system (such as healthchecks)"), | ||
(name = USER_TAG, description = "Endpoints related to users and their accounts"), | ||
(name = SYSTEM_TAG, description = "Endpoints related to monitored systems"), | ||
(name = DATA_TAG, description = "Endpoints that must be connected to by the monitored systems"), | ||
(name = PUBLIC_SYSTEM_TAG, description = "Endpoints related to monitored systems that are public") | ||
) | ||
)] | ||
pub(super) struct ApiDoc; | ||
|
||
struct ApiDocSecurityAddon; | ||
|
||
impl Modify for ApiDocSecurityAddon { | ||
fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) { | ||
if let Some(components) = openapi.components.as_mut() { | ||
components.add_security_scheme( | ||
"session", | ||
SecurityScheme::ApiKey(ApiKey::Cookie(ApiKeyValue::new("monitor_id"))), | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use sidekiq::RedisConnectionManager; | ||
use tower_sessions_redis_store::fred::{ | ||
prelude::{ClientLike, RedisConfig as RedisFredConfig, RedisPool as RedisFredPool}, | ||
types::ReconnectPolicy, | ||
}; | ||
use tracing::info; | ||
|
||
use crate::app::App; | ||
|
||
pub type RedisLibPool = bb8::Pool<RedisConnectionManager>; | ||
|
||
impl App { | ||
pub(super) async fn setup_redis_lib() -> color_eyre::Result<RedisLibPool> { | ||
info!("Redis Lib: Connecting to Redis (to manage workers)..."); | ||
|
||
let db_num = 1u8; | ||
|
||
let redis_url = std::env::var("REDIS_URL")?; | ||
let redis_url = format!("{}/{}", redis_url, db_num); | ||
let manager = RedisConnectionManager::new(redis_url)?; | ||
let redis = bb8::Pool::builder().build(manager).await?; | ||
|
||
info!("Redis Lib: Connected to Redis (to manage workers)"); | ||
|
||
Ok(redis) | ||
} | ||
|
||
pub(super) async fn setup_redis_fred() -> color_eyre::Result<RedisFredPool> { | ||
info!("Redis Fred: Connecting to Redis (to manage sessions)..."); | ||
|
||
let db_num = 0u8; | ||
|
||
let redis_url = std::env::var("REDIS_URL")?; | ||
let redis_url = format!("{}/{}", redis_url, db_num); | ||
|
||
let config = RedisFredConfig::from_url(&redis_url)?; | ||
|
||
let pool = RedisFredPool::new(config, None, None, Some(ReconnectPolicy::default()), 6)?; | ||
|
||
pool.init().await?; | ||
|
||
info!("Redis Fred: Connected to Redis (to manage sessions)"); | ||
|
||
Ok(pool) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use sidekiq::{periodic, Processor}; | ||
use sqlx::PgPool; | ||
use tracing::info; | ||
|
||
use crate::{ | ||
app::{redis::RedisLibPool, App}, | ||
workers::{email_worker::SmtpClient, register_workers}, | ||
PRODUCTION, | ||
}; | ||
|
||
impl App { | ||
pub(super) async fn start_workers(p: Processor) -> color_eyre::Result<()> { | ||
info!( | ||
"Sidekiq: Workers started in {} mode", | ||
if *PRODUCTION { | ||
"production" | ||
} else { | ||
"development" | ||
} | ||
); | ||
|
||
// Start the server | ||
p.run().await; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(super) async fn init_workers( | ||
redis: RedisLibPool, | ||
db: PgPool, | ||
smtp_client: SmtpClient, | ||
) -> color_eyre::Result<Processor> { | ||
// Clear out all periodic jobs and their schedules | ||
periodic::destroy_all(redis.clone()).await?; | ||
|
||
// Sidekiq server | ||
let mut p = Processor::new(redis, vec!["down_emails".to_string()]); | ||
|
||
// Add known workers | ||
register_workers(&mut p, db, smtp_client).await?; | ||
|
||
Ok(p) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.