chore: focus on uofg

This commit is contained in:
newt 2024-09-25 01:23:50 +01:00
parent 4b70ac331d
commit e214b265cd
3 changed files with 29 additions and 84 deletions

51
Cargo.lock generated
View file

@ -263,21 +263,6 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "futures"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]] [[package]]
name = "futures-channel" name = "futures-channel"
version = "0.3.30" version = "0.3.30"
@ -285,7 +270,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-sink",
] ]
[[package]] [[package]]
@ -294,34 +278,6 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
[[package]]
name = "futures-executor"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
[[package]]
name = "futures-macro"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "futures-sink" name = "futures-sink"
version = "0.3.30" version = "0.3.30"
@ -340,16 +296,10 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
dependencies = [ dependencies = [
"futures-channel",
"futures-core", "futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task", "futures-task",
"memchr",
"pin-project-lite", "pin-project-lite",
"pin-utils", "pin-utils",
"slab",
] ]
[[package]] [[package]]
@ -1334,7 +1284,6 @@ version = "0.1.0"
dependencies = [ dependencies = [
"axum", "axum",
"color-eyre", "color-eyre",
"futures",
"reqwest", "reqwest",
"serde", "serde",
"tokio", "tokio",

View file

@ -1,12 +1,12 @@
[package] [package]
name = "uofgcal" name = "uofgcal"
publish = false
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
axum = "0.7.6" axum = "0.7.6"
color-eyre = "0.6.3" color-eyre = "0.6.3"
futures = "0.3.30"
reqwest = "0.12.7" reqwest = "0.12.7"
serde = { version = "1.0.210", features = ["derive"] } serde = { version = "1.0.210", features = ["derive"] }
tokio = { version = "1.40.0", features = ["full"] } tokio = { version = "1.40.0", features = ["full"] }

View file

@ -1,5 +1,4 @@
use axum::{ use axum::{
body::Bytes,
extract::{Query, State}, extract::{Query, State},
http::StatusCode, http::StatusCode,
response::{AppendHeaders, IntoResponse}, response::{AppendHeaders, IntoResponse},
@ -13,51 +12,43 @@ use std::borrow::Cow;
use tokio::net::TcpListener; use tokio::net::TcpListener;
const PORT: u16 = 8080; const PORT: u16 = 8080;
const CALENDAR_URL: &str = "https://frontdoor.spa.gla.ac.uk/spacett/download/uogtimetable.ics";
#[derive(Deserialize)] #[derive(Deserialize)]
struct CalendarPayload { struct AuthPayload {
calendar: Cow<'static, str>, guid: Cow<'static, str>,
filename: Option<Cow<'static, str>>, pass: Cow<'static, str>,
username: Cow<'static, str>,
password: Cow<'static, str>,
} }
async fn get_calender( async fn get_calender(
Query(auth): Query<CalendarPayload>, Query(auth): Query<AuthPayload>,
State(client): State<Client>, State(client): State<Client>,
) -> impl IntoResponse { ) -> impl IntoResponse {
if let Ok(resp) = client if let Ok(resp) = client
.get(auth.calendar.as_ref()) .get(CALENDAR_URL)
.basic_auth(auth.username.as_ref(), Some(auth.password.as_ref())) .basic_auth(auth.guid.as_ref(), Some(auth.pass.as_ref()))
.send() .send()
.await .await
{ {
Ok(( match resp.status() {
AppendHeaders([ StatusCode::UNAUTHORIZED => {
( Err((StatusCode::UNAUTHORIZED, "Invalid username or password"))
header::CONTENT_TYPE, }
"text/calendar; charset=utf-8".to_string(), _ => Ok((
), AppendHeaders([
( (header::CONTENT_TYPE, "text/calendar; charset=utf-8"),
header::CONTENT_DISPOSITION, (
format!( header::CONTENT_DISPOSITION,
r#"attachment; filename="{}""#, r#"attachment; filename="calendar.ics""#,
auth.filename.unwrap_or_else(|| {
if auth.calendar.ends_with(".ics") {
Cow::Borrowed(&auth.calendar.split('/').last().unwrap())
} else {
Cow::Borrowed("calendar.ics")
}
})
), ),
), ]),
]), resp.bytes().await.ok().unwrap_or_default(),
resp.bytes().await.ok().unwrap_or_default(), )),
)) }
} else { } else {
Err(( Err((
StatusCode::INTERNAL_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR,
"Failed to fetch calendar".to_string(), "Failed to fetch calendar",
)) ))
} }
} }
@ -65,11 +56,16 @@ async fn get_calender(
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
color_eyre::install()?; color_eyre::install()?;
let tcp_listener = TcpListener::bind(format!("0.0.0.0:{PORT}")).await?;
// setup the http client and tcp listener
let client = Client::new(); let client = Client::new();
let tcp_listener = TcpListener::bind(format!("0.0.0.0:{PORT}")).await?;
// start the server
let router = Router::new() let router = Router::new()
.route("/", get(get_calender)) .route("/", get(get_calender))
.with_state(client); .with_state(client);
axum::serve(tcp_listener, router).await?; axum::serve(tcp_listener, router).await?;
Ok(()) Ok(())
} }