This commit is contained in:
parent
5393ed3832
commit
d098beccae
|
@ -1,10 +1,8 @@
|
||||||
[package]
|
[package]
|
||||||
name = "quick-start"
|
name = "gabrielsimmerdotcom"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = { version = "0.6.18", features = ["json"] }
|
axum = { version = "0.6.18", features = ["json"] }
|
||||||
sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-rustls", "sqlite" ] }
|
sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-rustls", "sqlite" ] }
|
||||||
|
|
|
@ -11,7 +11,7 @@ RUN apt-get update -y && apt-get install -y ca-certificates protobuf-compiler &&
|
||||||
# Will build and cache the binary and dependent crates in release mode
|
# Will build and cache the binary and dependent crates in release mode
|
||||||
RUN --mount=type=cache,target=/usr/local/cargo,from=rust:latest,source=/usr/local/cargo \
|
RUN --mount=type=cache,target=/usr/local/cargo,from=rust:latest,source=/usr/local/cargo \
|
||||||
--mount=type=cache,target=target \
|
--mount=type=cache,target=target \
|
||||||
cargo build --release && mv ./target/release/quick-start ./gabrielsimmerdotcom
|
cargo build --release
|
||||||
|
|
||||||
# Runtime image
|
# Runtime image
|
||||||
FROM debian:bookworm-slim
|
FROM debian:bookworm-slim
|
||||||
|
|
12
src/cache/mod.rs
vendored
12
src/cache/mod.rs
vendored
|
@ -1,6 +1,6 @@
|
||||||
use sqlx::FromRow;
|
use sqlx::FromRow;
|
||||||
use std::sync::{OnceLock, Mutex};
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::sync::{Mutex, OnceLock};
|
||||||
|
|
||||||
#[derive(Clone, Debug, FromRow)]
|
#[derive(Clone, Debug, FromRow)]
|
||||||
pub struct CachedItem {
|
pub struct CachedItem {
|
||||||
|
@ -25,7 +25,10 @@ fn should_use(_item: &CachedItem) -> bool {
|
||||||
static CACHE: OnceLock<Mutex<HashMap<String, CachedItem>>> = OnceLock::new();
|
static CACHE: OnceLock<Mutex<HashMap<String, CachedItem>>> = OnceLock::new();
|
||||||
|
|
||||||
pub async fn get(key: &String) -> Option<CachedItem> {
|
pub async fn get(key: &String) -> Option<CachedItem> {
|
||||||
let data = CACHE.get_or_init(|| Mutex::new(HashMap::new())).lock().unwrap();
|
let data = CACHE
|
||||||
|
.get_or_init(|| Mutex::new(HashMap::new()))
|
||||||
|
.lock()
|
||||||
|
.unwrap();
|
||||||
dbg!(&key);
|
dbg!(&key);
|
||||||
match data.get(key) {
|
match data.get(key) {
|
||||||
Some(c) => {
|
Some(c) => {
|
||||||
|
@ -47,6 +50,9 @@ pub async fn get(key: &String) -> Option<CachedItem> {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
pub async fn set(key: String, item: CachedItem) {
|
pub async fn set(key: String, item: CachedItem) {
|
||||||
let mut data = CACHE.get_or_init(|| Mutex::new(HashMap::new())).lock().unwrap();
|
let mut data = CACHE
|
||||||
|
.get_or_init(|| Mutex::new(HashMap::new()))
|
||||||
|
.lock()
|
||||||
|
.unwrap();
|
||||||
data.insert(key, item);
|
data.insert(key, item);
|
||||||
}
|
}
|
||||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -27,12 +27,10 @@ use prost::Message;
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use rss::ChannelBuilder;
|
use rss::ChannelBuilder;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions};
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::net::UdpSocket;
|
use std::net::UdpSocket;
|
||||||
use std::str::FromStr;
|
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
use std::{env, io, thread};
|
use std::{io, thread};
|
||||||
use time::{self, format_description, format_description::well_known::Rfc2822};
|
use time::{self, format_description, format_description::well_known::Rfc2822};
|
||||||
use tower_http::services::ServeDir;
|
use tower_http::services::ServeDir;
|
||||||
|
|
||||||
|
@ -137,12 +135,13 @@ async fn main() -> Result<(), sqlx::Error> {
|
||||||
Some(items::payload::Msg::Cache(cache)) => {
|
Some(items::payload::Msg::Cache(cache)) => {
|
||||||
cache::set(
|
cache::set(
|
||||||
cache.key.clone(),
|
cache.key.clone(),
|
||||||
CachedItem{
|
CachedItem {
|
||||||
content_type: cache.content_type.clone(),
|
content_type: cache.content_type.clone(),
|
||||||
content: cache.content.clone(),
|
content: cache.content.clone(),
|
||||||
cached: cache.timestamp,
|
cached: cache.timestamp,
|
||||||
}
|
},
|
||||||
).await;
|
)
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
@ -506,11 +505,14 @@ async fn cached_page<T>(
|
||||||
.try_into()
|
.try_into()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let content = String::from_utf8(res).unwrap();
|
let content = String::from_utf8(res).unwrap();
|
||||||
cache::set(path.clone(), CachedItem{
|
cache::set(
|
||||||
|
path.clone(),
|
||||||
|
CachedItem {
|
||||||
content_type: contenttype.to_owned(),
|
content_type: contenttype.to_owned(),
|
||||||
content: content.clone(),
|
content: content.clone(),
|
||||||
cached: current_time,
|
cached: current_time,
|
||||||
})
|
},
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
let gossip = items::Payload {
|
let gossip = items::Payload {
|
||||||
peer: "".to_owned(),
|
peer: "".to_owned(),
|
||||||
|
|
Loading…
Reference in a new issue