1use anyhow::anyhow;
2use axum::headers::HeaderMapExt;
3use axum::{
4 extract::MatchedPath,
5 http::{Request, Response},
6 routing::get,
7 Extension, Router,
8};
9use collab::api::billing::sync_llm_usage_with_stripe_periodically;
10use collab::api::CloudflareIpCountryHeader;
11use collab::llm::{db::LlmDatabase, log_usage_periodically};
12use collab::migrations::run_database_migrations;
13use collab::user_backfiller::spawn_user_backfiller;
14use collab::{api::billing::poll_stripe_events_periodically, llm::LlmState, ServiceMode};
15use collab::{
16 api::fetch_extensions_from_blob_store_periodically, db, env, executor::Executor,
17 rpc::ResultExt, AppState, Config, RateLimiter, Result,
18};
19use db::Database;
20use std::{
21 env::args,
22 net::{SocketAddr, TcpListener},
23 path::Path,
24 sync::Arc,
25 time::Duration,
26};
27#[cfg(unix)]
28use tokio::signal::unix::SignalKind;
29use tower_http::trace::TraceLayer;
30use tracing_subscriber::{
31 filter::EnvFilter, fmt::format::JsonFields, util::SubscriberInitExt, Layer,
32};
33use util::{maybe, ResultExt as _};
34
35const VERSION: &str = env!("CARGO_PKG_VERSION");
36const REVISION: Option<&'static str> = option_env!("GITHUB_SHA");
37
38#[tokio::main]
39async fn main() -> Result<()> {
40 if let Err(error) = env::load_dotenv() {
41 eprintln!(
42 "error loading .env.toml (this is expected in production): {}",
43 error
44 );
45 }
46
47 let mut args = args().skip(1);
48 match args.next().as_deref() {
49 Some("version") => {
50 println!("collab v{} ({})", VERSION, REVISION.unwrap_or("unknown"));
51 }
52 Some("migrate") => {
53 let config = envy::from_env::<Config>().expect("error loading config");
54 setup_app_database(&config).await?;
55 }
56 Some("seed") => {
57 let config = envy::from_env::<Config>().expect("error loading config");
58 let db_options = db::ConnectOptions::new(config.database_url.clone());
59
60 let mut db = Database::new(db_options, Executor::Production).await?;
61 db.initialize_notification_kinds().await?;
62
63 collab::seed::seed(&config, &db, false).await?;
64
65 if let Some(llm_database_url) = config.llm_database_url.clone() {
66 let db_options = db::ConnectOptions::new(llm_database_url);
67 let mut db = LlmDatabase::new(db_options.clone(), Executor::Production).await?;
68 db.initialize().await?;
69 collab::llm::db::seed_database(&config, &mut db, true).await?;
70 }
71 }
72 Some("serve") => {
73 let mode = match args.next().as_deref() {
74 Some("collab") => ServiceMode::Collab,
75 Some("api") => ServiceMode::Api,
76 Some("llm") => ServiceMode::Llm,
77 Some("all") => ServiceMode::All,
78 _ => {
79 return Err(anyhow!(
80 "usage: collab <version | migrate | seed | serve <api|collab|llm|all>>"
81 ))?;
82 }
83 };
84
85 let config = envy::from_env::<Config>().expect("error loading config");
86 init_tracing(&config);
87 let mut app = Router::new()
88 .route("/", get(handle_root))
89 .route("/healthz", get(handle_liveness_probe))
90 .layer(Extension(mode));
91
92 let listener = TcpListener::bind(format!("0.0.0.0:{}", config.http_port))
93 .expect("failed to bind TCP listener");
94
95 let mut on_shutdown = None;
96
97 if mode.is_llm() {
98 setup_llm_database(&config).await?;
99
100 let state = LlmState::new(config.clone(), Executor::Production).await?;
101
102 log_usage_periodically(state.clone());
103
104 app = app
105 .merge(collab::llm::routes())
106 .layer(Extension(state.clone()));
107 }
108
109 if mode.is_collab() || mode.is_api() {
110 setup_app_database(&config).await?;
111
112 let state = AppState::new(config, Executor::Production).await?;
113
114 if let Some(stripe_billing) = state.stripe_billing.clone() {
115 let executor = state.executor.clone();
116 executor.spawn_detached(async move {
117 stripe_billing.initialize().await.trace_err();
118 });
119 }
120
121 if mode.is_collab() {
122 state.db.purge_old_embeddings().await.trace_err();
123 RateLimiter::save_periodically(
124 state.rate_limiter.clone(),
125 state.executor.clone(),
126 );
127
128 let epoch = state
129 .db
130 .create_server(&state.config.zed_environment)
131 .await?;
132 let rpc_server = collab::rpc::Server::new(epoch, state.clone());
133 rpc_server.start().await?;
134
135 app = app
136 .merge(collab::api::routes(rpc_server.clone()))
137 .merge(collab::rpc::routes(rpc_server.clone()));
138
139 on_shutdown = Some(Box::new(move || rpc_server.teardown()));
140 }
141
142 if mode.is_api() {
143 poll_stripe_events_periodically(state.clone());
144 fetch_extensions_from_blob_store_periodically(state.clone());
145 spawn_user_backfiller(state.clone());
146
147 let llm_db = maybe!(async {
148 let database_url = state
149 .config
150 .llm_database_url
151 .as_ref()
152 .ok_or_else(|| anyhow!("missing LLM_DATABASE_URL"))?;
153 let max_connections = state
154 .config
155 .llm_database_max_connections
156 .ok_or_else(|| anyhow!("missing LLM_DATABASE_MAX_CONNECTIONS"))?;
157
158 let mut db_options = db::ConnectOptions::new(database_url);
159 db_options.max_connections(max_connections);
160 LlmDatabase::new(db_options, state.executor.clone()).await
161 })
162 .await
163 .trace_err();
164
165 if let Some(mut llm_db) = llm_db {
166 llm_db.initialize().await?;
167 sync_llm_usage_with_stripe_periodically(state.clone());
168 }
169
170 app = app
171 .merge(collab::api::events::router())
172 .merge(collab::api::extensions::router())
173 }
174
175 app = app.layer(Extension(state.clone()));
176 }
177
178 app = app.layer(
179 TraceLayer::new_for_http()
180 .make_span_with(|request: &Request<_>| {
181 let matched_path = request
182 .extensions()
183 .get::<MatchedPath>()
184 .map(MatchedPath::as_str);
185
186 let geoip_country_code = request
187 .headers()
188 .typed_get::<CloudflareIpCountryHeader>()
189 .map(|header| header.to_string());
190
191 tracing::info_span!(
192 "http_request",
193 method = ?request.method(),
194 matched_path,
195 geoip_country_code,
196 user_id = tracing::field::Empty,
197 login = tracing::field::Empty,
198 authn.jti = tracing::field::Empty,
199 is_staff = tracing::field::Empty
200 )
201 })
202 .on_response(
203 |response: &Response<_>, latency: Duration, _: &tracing::Span| {
204 let duration_ms = latency.as_micros() as f64 / 1000.;
205 tracing::info!(
206 duration_ms,
207 status = response.status().as_u16(),
208 "finished processing request"
209 );
210 },
211 ),
212 );
213
214 #[cfg(unix)]
215 let signal = async move {
216 let mut sigterm = tokio::signal::unix::signal(SignalKind::terminate())
217 .expect("failed to listen for interrupt signal");
218 let mut sigint = tokio::signal::unix::signal(SignalKind::interrupt())
219 .expect("failed to listen for interrupt signal");
220 let sigterm = sigterm.recv();
221 let sigint = sigint.recv();
222 futures::pin_mut!(sigterm, sigint);
223 futures::future::select(sigterm, sigint).await;
224 };
225
226 #[cfg(windows)]
227 let signal = async move {
228 // todo(windows):
229 // `ctrl_close` does not work well, because tokio's signal handler always returns soon,
230 // but system terminates the application soon after returning CTRL+CLOSE handler.
231 // So we should implement blocking handler to treat CTRL+CLOSE signal.
232 let mut ctrl_break = tokio::signal::windows::ctrl_break()
233 .expect("failed to listen for interrupt signal");
234 let mut ctrl_c = tokio::signal::windows::ctrl_c()
235 .expect("failed to listen for interrupt signal");
236 let ctrl_break = ctrl_break.recv();
237 let ctrl_c = ctrl_c.recv();
238 futures::pin_mut!(ctrl_break, ctrl_c);
239 futures::future::select(ctrl_break, ctrl_c).await;
240 };
241
242 axum::Server::from_tcp(listener)
243 .map_err(|e| anyhow!(e))?
244 .serve(app.into_make_service_with_connect_info::<SocketAddr>())
245 .with_graceful_shutdown(async move {
246 signal.await;
247 tracing::info!("Received interrupt signal");
248
249 if let Some(on_shutdown) = on_shutdown {
250 on_shutdown();
251 }
252 })
253 .await
254 .map_err(|e| anyhow!(e))?;
255 }
256 _ => {
257 Err(anyhow!(
258 "usage: collab <version | migrate | seed | serve <api|collab|llm|all>>"
259 ))?;
260 }
261 }
262 Ok(())
263}
264
265async fn setup_app_database(config: &Config) -> Result<()> {
266 let db_options = db::ConnectOptions::new(config.database_url.clone());
267 let mut db = Database::new(db_options, Executor::Production).await?;
268
269 let migrations_path = config.migrations_path.as_deref().unwrap_or_else(|| {
270 #[cfg(feature = "sqlite")]
271 let default_migrations = concat!(env!("CARGO_MANIFEST_DIR"), "/migrations.sqlite");
272 #[cfg(not(feature = "sqlite"))]
273 let default_migrations = concat!(env!("CARGO_MANIFEST_DIR"), "/migrations");
274
275 Path::new(default_migrations)
276 });
277
278 let migrations = run_database_migrations(db.options(), migrations_path).await?;
279 for (migration, duration) in migrations {
280 log::info!(
281 "Migrated {} {} {:?}",
282 migration.version,
283 migration.description,
284 duration
285 );
286 }
287
288 db.initialize_notification_kinds().await?;
289
290 if config.seed_path.is_some() {
291 collab::seed::seed(config, &db, false).await?;
292 }
293
294 Ok(())
295}
296
297async fn setup_llm_database(config: &Config) -> Result<()> {
298 let database_url = config
299 .llm_database_url
300 .as_ref()
301 .ok_or_else(|| anyhow!("missing LLM_DATABASE_URL"))?;
302
303 let db_options = db::ConnectOptions::new(database_url.clone());
304 let db = LlmDatabase::new(db_options, Executor::Production).await?;
305
306 let migrations_path = config
307 .llm_database_migrations_path
308 .as_deref()
309 .unwrap_or_else(|| {
310 #[cfg(feature = "sqlite")]
311 let default_migrations = concat!(env!("CARGO_MANIFEST_DIR"), "/migrations_llm.sqlite");
312 #[cfg(not(feature = "sqlite"))]
313 let default_migrations = concat!(env!("CARGO_MANIFEST_DIR"), "/migrations_llm");
314
315 Path::new(default_migrations)
316 });
317
318 let migrations = run_database_migrations(db.options(), migrations_path).await?;
319 for (migration, duration) in migrations {
320 log::info!(
321 "Migrated {} {} {:?}",
322 migration.version,
323 migration.description,
324 duration
325 );
326 }
327
328 Ok(())
329}
330
331async fn handle_root(Extension(mode): Extension<ServiceMode>) -> String {
332 format!("zed:{mode} v{VERSION} ({})", REVISION.unwrap_or("unknown"))
333}
334
335async fn handle_liveness_probe(
336 app_state: Option<Extension<Arc<AppState>>>,
337 llm_state: Option<Extension<Arc<LlmState>>>,
338) -> Result<String> {
339 if let Some(state) = app_state {
340 state.db.get_all_users(0, 1).await?;
341 }
342
343 if let Some(llm_state) = llm_state {
344 llm_state.db.list_providers().await?;
345 }
346
347 Ok("ok".to_string())
348}
349
350pub fn init_tracing(config: &Config) -> Option<()> {
351 use std::str::FromStr;
352 use tracing_subscriber::layer::SubscriberExt;
353
354 let filter = EnvFilter::from_str(config.rust_log.as_deref()?).log_err()?;
355
356 tracing_subscriber::registry()
357 .with(if config.log_json.unwrap_or(false) {
358 Box::new(
359 tracing_subscriber::fmt::layer()
360 .fmt_fields(JsonFields::default())
361 .event_format(
362 tracing_subscriber::fmt::format()
363 .json()
364 .flatten_event(true)
365 .with_span_list(false),
366 )
367 .with_filter(filter),
368 ) as Box<dyn Layer<_> + Send + Sync>
369 } else {
370 Box::new(
371 tracing_subscriber::fmt::layer()
372 .event_format(tracing_subscriber::fmt::format().pretty())
373 .with_filter(filter),
374 )
375 })
376 .init();
377
378 None
379}