corobel/src/main.rs

168 lines
5.3 KiB
Rust

use clap::Parser;
use std::collections::HashMap;
use std::error::Error;
#[macro_use]
extern crate rocket;
use reqwest::StatusCode;
use rocket::response::content::RawHtml;
use rocket::serde::json::Json;
mod cohost_account;
mod cohost_posts;
mod syndication;
mod webfinger;
use cohost_account::{CohostAccount, COHOST_ACCOUNT_API_URL};
use cohost_posts::{cohost_posts_api_url, CohostPostsPage};
use webfinger::CohostWebfingerResource;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// The base URL for the corobel instance
#[clap(short, long, required = true)]
domain: String,
/// The base URL for the corobel instance
#[clap(short, long, default_value_t = default_base_url() )]
base_url: String,
}
fn default_base_url() -> String {
"/".into()
}
static ARGS: once_cell::sync::Lazy<Args> = once_cell::sync::Lazy::new(|| Args::parse());
#[get("/")]
fn index() -> RawHtml<&'static str> {
RawHtml(include_str!("../static/index.html"))
}
#[get("/<project>/feed.rss?<page>")]
async fn syndication_rss_route(project: &str, page: Option<u64>) -> Option<String> {
let page = page.unwrap_or(0);
let project_url = format!("{}{}", COHOST_ACCOUNT_API_URL, project);
let posts_url = cohost_posts_api_url(project, page);
eprintln!("making request to {}", project_url);
let project_data: CohostAccount = match reqwest::get(project_url).await {
Ok(v) => match v.status() {
StatusCode::OK => match v.json::<CohostAccount>().await {
Ok(a) => a,
Err(e) => {
eprintln!("Couldn't deserialize Cohost project '{}': {:?}", project, e);
return None;
}
},
// TODO NORA: Handle possible redirects
s => {
eprintln!(
"Didn't receive status code 200 for Cohost project '{}'; got {:?} instead.",
project, s
);
return None;
}
},
Err(e) => {
eprintln!(
"Error making request to Cohost for project '{}': {:?}",
project, e
);
return None;
}
};
eprintln!("making request to {}", posts_url);
match reqwest::get(posts_url).await {
Ok(v) => match v.status() {
StatusCode::OK => match v.json::<CohostPostsPage>().await {
Ok(page_data) => {
return Some(
syndication::channel_for_posts_page(project, page, project_data, page_data)
.to_string(),
);
}
Err(e) => {
eprintln!(
"Couldn't deserialize Cohost posts page for '{}': {:?}",
project, e
);
}
},
// TODO NORA: Handle possible redirects
s => {
eprintln!("Didn't receive status code 200 for posts for Cohost project '{}'; got {:?} instead.", page, s);
return None;
}
},
Err(e) => {
eprintln!(
"Error making request to Cohost for posts for project '{}': {:?}",
project, e
);
return None;
}
};
None
}
#[get("/.well-known/webfinger?<params..>")]
async fn webfinger_route(params: HashMap<String, String>) -> Option<Json<CohostWebfingerResource>> {
if params.len() != 1 {
eprintln!(
"Too may or too few parameters. Expected 1, got {}",
params.len()
);
return None;
}
if let Some(param) = params.iter().next() {
let url = format!("{}{}", COHOST_ACCOUNT_API_URL, param.0);
eprintln!("making request to {}", url);
match reqwest::get(url).await {
Ok(v) => {
match v.status() {
StatusCode::OK => match v.json::<CohostAccount>().await {
Ok(_v) => {
return Some(Json(CohostWebfingerResource::new(
param.0.as_str(),
&ARGS.domain,
)));
}
Err(e) => {
eprintln!("Couldn't deserialize Cohost project '{}': {:?}", param.0, e);
}
},
// TODO NORA: Handle possible redirects
s => {
eprintln!("Didn't receive status code 200 for Cohost project '{}'; got {:?} instead.", param.0, s);
return None;
}
}
}
Err(e) => {
eprintln!(
"Error making request to Cohost for project '{}': {:?}",
param.0, e
);
return None;
}
};
}
None
}
#[rocket::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Set up the global config
once_cell::sync::Lazy::force(&ARGS);
let _rocket = rocket::build()
.mount(
&ARGS.base_url,
routes![index, webfinger_route, syndication_rss_route],
)
.ignite()
.await?
.launch()
.await?;
Ok(())
}