chore: run cargo fmt
This commit is contained in:
parent
513c1c66c5
commit
b02a6e8b7c
|
@ -34,7 +34,13 @@ impl Loader {
|
|||
fn load_image(input_path: &Path, hashed_name: bool) -> Result<Vec<Asset>> {
|
||||
let img = ImageReader::open(input_path)?.decode()?;
|
||||
Ok(vec![
|
||||
Self::generate_image(input_path, &img, ImageOutputFormat::Avif, "avif", hashed_name)?,
|
||||
Self::generate_image(
|
||||
input_path,
|
||||
&img,
|
||||
ImageOutputFormat::Avif,
|
||||
"avif",
|
||||
hashed_name,
|
||||
)?,
|
||||
Self::generate_image(
|
||||
input_path,
|
||||
&img,
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
use maud::PreEscaped;
|
||||
use reqwest::{ClientBuilder, Client};
|
||||
use reqwest::{Client, ClientBuilder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{error::Result, apis::USER_AGENT};
|
||||
|
||||
use crate::{apis::USER_AGENT, error::Result};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MediawikiClient<S: ClientState> {
|
||||
|
@ -17,7 +16,8 @@ impl MediawikiClient<Credentials> {
|
|||
let client = ClientBuilder::new()
|
||||
.user_agent(USER_AGENT)
|
||||
.cookie_store(true)
|
||||
.build().expect("failed to build client");
|
||||
.build()
|
||||
.expect("failed to build client");
|
||||
Self {
|
||||
state: Credentials { username, password },
|
||||
client,
|
||||
|
@ -26,19 +26,28 @@ impl MediawikiClient<Credentials> {
|
|||
}
|
||||
|
||||
async fn get_login_token(&self) -> Result<String> {
|
||||
let url = format!("https://{domain}/w/api.php?action=query&meta=tokens&format=json&type=login", domain = self.domain);
|
||||
let res: QueryResponse = self.client.get(url)
|
||||
.send().await?
|
||||
let url = format!(
|
||||
"https://{domain}/w/api.php?action=query&meta=tokens&format=json&type=login",
|
||||
domain = self.domain
|
||||
);
|
||||
let res: QueryResponse = self
|
||||
.client
|
||||
.get(url)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.json().await?;
|
||||
.json()
|
||||
.await?;
|
||||
|
||||
Ok(res.query.tokens.login_token)
|
||||
}
|
||||
|
||||
pub async fn log_in(self, allowed_pages: Vec<String>) -> Result<MediawikiClient<LoggedIn>> {
|
||||
let login_token = self.get_login_token().await?;
|
||||
let url = format!( "https://{domain}/w/api.php", domain = self.domain);
|
||||
let _res = self.client.post(url)
|
||||
let url = format!("https://{domain}/w/api.php", domain = self.domain);
|
||||
let _res = self
|
||||
.client
|
||||
.post(url)
|
||||
.form(&LoginForm {
|
||||
action: "login",
|
||||
lgname: self.state.username,
|
||||
|
@ -46,21 +55,23 @@ impl MediawikiClient<Credentials> {
|
|||
lgtoken: login_token,
|
||||
format: "json",
|
||||
})
|
||||
.send().await?
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
Ok(MediawikiClient {
|
||||
client: self.client,
|
||||
domain: self.domain,
|
||||
state: LoggedIn {
|
||||
allowed_pages,
|
||||
},
|
||||
state: LoggedIn { allowed_pages },
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl MediawikiClient<LoggedIn> {
|
||||
pub async fn get_page(&self, page_title: String) -> Result<Option<(String, PreEscaped<String>)>> {
|
||||
pub async fn get_page(
|
||||
&self,
|
||||
page_title: String,
|
||||
) -> Result<Option<(String, PreEscaped<String>)>> {
|
||||
if !self.state.allowed_pages.contains(&page_title) {
|
||||
return Ok(None);
|
||||
}
|
||||
|
@ -70,10 +81,14 @@ impl MediawikiClient<LoggedIn> {
|
|||
domain = self.domain,
|
||||
);
|
||||
|
||||
let res: ParseResponse = self.client.get(url)
|
||||
.send().await?
|
||||
let res: ParseResponse = self
|
||||
.client
|
||||
.get(url)
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.json().await?;
|
||||
.json()
|
||||
.await?;
|
||||
|
||||
Ok(Some((res.parse.title, PreEscaped(res.parse.text.content))))
|
||||
}
|
||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -12,7 +12,8 @@ mod templates;
|
|||
use std::path::Path;
|
||||
|
||||
use apis::{
|
||||
CachingFetcher, NowPlayingInfo, PronounsPageProfile, NOWPLAYING_URL, PRONOUNS_PAGE_URL, mediawiki::MediawikiClient,
|
||||
mediawiki::MediawikiClient, CachingFetcher, NowPlayingInfo, PronounsPageProfile,
|
||||
NOWPLAYING_URL, PRONOUNS_PAGE_URL,
|
||||
};
|
||||
use axum::extract::Extension;
|
||||
#[cfg(debug_assertions)]
|
||||
|
@ -51,7 +52,12 @@ async fn main() -> error::Result<()> {
|
|||
|
||||
tracing::info!("Logging into mediawiki instance...");
|
||||
let mediawiki_client = mediawiki_client
|
||||
.log_in(fetch_env!("MW_TITLE_ALLOWLIST").split(',').map(|s| s.to_owned()).collect::<Vec<_>>())
|
||||
.log_in(
|
||||
fetch_env!("MW_TITLE_ALLOWLIST")
|
||||
.split(',')
|
||||
.map(|s| s.to_owned())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let app = routes::build_router()
|
||||
|
|
|
@ -1,27 +1,31 @@
|
|||
use axum::{extract::{Path, Extension}, response::IntoResponse};
|
||||
use axum::{
|
||||
extract::{Extension, Path},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use maud::PreEscaped;
|
||||
use reqwest::StatusCode;
|
||||
|
||||
use crate::{apis::mediawiki::{MediawikiClient, LoggedIn}, templates::{ExtraTemplate, HtmlTemplate}};
|
||||
use crate::{
|
||||
apis::mediawiki::{LoggedIn, MediawikiClient},
|
||||
templates::{ExtraTemplate, HtmlTemplate},
|
||||
};
|
||||
|
||||
pub async fn page(
|
||||
Path(title): Path<String>,
|
||||
Extension(mediawiki_client): Extension<MediawikiClient<LoggedIn>>,
|
||||
) -> Result<impl IntoResponse, StatusCode> {
|
||||
if let Some((title, PreEscaped(content))) = mediawiki_client.get_page(title)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
if let Some((title, PreEscaped(content))) =
|
||||
mediawiki_client.get_page(title).await.map_err(|e| {
|
||||
tracing::error!("Failed to get page: {}", e);
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
})? {
|
||||
Ok(HtmlTemplate::new(
|
||||
format!("/extras/{title}"),
|
||||
ExtraTemplate {
|
||||
title,
|
||||
content,
|
||||
}
|
||||
).into_response().await)
|
||||
} else {
|
||||
Ok(super::handle_404().await)
|
||||
}
|
||||
})?
|
||||
{
|
||||
Ok(
|
||||
HtmlTemplate::new(format!("/extras/{title}"), ExtraTemplate { title, content })
|
||||
.into_response()
|
||||
.await,
|
||||
)
|
||||
} else {
|
||||
Ok(super::handle_404().await)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,7 +73,8 @@ async fn handle_404() -> Response {
|
|||
)
|
||||
.into_response()
|
||||
.await,
|
||||
).into_response()
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
pub fn build_router() -> Router {
|
||||
|
|
|
@ -2,7 +2,6 @@ use std::collections::HashMap;
|
|||
|
||||
use axum::{extract::Path, response::IntoResponse};
|
||||
use regex::Regex;
|
||||
use reqwest::StatusCode;
|
||||
use rust_embed::RustEmbed;
|
||||
|
||||
use crate::{
|
||||
|
@ -59,9 +58,7 @@ fn load_project(filename: &str) -> Option<Project> {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn project(
|
||||
Path((year, slug)): Path<(String, String)>,
|
||||
) -> impl IntoResponse {
|
||||
pub async fn project(Path((year, slug)): Path<(String, String)>) -> impl IntoResponse {
|
||||
if let Some(post) = load_project(&format!("{}-{}.md", year, slug)) {
|
||||
HtmlTemplate::new(
|
||||
format!("/projects/{}/{}", year, slug),
|
||||
|
|
Loading…
Reference in New Issue