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 Json(json!({
39 "availableUserDomains": domains,
40 "inviteCodeRequired": invite_code_required,
41 "did": format!("did:web:{}", pds_hostname),
42 "links": {
43 "privacyPolicy": privacy_policy,
44 "termsOfService": terms_of_service
45 },
46 "contact": {
47 "email": contact_email
48 },
49 "version": env!("CARGO_PKG_VERSION"),
50 "availableCommsChannels": get_available_comms_channels()
51 }))
52}
53pub async fn health(State(state): State<AppState>) -> impl IntoResponse {
54 match sqlx::query!("SELECT 1 as one").fetch_one(&state.db).await {
55 Ok(_) => (StatusCode::OK, "OK"),
56 Err(e) => {
57 error!("Health check failed: {:?}", e);
58 (StatusCode::SERVICE_UNAVAILABLE, "Service Unavailable")
59 }
60 }
61}