add reloading arts
This commit is contained in:
parent
6d4665af74
commit
bee55f62a2
20
Cargo.lock
generated
20
Cargo.lock
generated
@ -448,6 +448,7 @@ dependencies = [
|
||||
"http",
|
||||
"maud",
|
||||
"reqwest",
|
||||
"signal-hook",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
@ -862,6 +863,25 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook"
|
||||
version = "0.3.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b824b6e687aff278cdbf3b36f07aa52d4bd4099699324d5da86a2ebce3aa00b3"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"signal-hook-registry",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.8"
|
||||
|
@ -10,4 +10,5 @@ http = "0.2"
|
||||
fastrand = {version = "2", features = ["std"]}
|
||||
reqwest = {version = "0.11", default-features = false, features = ["rustls-tls-native-roots"]}
|
||||
dashmap = "5"
|
||||
maud = "0.25"
|
||||
maud = "0.25"
|
||||
signal-hook = "0.3"
|
3
arts.txt
3
arts.txt
@ -1,3 +1,4 @@
|
||||
https://twitter.com/FFJ_OFF/status/1679888209705340928
|
||||
https://twitter.com/rauchoi_ovu/status/1680486681663922177
|
||||
https://twitter.com/lk0_71604/status/1680425494741938179
|
||||
https://twitter.com/lk0_71604/status/1680425494741938179
|
||||
https://twitter.com/stellaofastra/status/1680217448900096001
|
57
src/main.rs
57
src/main.rs
@ -2,12 +2,18 @@ use axum::{extract::State, response::Html, routing::get, Router};
|
||||
use dashmap::DashMap;
|
||||
use error::AppError;
|
||||
use http::Uri;
|
||||
use std::{ops::Deref, str::FromStr, sync::Arc};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
ops::Deref,
|
||||
str::FromStr,
|
||||
sync::{atomic::AtomicBool, Arc, Mutex},
|
||||
};
|
||||
|
||||
mod error;
|
||||
|
||||
type AppResult<T> = Result<T, AppError>;
|
||||
|
||||
#[derive(Clone)]
|
||||
enum ArtKind {
|
||||
Twitter,
|
||||
}
|
||||
@ -23,6 +29,7 @@ impl FromStr for ArtKind {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Art {
|
||||
url: Uri,
|
||||
kind: ArtKind,
|
||||
@ -42,16 +49,19 @@ impl FromStr for Art {
|
||||
struct Data {
|
||||
// actual arts
|
||||
art: Vec<Art>,
|
||||
art_indices: HashMap<Uri, usize>,
|
||||
}
|
||||
|
||||
impl Data {
|
||||
fn parse(data: &str) -> AppResult<Self> {
|
||||
let mut this = Self {
|
||||
art: Default::default(),
|
||||
art_indices: Default::default(),
|
||||
};
|
||||
|
||||
for entry in data.lines() {
|
||||
let art: Art = entry.parse()?;
|
||||
this.art_indices.insert(art.url.clone(), this.art.len());
|
||||
this.art.push(art);
|
||||
}
|
||||
|
||||
@ -62,12 +72,23 @@ impl Data {
|
||||
let no = fastrand::usize(0..self.art.len());
|
||||
&self.art[no]
|
||||
}
|
||||
|
||||
fn reload(&mut self, data: &str) -> AppResult<()> {
|
||||
for entry in data.lines() {
|
||||
let art: Art = entry.parse()?;
|
||||
if !self.art_indices.contains_key(&art.url) {
|
||||
self.art_indices.insert(art.url.clone(), self.art.len());
|
||||
self.art.push(art);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct InternalAppState {
|
||||
// cached direct links to images
|
||||
direct_links: DashMap<Uri, String>,
|
||||
data: Data,
|
||||
data: Mutex<Data>,
|
||||
http: reqwest::Client,
|
||||
}
|
||||
|
||||
@ -80,7 +101,7 @@ impl AppState {
|
||||
fn new(data: Data) -> Self {
|
||||
Self {
|
||||
internal: Arc::new(InternalAppState {
|
||||
data,
|
||||
data: Mutex::new(data),
|
||||
direct_links: Default::default(),
|
||||
http: reqwest::ClientBuilder::new()
|
||||
.redirect(reqwest::redirect::Policy::none())
|
||||
@ -99,11 +120,27 @@ impl Deref for AppState {
|
||||
}
|
||||
}
|
||||
|
||||
const ARTS_PATH: &str = "arts.txt";
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let arts = std::fs::read_to_string("arts.txt").unwrap();
|
||||
let data = AppState::new(Data::parse(&arts).unwrap());
|
||||
let app = Router::new().route("/", get(show_art)).with_state(data);
|
||||
let arts = std::fs::read_to_string(ARTS_PATH).unwrap();
|
||||
let state = AppState::new(Data::parse(&arts).unwrap());
|
||||
|
||||
std::thread::spawn({
|
||||
use signal_hook::{consts::SIGUSR2, iterator::Signals};
|
||||
|
||||
let state = state.clone();
|
||||
move || {
|
||||
let mut signals = Signals::new(&[SIGUSR2]).unwrap();
|
||||
for _ in signals.forever() {
|
||||
let data = std::fs::read_to_string(ARTS_PATH).unwrap();
|
||||
state.data.lock().unwrap().reload(&data).unwrap();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let app = Router::new().route("/", get(show_art)).with_state(state);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
|
||||
.await
|
||||
@ -113,15 +150,15 @@ async fn main() {
|
||||
}
|
||||
|
||||
async fn show_art(state: State<AppState>) -> AppResult<Html<String>> {
|
||||
let art = state.data.pick_random_art();
|
||||
let art = state.data.lock().unwrap().pick_random_art().clone();
|
||||
if let Some(image_link) = state.direct_links.get(&art.url) {
|
||||
Ok(render_page(art, &image_link))
|
||||
Ok(render_page(&art, &image_link))
|
||||
} else {
|
||||
let image_link = match art.kind {
|
||||
ArtKind::Twitter => fetch_twitter_image_link(&state.http, &art.url).await?,
|
||||
};
|
||||
let page = render_page(art, &image_link);
|
||||
state.direct_links.insert(art.url.clone(), image_link);
|
||||
let page = render_page(&art, &image_link);
|
||||
state.direct_links.insert(art.url, image_link);
|
||||
Ok(page)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user