mod config; use std::convert::Infallible; use std::time::Duration; use axum::extract::{Path, State}; use axum::http::{header, StatusCode}; use axum::response::sse::{Event, KeepAlive, Sse}; use axum::response::{IntoResponse, Response}; use axum::routing::get; use axum::Router; use domain::JobEvent; use futures::stream::{self, StreamExt}; use leptos::config::get_configuration; use leptos::prelude::provide_context; use leptos_axum::{generate_route_list, LeptosRoutes}; use pipeline::Pipeline; use tokio_stream::wrappers::BroadcastStream; #[tokio::main] async fn main() -> anyhow::Result<()> { let _ = dotenvy::dotenv(); tracing_subscriber::fmt() .with_env_filter( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| "info,server=debug,pipeline=debug,providers=debug".into()), ) .init(); let pipeline = config::init_pipeline_from_env().await?; let conf = get_configuration(None)?; let leptos_options = conf.leptos_options; let addr = leptos_options.site_addr; let routes = generate_route_list(app::App); let pipe_for_ctx = pipeline.clone(); let app_router = Router::new() .route("/health/live", get(health_live)) .route("/health/ready", get(health_check)) .route("/api/health", get(health_check)) .route("/api/jobs/{id}/events", get(job_events_sse)) .route("/api/photos/{id}", get(serve_photo)) .with_state(pipeline) .leptos_routes_with_context( &leptos_options, routes, move || { provide_context(pipe_for_ctx.clone()); }, { let opts = leptos_options.clone(); move || app::shell(opts.clone()) }, ) .fallback(leptos_axum::file_and_error_handler(app::shell)) .with_state(leptos_options); tracing::info!("listening on http://{addr}"); let listener = tokio::net::TcpListener::bind(&addr).await?; axum::serve(listener, app_router.into_make_service()).await?; Ok(()) } async fn job_events_sse( State(pipeline): State, Path(job_id): Path, ) -> Sse>> { let (past, rx_opt) = prepare_job_subscription(pipeline.subscribe_job(&job_id).await); let already_done = past.iter().any(|e| e.is_terminal()); let past_stream = stream::iter(past).map(|ev| { let json = serde_json::to_string(&ev).unwrap_or_default(); Ok::<_, Infallible>(Event::default().data(json)) }); let live_stream = match rx_opt { Some(rx) if !already_done => BroadcastStream::new(rx) .filter_map(|item| async move { item.ok() }) .map(|ev| { let done = ev.is_terminal(); let json = serde_json::to_string(&ev).unwrap_or_default(); (done, Ok::<_, Infallible>(Event::default().data(json))) }) .scan(false, |finished, (done, ev)| { let emit = if *finished { None } else { Some(ev) }; if done { *finished = true; } futures::future::ready(emit) }) .left_stream(), _ => stream::empty().right_stream(), }; Sse::new(past_stream.chain(live_stream)).keep_alive( KeepAlive::new() .interval(Duration::from_secs(10)) .text("keep-alive"), ) } type JobSubscription = ( Vec, Option>, ); fn prepare_job_subscription(subscription: anyhow::Result) -> JobSubscription { let (mut past, receiver) = match subscription { Ok(subscription) => subscription, Err(error) => { tracing::warn!(%error, "could not subscribe to analysis job"); return ( vec![JobEvent::Error { message: "분석 진행 상태를 불러오지 못했습니다. 잠시 후 다시 시도해 주세요." .into(), }], None, ); } }; // A stored job may outlive its process. Replaying only its nonterminal history // would make EventSource reconnect forever, resetting the client's retry counter. if receiver.is_none() && !past.iter().any(JobEvent::is_terminal) { let message = if past.is_empty() { "분석 작업을 찾을 수 없습니다. 장소에서 분석을 다시 시작해 주세요." } else { "분석 작업이 중단됐습니다. 장소에서 분석을 다시 시작해 주세요." }; past.push(JobEvent::Error { message: message.into(), }); } (past, receiver) } async fn health_live() -> Response { ( StatusCode::OK, [(header::CONTENT_TYPE, "application/json")], r#"{"status":"ok"}"#, ) .into_response() } async fn health_check(State(pipeline): State) -> Response { let storage_ok = pipeline.store.health_check().await.is_ok(); let llm_providers = pipeline.llm.health_check_all().await; let any_llm_healthy = llm_providers.iter().any(|p| p.healthy); let status = if storage_ok && any_llm_healthy { StatusCode::OK } else { StatusCode::SERVICE_UNAVAILABLE }; let body = serde_json::json!({ "status": if status == StatusCode::OK { "ok" } else { "degraded" }, "storage_healthy": storage_ok, "llm_providers": llm_providers, }); ( status, [(header::CONTENT_TYPE, "application/json")], body.to_string(), ) .into_response() } async fn serve_photo(State(pipeline): State, Path(photo_id): Path) -> Response { let Ok(Some(photo)) = pipeline.store.get_photo(&photo_id).await else { return StatusCode::NOT_FOUND.into_response(); }; match pipeline.store.get_blob(&photo.object_key).await { Ok(bytes) => ( StatusCode::OK, [ (header::CONTENT_TYPE, "image/jpeg"), (header::CACHE_CONTROL, "public, max-age=86400"), ], bytes, ) .into_response(), Err(_) => StatusCode::NOT_FOUND.into_response(), } } #[cfg(test)] mod job_events_tests { use super::*; use axum::body::Body; use axum::http::Request; #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn ssr_routes_render_without_spawn_local_panic() { std::env::set_var("LEPTOS_OUTPUT_NAME", "make-space"); let store = storage::Store::connect(&storage::StorageConfig { mongodb_uri: "mongodb://127.0.0.1:1".into(), mongodb_db: "test".into(), minio_endpoint: "http://127.0.0.1:1".into(), minio_access_key: String::new(), minio_secret_key: String::new(), minio_bucket: "test".into(), }) .await .expect("in-memory store fallback"); let llm = llm::LlmClient::new("http://127.0.0.1:1", "", "test-model"); let pipeline = Pipeline::new( store, llm, pipeline::PipelineConfig { kakao_api_key: String::new(), ncp_client_id: String::new(), ncp_client_secret: String::new(), browser_scraper_url: None, max_photos_per_source: 5, max_photos_total: 10, }, ); let conf = get_configuration(None).expect("leptos config"); let leptos_options = conf.leptos_options; let routes = generate_route_list(app::App); let pipe_for_ctx = pipeline.clone(); let _app_router: Router<()> = Router::new() .route("/health/live", get(health_live)) .with_state(pipeline.clone()) .leptos_routes_with_context( &leptos_options, routes, move || { provide_context(pipe_for_ctx.clone()); }, { let opts = leptos_options.clone(); move || app::shell(opts.clone()) }, ) .fallback(leptos_axum::file_and_error_handler(app::shell)) .with_state(leptos_options.clone()); for uri in ["/", "/?q=test", "/recent", "/place/kakao%3A1", "/nonexistent-404-path"] { use axum::body::to_bytes; let pipe = pipeline.clone(); let opts = leptos_options.clone(); let handler = leptos_axum::render_app_to_stream_with_context( move || { provide_context(pipe.clone()); }, move || app::shell(opts.clone()), ); let req = Request::builder() .uri(uri) .header("Accept", "text/html") .body(Body::empty()) .unwrap(); let resp = handler(req).await; assert_eq!(resp.status(), StatusCode::OK); let bytes = to_bytes(resp.into_body(), usize::MAX).await.unwrap(); assert!(!bytes.is_empty()); } } #[test] fn missing_job_returns_a_terminal_error() { let (events, receiver) = prepare_job_subscription(Ok((Vec::new(), None))); assert!(receiver.is_none()); assert_eq!(events.len(), 1); assert!( matches!(&events[0], JobEvent::Error { message } if message.contains("찾을 수 없습니다")) ); } #[test] fn interrupted_job_keeps_history_then_terminates() { let past = vec![ JobEvent::Started { place_id: "place-1".into(), }, JobEvent::LayoutDraft { attempt: 1 }, ]; let (events, receiver) = prepare_job_subscription(Ok((past, None))); assert!(receiver.is_none()); assert_eq!(events.len(), 3); assert!(matches!(events[0], JobEvent::Started { .. })); assert!(matches!(events[1], JobEvent::LayoutDraft { .. })); assert!(matches!(&events[2], JobEvent::Error { message } if message.contains("중단"))); } #[test] fn subscription_failure_returns_a_user_facing_terminal_error() { let (events, receiver) = prepare_job_subscription(Err(anyhow::anyhow!("private storage error"))); assert!(receiver.is_none()); assert_eq!(events.len(), 1); assert!(matches!(&events[0], JobEvent::Error { message } if !message.contains("private"))); } #[test] fn live_job_and_finished_job_are_not_marked_as_interrupted() { let (_sender, receiver) = tokio::sync::broadcast::channel(2); let (events, receiver) = prepare_job_subscription(Ok((Vec::new(), Some(receiver)))); assert!(events.is_empty()); assert!(receiver.is_some()); for terminal in [ JobEvent::Done { place_id: "place-1".into(), }, JobEvent::Error { message: "analysis failed".into(), }, ] { let (events, receiver) = prepare_job_subscription(Ok((vec![terminal], None))); assert!(receiver.is_none()); assert_eq!(events.len(), 1); assert!(events[0].is_terminal()); } } }