lib.rs

  1pub mod api;
  2pub mod auth;
  3pub mod db;
  4pub mod env;
  5pub mod executor;
  6#[cfg(test)]
  7mod integration_tests;
  8pub mod rpc;
  9
 10use axum::{http::StatusCode, response::IntoResponse};
 11use db::Database;
 12use serde::Deserialize;
 13use std::{path::PathBuf, sync::Arc};
 14
 15pub type Result<T, E = Error> = std::result::Result<T, E>;
 16
 17pub enum Error {
 18    Http(StatusCode, String),
 19    Database(sea_orm::error::DbErr),
 20    Internal(anyhow::Error),
 21}
 22
 23impl From<anyhow::Error> for Error {
 24    fn from(error: anyhow::Error) -> Self {
 25        Self::Internal(error)
 26    }
 27}
 28
 29impl From<sea_orm::error::DbErr> for Error {
 30    fn from(error: sea_orm::error::DbErr) -> Self {
 31        Self::Database(error)
 32    }
 33}
 34
 35impl From<axum::Error> for Error {
 36    fn from(error: axum::Error) -> Self {
 37        Self::Internal(error.into())
 38    }
 39}
 40
 41impl From<hyper::Error> for Error {
 42    fn from(error: hyper::Error) -> Self {
 43        Self::Internal(error.into())
 44    }
 45}
 46
 47impl From<serde_json::Error> for Error {
 48    fn from(error: serde_json::Error) -> Self {
 49        Self::Internal(error.into())
 50    }
 51}
 52
 53impl IntoResponse for Error {
 54    fn into_response(self) -> axum::response::Response {
 55        match self {
 56            Error::Http(code, message) => (code, message).into_response(),
 57            Error::Database(error) => {
 58                (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response()
 59            }
 60            Error::Internal(error) => {
 61                (StatusCode::INTERNAL_SERVER_ERROR, format!("{}", &error)).into_response()
 62            }
 63        }
 64    }
 65}
 66
 67impl std::fmt::Debug for Error {
 68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 69        match self {
 70            Error::Http(code, message) => (code, message).fmt(f),
 71            Error::Database(error) => error.fmt(f),
 72            Error::Internal(error) => error.fmt(f),
 73        }
 74    }
 75}
 76
 77impl std::fmt::Display for Error {
 78    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 79        match self {
 80            Error::Http(code, message) => write!(f, "{code}: {message}"),
 81            Error::Database(error) => error.fmt(f),
 82            Error::Internal(error) => error.fmt(f),
 83        }
 84    }
 85}
 86
 87impl std::error::Error for Error {}
 88
 89#[derive(Default, Deserialize)]
 90pub struct Config {
 91    pub http_port: u16,
 92    pub database_url: String,
 93    pub api_token: String,
 94    pub invite_link_prefix: String,
 95    pub live_kit_server: Option<String>,
 96    pub live_kit_key: Option<String>,
 97    pub live_kit_secret: Option<String>,
 98    pub rust_log: Option<String>,
 99    pub log_json: Option<bool>,
100}
101
102#[derive(Default, Deserialize)]
103pub struct MigrateConfig {
104    pub database_url: String,
105    pub migrations_path: Option<PathBuf>,
106}
107
108pub struct AppState {
109    pub db: Arc<Database>,
110    pub live_kit_client: Option<Arc<dyn live_kit_server::api::Client>>,
111    pub config: Config,
112}
113
114impl AppState {
115    pub async fn new(config: Config) -> Result<Arc<Self>> {
116        let mut db_options = db::ConnectOptions::new(config.database_url.clone());
117        db_options.max_connections(5);
118        let db = Database::new(db_options).await?;
119        let live_kit_client = if let Some(((server, key), secret)) = config
120            .live_kit_server
121            .as_ref()
122            .zip(config.live_kit_key.as_ref())
123            .zip(config.live_kit_secret.as_ref())
124        {
125            Some(Arc::new(live_kit_server::api::LiveKitClient::new(
126                server.clone(),
127                key.clone(),
128                secret.clone(),
129            )) as Arc<dyn live_kit_server::api::Client>)
130        } else {
131            None
132        };
133
134        let this = Self {
135            db: Arc::new(db),
136            live_kit_client,
137            config,
138        };
139        Ok(Arc::new(this))
140    }
141}