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