lib.rs

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