#[cfg(all(feature = "backend_actix-web", feature = "backend_poem"))]
compile_error!(
"feature \"backend_actix-web\" and feature \"backend_poem\" cannot be enabled at the same time"
);
#[cfg(all(feature = "database_sqlite", feature = "database_postgres"))]
compile_error!(
"feature \"database_sqlite\" and feature \"database_postgres\" cannot be enabled at the same time"
);
mod util;
pub use util::*;
#[macro_use]
extern crate diesel;
#[cfg(feature = "plugin_auth")]
pub mod auth;
#[cfg(feature = "plugin_tasks")]
pub mod tasks;
#[cfg(all(feature = "plugin_dev", debug_assertions))]
pub mod dev;
#[cfg(all(feature = "plugin_dev", debug_assertions))]
pub use dev::setup_development;
mod database;
pub use database::{Connection, Database, Pool};
#[cfg(feature = "backend_poem")]
mod logger;
#[allow(deprecated)] #[cfg(feature = "backend_poem")]
pub use logger::Logger as PoemLogger;
#[cfg(feature = "plugin_storage")]
mod storage;
#[cfg(feature = "plugin_storage")]
pub use storage::{Attachment, AttachmentBlob, AttachmentData, Storage};
mod mailer;
pub use mailer::Mailer;
#[cfg(feature = "plugin_auth")]
pub use mailer::{DefaultMailTemplates, EmailTemplates};
#[derive(Clone)]
pub struct AppConfig {
pub app_url: String,
}
#[derive(Clone)]
pub struct AppData {
pub mailer: Mailer,
pub database: Database,
#[cfg(feature = "plugin_storage")]
pub storage: Storage,
}
#[cfg(feature = "plugin_auth")]
impl AppData {
pub fn with_custom_email_templates<T: EmailTemplates + 'static>(
mut self,
templates: T,
) -> Self {
self.mailer = Mailer::new(Box::new(templates));
self
}
}
#[cfg(debug_assertions)]
fn load_env_vars() {
static START: std::sync::Once = std::sync::Once::new();
START.call_once(|| {
dotenv::dotenv().unwrap_or_else(|_| {
panic!("ERROR: Could not load environment variables from dotenv file");
});
});
}
pub fn setup() -> AppData {
#[cfg(debug_assertions)]
{
load_env_vars();
}
#[cfg(feature = "plugin_auth")]
if std::env::var("SECRET_KEY").is_err() {
panic!("No SECRET_KEY environment variable set!");
}
if std::env::var("DATABASE_URL").is_err() {
panic!("No DATABASE_URL environment variable set!");
}
AppData {
mailer: Mailer::default(),
database: Database::new(),
#[cfg(feature = "plugin_storage")]
storage: Storage::new(),
}
}
#[cfg(feature = "backend_poem")]
pub async fn not_found(_: poem::error::NotFoundError) -> poem::Response {
let json = serde_json::json!({
"success": false,
"message": "Invalid endpoint"
});
poem::Response::builder()
.status(poem::http::StatusCode::NOT_FOUND)
.header("Content-Type", "application/json")
.body(json.to_string())
}