Skip to main content

systemprompt_api/services/static_content/
fallback.rs

1//! Fallback handler returning JSON 404s for unmatched API paths and static
2//! content otherwise.
3//!
4//! Copyright (c) systemprompt.io — Business Source License 1.1.
5//! See <https://systemprompt.io> for licensing details.
6
7use axum::extract::State;
8use axum::http::{HeaderMap, Method, StatusCode, Uri};
9use axum::response::{IntoResponse, Json};
10use serde_json::json;
11use systemprompt_models::modules::ApiPaths;
12
13use super::static_files::StaticContentState;
14
15pub async fn smart_fallback_handler(
16    State(state): State<StaticContentState>,
17    uri: Uri,
18    method: Method,
19    headers: HeaderMap,
20    req_ctx: Option<axum::Extension<systemprompt_models::RequestContext>>,
21) -> impl IntoResponse {
22    let path = uri.path();
23
24    if is_api_path(path) {
25        return (
26            StatusCode::NOT_FOUND,
27            Json(json!({
28                "error": "Not Found",
29                "message": format!("No route matches {method} {path}"),
30                "path": path,
31                "suggestions": get_api_suggestions(path)
32            })),
33        )
34            .into_response();
35    }
36
37    super::serve_static_content(State(state), uri, headers, req_ctx)
38        .await
39        .into_response()
40}
41
42pub fn is_api_path(path: &str) -> bool {
43    path.starts_with(ApiPaths::API_BASE)
44        || path.starts_with(ApiPaths::WELLKNOWN_BASE)
45        || path.starts_with("/server/")
46        || path.starts_with("/mcp/")
47        || path.starts_with("/health")
48        || path.starts_with(ApiPaths::OPENAPI_BASE)
49        || path.starts_with(ApiPaths::DOCS_BASE)
50        || path.starts_with(ApiPaths::SWAGGER_BASE)
51        || path.starts_with("/v1/")
52        || path.starts_with("/auth/")
53        || path.starts_with("/oauth/")
54}
55
56pub fn get_api_suggestions(path: &str) -> Vec<String> {
57    if path.starts_with(ApiPaths::API_BASE) {
58        vec![
59            format!("{} - API discovery endpoint", ApiPaths::DISCOVERY),
60            format!("{} - Health check", ApiPaths::HEALTH),
61            format!(
62                "{} - Core services (contexts, tasks, artifacts)",
63                ApiPaths::CORE_BASE
64            ),
65            format!("{} - Agent registry", ApiPaths::AGENTS_REGISTRY),
66            format!("{} - MCP server registry", ApiPaths::MCP_REGISTRY),
67        ]
68    } else if path.starts_with(ApiPaths::WELLKNOWN_BASE) {
69        vec![
70            format!("{} - OAuth metadata", ApiPaths::WELLKNOWN_OAUTH_SERVER),
71            format!("{} - Agent card", ApiPaths::WELLKNOWN_AGENT_CARD),
72        ]
73    } else if path.contains("health") {
74        vec![format!("{} - Health check endpoint", ApiPaths::HEALTH)]
75    } else if path.contains("openapi") || path.contains("swagger") {
76        vec![format!(
77            "{} - API discovery (OpenAPI not yet available)",
78            ApiPaths::DISCOVERY
79        )]
80    } else {
81        vec![
82            format!("{} - Start here for API discovery", ApiPaths::DISCOVERY),
83            "/ - Frontend application".to_owned(),
84        ]
85    }
86}