headless_lms_server/
lib.rspub mod config;
pub mod controllers;
pub mod domain;
pub mod prelude;
pub mod programs;
#[cfg(test)]
pub mod test_helper;
#[cfg(all(test, feature = "ts_rs"))]
pub mod ts_binding_generator;
#[macro_use]
extern crate tracing;
#[macro_use]
extern crate doc_macro;
use anyhow::Result;
use headless_lms_utils::file_store::{
google_cloud_file_store::GoogleCloudFileStore, local_file_store::LocalFileStore, FileStore,
};
use oauth2::{EndpointNotSet, EndpointSet};
use std::{env, sync::Arc};
use tracing_error::ErrorLayer;
use tracing_log::LogTracer;
use tracing_subscriber::{layer::SubscriberExt, EnvFilter};
pub type OAuthClient = oauth2::basic::BasicClient<
EndpointSet,
EndpointNotSet,
EndpointNotSet,
EndpointNotSet,
EndpointSet,
>;
pub fn setup_tracing() -> Result<()> {
let subscriber = tracing_subscriber::Registry::default()
.with(
tracing_subscriber::fmt::layer()
.event_format(tracing_subscriber::fmt::format().compact()),
)
.with(ErrorLayer::default())
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")));
tracing::subscriber::set_global_default(subscriber)?;
LogTracer::init()?;
Ok(())
}
pub fn setup_file_store() -> Arc<dyn FileStore + Send + Sync> {
if env::var("FILE_STORE_USE_GOOGLE_CLOUD_STORAGE").is_ok() {
info!("Using Google Cloud Storage as the file store");
let bucket_name = env::var("GOOGLE_CLOUD_STORAGE_BUCKET_NAME").expect("env FILE_STORE_USE_GOOGLE_CLOUD_STORAGE was defined but GOOGLE_CLOUD_STORAGE_BUCKET_NAME was not.");
Arc::new(GoogleCloudFileStore::new(bucket_name).expect("Failed to initialize file store"))
} else {
info!("Using local file storage as the file store");
Arc::new(
LocalFileStore::new(
"uploads".into(),
"http://project-331.local/api/v0/files/uploads/".into(),
)
.expect("Failed to initialize file store"),
)
}
}
#[macro_export]
macro_rules! generated_docs {
($t: expr, ts) => {
concat!(
"## Response TypeScript definition\n",
"```ts\n",
include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/generated-docs/",
stringify!($t),
".ts"
)),
"\n```\n",
)
};
($t: expr, json) => {
concat!(
"## Example response\n",
"```json\n",
include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/generated-docs/",
stringify!($t),
".json"
)),
"\n```\n",
)
};
($t: expr) => {
concat!(generated_docs!($t, ts), generated_docs!($t, json),)
};
}