Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wyatt-herkamp committed Oct 27, 2024
1 parent c2d3aa4 commit ca85bd4
Show file tree
Hide file tree
Showing 12 changed files with 718 additions and 751 deletions.
195 changes: 103 additions & 92 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ toml = "0.8"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
tracing-appender = "0.2"
utoipa = { version = "5.0.0-rc.0", features = [
"chrono",
"uuid",
"url",
"debug",
] }
utoipa = { version = "5.0.0", features = ["chrono", "uuid", "url", "debug"] }
rand = "0.8"

nr-core = { path = "crates/core" }
Expand Down Expand Up @@ -68,7 +63,9 @@ bytes = "1"
pin-project = "1"
url = "2"
## Hashing
digestible = { version = "0.2", features = ["base64"] }
digestible = { git = "https://github.com/wyatt-herkamp/digestible.git", features = [
"base64",
] }
digest = { version = "0.10", features = ["std", "alloc"] }
md-5 = "0.10"
sha1 = "0.10"
Expand Down
23 changes: 5 additions & 18 deletions crates/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,12 @@ pub mod validations {
std::borrow::Cow::Borrowed(stringify!($ty))
}
}
impl utoipa::__dev::SchemaReferences for $ty {
fn schemas(
schemas: &mut Vec<(
String,
utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>,
)>,
) {
schemas.extend([]);
}
}
};
(
$ty:ty, format = $format:ident
) => {
impl utoipa::__dev::ComposeSchema for $ty {
fn compose(
_: Vec<utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>>,
) -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
impl utoipa::PartialSchema for $ty {
fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
utoipa::openapi::ObjectBuilder::new()
.schema_type(utoipa::openapi::schema::SchemaType::new(
utoipa::openapi::schema::Type::String,
Expand All @@ -139,15 +127,14 @@ pub mod validations {
.into()
}
}

crate::utils::validations::schema_for_new_type_str!($ty);
};
(
$ty:ty, pattern = $pattern:literal
) => {
impl utoipa::__dev::ComposeSchema for $ty {
fn compose(
_: Vec<utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>>,
) -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
impl utoipa::PartialSchema for $ty {
fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
utoipa::openapi::ObjectBuilder::new()
.schema_type(utoipa::openapi::schema::SchemaType::new(
utoipa::openapi::schema::Type::String,
Expand Down
4 changes: 1 addition & 3 deletions nitro_repo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ http.workspace = true
mime.workspace = true
http-body-util.workspace = true
utoipa = { workspace = true, features = ["axum_extras"] }
utoipa-scalar = { version = "0.2.0-alpha", features = [
"axum",
], optional = true }
utoipa-scalar = { version = "0.2.0", features = ["axum"], optional = true }
async-trait = "0.1"
# TLS
tokio-rustls = "0.26"
Expand Down
3 changes: 2 additions & 1 deletion nitro_repo/src/app/authentication/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ impl Debug for SessionManager {
}
}
impl SessionManager {
pub fn new(session_config: SessionManagerConfig, mode: Mode) -> Result<Self, Error> {
pub fn new(session_config: Option<SessionManagerConfig>, mode: Mode) -> Result<Self, Error> {
let session_config = session_config.unwrap_or_default();
let sessions = if session_config.database_location.exists() {
let database = Database::open(&session_config.database_location)?;
if mode == Mode::Debug {
Expand Down
46 changes: 14 additions & 32 deletions nitro_repo/src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,21 @@ impl Default for PasswordRules {
}
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
#[serde(default)]
pub struct NitroRepoConfig {
pub database: PostgresSettings,
pub log: LoggingConfig,
pub bind_address: String,
pub max_upload: ConfigSize,
pub database: Option<PostgresSettings>,
pub log: Option<LoggingConfig>,
pub bind_address: Option<String>,
pub max_upload: Option<ConfigSize>,
pub server_workers: Option<usize>,
pub mode: Mode,
pub sessions: SessionManagerConfig,
pub mode: Option<Mode>,
pub sessions: Option<SessionManagerConfig>,
pub tls: Option<TlsConfig>,
pub email: EmailSetting,
pub site: SiteSetting,
pub security: SecuritySettings,
pub staging_config: StagingConfig,
pub email: Option<EmailSetting>,
pub site: Option<SiteSetting>,
pub security: Option<SecuritySettings>,
pub staging: Option<StagingConfig>,
}

impl NitroRepoConfig {
Expand All @@ -116,28 +116,10 @@ impl NitroRepoConfig {
Ok(app)
}
}

impl Default for NitroRepoConfig {
fn default() -> Self {
Self {
database: PostgresSettings::default(),
log: LoggingConfig::default(),
bind_address: "0.0.0.0:6742".to_string(),
max_upload: ConfigSize {
size: 1024,
unit: Unit::Mebibytes,
},
server_workers: None,
mode: Mode::default(),
tls: None,
sessions: SessionManagerConfig::default(),
email: EmailSetting::default(),
site: SiteSetting::default(),
security: SecuritySettings::default(),
staging_config: StagingConfig::default(),
}
}
pub fn default_bind_address() -> String {
"0.0.0.0:6742".to_string()
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct PostgresSettings {
pub user: String,
Expand Down
11 changes: 11 additions & 0 deletions nitro_repo/src/app/email_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ type Transport = AsyncSmtpTransport<lettre::Tokio1Executor>;
#[derive(Debug)]
pub struct EmailService;
impl EmailService {
pub fn no_email() -> EmailAccess {
let (sender, receiver) = flume::bounded(100);
tokio::spawn(async move {
Self::run_no_transport(receiver).await;
});
EmailAccess {
queue: sender,
message_builder: Message::builder(),
email_handlebars: Handlebars::new(),
}
}
pub async fn start(email: EmailSetting) -> io::Result<EmailAccess> {
let transport = Self::build_connection(email.clone()).await;

Expand Down
18 changes: 12 additions & 6 deletions nitro_repo/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use axum::extract::State;
use config::{Mode, PasswordRules, PostgresSettings, SecuritySettings, SiteSetting};
use derive_more::{derive::Deref, AsRef, Into};
use email::EmailSetting;
use email_service::EmailAccess;
use email_service::{EmailAccess, EmailService};
use http::Uri;
use nr_core::{
database::{
Expand Down Expand Up @@ -130,14 +130,16 @@ impl NitroRepo {
pub async fn new(
mode: Mode,
site: SiteSetting,
security: SecuritySettings,
session_manager: SessionManagerConfig,
staging_config: StagingConfig,
email_settings: EmailSetting,
security: Option<SecuritySettings>,
session_manager: Option<SessionManagerConfig>,
staging_config: Option<StagingConfig>,
email_settings: Option<EmailSetting>,
database: PostgresSettings,
) -> anyhow::Result<Self> {
let database = Self::load_database(database).await?;
let is_installed = user_utils::does_user_exist(&database).await?;
let security = security.unwrap_or_default();
let staging_config = staging_config.unwrap_or_default();
let instance = Instance {
mode,
version: current_semver!(),
Expand All @@ -148,7 +150,11 @@ impl NitroRepo {
is_https: site.is_https,
password_rules: security.password_rules.clone(),
};
let email_service = email_service::EmailService::start(email_settings).await?;
let email_service = if let Some(email_service) = email_settings {
EmailService::start(email_service).await?
} else {
EmailService::no_email()
};
let session_manager = SessionManager::new(session_manager, mode)?;

let nitro_repo = NitroRepoInner {
Expand Down
17 changes: 13 additions & 4 deletions nitro_repo/src/app/web.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::authentication::api_middleware::AuthenticationLayer;
use super::logging::request_tracing::NitroRepoTracing;
use super::NitroRepo;
use super::{api, config::NitroRepoConfig};
use super::{config, NitroRepo};

use anyhow::Context;
use axum::extract::DefaultBodyLimit;
Expand Down Expand Up @@ -37,13 +37,22 @@ pub(crate) async fn start(config_path: PathBuf, add_defaults: bool) -> anyhow::R
mode,
sessions,
tls,
staging_config,
staging: staging_config,
site,
security,
email,
..
} = NitroRepoConfig::load(config_path, add_defaults)?;
log.init(mode)?;
let bind_address = bind_address.unwrap_or_else(config::default_bind_address);
let max_upload = max_upload
.map(|size| size.get_as_bytes())
.unwrap_or(100 * 1024 * 1024);
let Some(database) = database else {
return Err(anyhow::anyhow!("Database Settings are Required"));
};
let mode = mode.unwrap_or_default();
let site = site.unwrap_or_default();
log.unwrap_or_default().init(mode)?;
let tls = tls
.map(|tls| {
rustls_server_config(tls.private_key, tls.certificate_chain)
Expand Down Expand Up @@ -94,7 +103,7 @@ pub(crate) async fn start(config_path: PathBuf, add_defaults: bool) -> anyhow::R
))
.layer(NitroRepoTracing::new_trace_layer())
.layer(PropagateRequestIdLayer::new(REQUEST_ID_HEADER))
.layer(DefaultBodyLimit::max(max_upload.get_as_bytes()))
.layer(DefaultBodyLimit::max(max_upload))
.layer(SetRequestIdLayer::new(REQUEST_ID_HEADER, MakeRequestUuid))
.layer(auth_layer);
if let Some(tls) = tls {
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "beta"
channel = "stable"
Loading

0 comments on commit ca85bd4

Please sign in to comment.