this repo has no description
1use crate::state::AppState;
2use axum::{Json, extract::State, http::StatusCode, response::IntoResponse};
3use serde_json::json;
4use tracing::error;
5
6fn get_available_comms_channels() -> Vec<&'static str> {
7 let mut channels = vec!["email"];
8 if std::env::var("DISCORD_WEBHOOK_URL").is_ok() {
9 channels.push("discord");
10 }
11 if std::env::var("TELEGRAM_BOT_TOKEN").is_ok() {
12 channels.push("telegram");
13 }
14 if std::env::var("SIGNAL_CLI_PATH").is_ok() && std::env::var("SIGNAL_SENDER_NUMBER").is_ok() {
15 channels.push("signal");
16 }
17 channels
18}
19
20pub async fn robots_txt() -> impl IntoResponse {
21 (
22 StatusCode::OK,
23 [("content-type", "text/plain")],
24 "# Hello!\n\n# Crawling the public API is allowed\nUser-agent: *\nAllow: /\n",
25 )
26}
27pub async fn describe_server() -> impl IntoResponse {
28 let pds_hostname = std::env::var("PDS_HOSTNAME").unwrap_or_else(|_| "localhost".to_string());
29 let domains_str =
30 std::env::var("AVAILABLE_USER_DOMAINS").unwrap_or_else(|_| pds_hostname.clone());
31 let domains: Vec<&str> = domains_str.split(',').map(|s| s.trim()).collect();
32 let invite_code_required = std::env::var("INVITE_CODE_REQUIRED")
33 .map(|v| v == "true" || v == "1")
34 .unwrap_or(false);
35 let privacy_policy = std::env::var("PRIVACY_POLICY_URL").ok();
36 let terms_of_service = std::env::var("TERMS_OF_SERVICE_URL").ok();
37 let contact_email = std::env::var("CONTACT_EMAIL").ok();
38 let mut links = serde_json::Map::new();
39 if let Some(pp) = privacy_policy {
40 links.insert("privacyPolicy".to_string(), json!(pp));
41 }
42 if let Some(tos) = terms_of_service {
43 links.insert("termsOfService".to_string(), json!(tos));
44 }
45 let mut contact = serde_json::Map::new();
46 if let Some(email) = contact_email {
47 contact.insert("email".to_string(), json!(email));
48 }
49 Json(json!({
50 "availableUserDomains": domains,
51 "inviteCodeRequired": invite_code_required,
52 "did": format!("did:web:{}", pds_hostname),
53 "links": links,
54 "contact": contact,
55 "version": env!("CARGO_PKG_VERSION"),
56 "availableCommsChannels": get_available_comms_channels()
57 }))
58}
59pub async fn health(State(state): State<AppState>) -> impl IntoResponse {
60 match sqlx::query!("SELECT 1 as one").fetch_one(&state.db).await {
61 Ok(_) => (StatusCode::OK, "OK"),
62 Err(e) => {
63 error!("Health check failed: {:?}", e);
64 (StatusCode::SERVICE_UNAVAILABLE, "Service Unavailable")
65 }
66 }
67}