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 api_token: String,
 95    pub invite_link_prefix: String,
 96    pub live_kit_server: Option<String>,
 97    pub live_kit_key: Option<String>,
 98    pub live_kit_secret: Option<String>,
 99    pub rust_log: Option<String>,
100    pub log_json: Option<bool>,
101    pub zed_environment: String,
102}
103
104#[derive(Default, Deserialize)]
105pub struct MigrateConfig {
106    pub database_url: String,
107    pub migrations_path: Option<PathBuf>,
108}
109
110pub struct AppState {
111    pub db: Arc<Database>,
112    pub live_kit_client: Option<Arc<dyn live_kit_server::api::Client>>,
113    pub config: Config,
114}
115
116impl AppState {
117    pub async fn new(config: Config) -> Result<Arc<Self>> {
118        let mut db_options = db::ConnectOptions::new(config.database_url.clone());
119        db_options.max_connections(5);
120        let db = Database::new(db_options).await?;
121        let live_kit_client = if let Some(((server, key), secret)) = config
122            .live_kit_server
123            .as_ref()
124            .zip(config.live_kit_key.as_ref())
125            .zip(config.live_kit_secret.as_ref())
126        {
127            Some(Arc::new(live_kit_server::api::LiveKitClient::new(
128                server.clone(),
129                key.clone(),
130                secret.clone(),
131            )) as Arc<dyn live_kit_server::api::Client>)
132        } else {
133            None
134        };
135
136        let this = Self {
137            db: Arc::new(db),
138            live_kit_client,
139            config,
140        };
141        Ok(Arc::new(this))
142    }
143}