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",
|
"http",
|
||||||
"maud",
|
"maud",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
"signal-hook",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -862,6 +863,25 @@ dependencies = [
|
|||||||
"serde",
|
"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]]
|
[[package]]
|
||||||
name = "slab"
|
name = "slab"
|
||||||
version = "0.4.8"
|
version = "0.4.8"
|
||||||
|
@ -11,3 +11,4 @@ fastrand = {version = "2", features = ["std"]}
|
|||||||
reqwest = {version = "0.11", default-features = false, features = ["rustls-tls-native-roots"]}
|
reqwest = {version = "0.11", default-features = false, features = ["rustls-tls-native-roots"]}
|
||||||
dashmap = "5"
|
dashmap = "5"
|
||||||
maud = "0.25"
|
maud = "0.25"
|
||||||
|
signal-hook = "0.3"
|
1
arts.txt
1
arts.txt
@ -1,3 +1,4 @@
|
|||||||
https://twitter.com/FFJ_OFF/status/1679888209705340928
|
https://twitter.com/FFJ_OFF/status/1679888209705340928
|
||||||
https://twitter.com/rauchoi_ovu/status/1680486681663922177
|
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 dashmap::DashMap;
|
||||||
use error::AppError;
|
use error::AppError;
|
||||||
use http::Uri;
|
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;
|
mod error;
|
||||||
|
|
||||||
type AppResult<T> = Result<T, AppError>;
|
type AppResult<T> = Result<T, AppError>;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
enum ArtKind {
|
enum ArtKind {
|
||||||
Twitter,
|
Twitter,
|
||||||
}
|
}
|
||||||
@ -23,6 +29,7 @@ impl FromStr for ArtKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
struct Art {
|
struct Art {
|
||||||
url: Uri,
|
url: Uri,
|
||||||
kind: ArtKind,
|
kind: ArtKind,
|
||||||
@ -42,16 +49,19 @@ impl FromStr for Art {
|
|||||||
struct Data {
|
struct Data {
|
||||||
// actual arts
|
// actual arts
|
||||||
art: Vec<Art>,
|
art: Vec<Art>,
|
||||||
|
art_indices: HashMap<Uri, usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Data {
|
impl Data {
|
||||||
fn parse(data: &str) -> AppResult<Self> {
|
fn parse(data: &str) -> AppResult<Self> {
|
||||||
let mut this = Self {
|
let mut this = Self {
|
||||||
art: Default::default(),
|
art: Default::default(),
|
||||||
|
art_indices: Default::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
for entry in data.lines() {
|
for entry in data.lines() {
|
||||||
let art: Art = entry.parse()?;
|
let art: Art = entry.parse()?;
|
||||||
|
this.art_indices.insert(art.url.clone(), this.art.len());
|
||||||
this.art.push(art);
|
this.art.push(art);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,12 +72,23 @@ impl Data {
|
|||||||
let no = fastrand::usize(0..self.art.len());
|
let no = fastrand::usize(0..self.art.len());
|
||||||
&self.art[no]
|
&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 {
|
struct InternalAppState {
|
||||||
// cached direct links to images
|
// cached direct links to images
|
||||||
direct_links: DashMap<Uri, String>,
|
direct_links: DashMap<Uri, String>,
|
||||||
data: Data,
|
data: Mutex<Data>,
|
||||||
http: reqwest::Client,
|
http: reqwest::Client,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +101,7 @@ impl AppState {
|
|||||||
fn new(data: Data) -> Self {
|
fn new(data: Data) -> Self {
|
||||||
Self {
|
Self {
|
||||||
internal: Arc::new(InternalAppState {
|
internal: Arc::new(InternalAppState {
|
||||||
data,
|
data: Mutex::new(data),
|
||||||
direct_links: Default::default(),
|
direct_links: Default::default(),
|
||||||
http: reqwest::ClientBuilder::new()
|
http: reqwest::ClientBuilder::new()
|
||||||
.redirect(reqwest::redirect::Policy::none())
|
.redirect(reqwest::redirect::Policy::none())
|
||||||
@ -99,11 +120,27 @@ impl Deref for AppState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ARTS_PATH: &str = "arts.txt";
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let arts = std::fs::read_to_string("arts.txt").unwrap();
|
let arts = std::fs::read_to_string(ARTS_PATH).unwrap();
|
||||||
let data = AppState::new(Data::parse(&arts).unwrap());
|
let state = AppState::new(Data::parse(&arts).unwrap());
|
||||||
let app = Router::new().route("/", get(show_art)).with_state(data);
|
|
||||||
|
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")
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
|
||||||
.await
|
.await
|
||||||
@ -113,15 +150,15 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn show_art(state: State<AppState>) -> AppResult<Html<String>> {
|
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) {
|
if let Some(image_link) = state.direct_links.get(&art.url) {
|
||||||
Ok(render_page(art, &image_link))
|
Ok(render_page(&art, &image_link))
|
||||||
} else {
|
} else {
|
||||||
let image_link = match art.kind {
|
let image_link = match art.kind {
|
||||||
ArtKind::Twitter => fetch_twitter_image_link(&state.http, &art.url).await?,
|
ArtKind::Twitter => fetch_twitter_image_link(&state.http, &art.url).await?,
|
||||||
};
|
};
|
||||||
let page = render_page(art, &image_link);
|
let page = render_page(&art, &image_link);
|
||||||
state.direct_links.insert(art.url.clone(), image_link);
|
state.direct_links.insert(art.url, image_link);
|
||||||
Ok(page)
|
Ok(page)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user