This commit is contained in:
parent
5393ed3832
commit
d098beccae
|
@ -1,10 +1,8 @@
|
|||
[package]
|
||||
name = "quick-start"
|
||||
name = "gabrielsimmerdotcom"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
axum = { version = "0.6.18", features = ["json"] }
|
||||
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
|
||||
RUN --mount=type=cache,target=/usr/local/cargo,from=rust:latest,source=/usr/local/cargo \
|
||||
--mount=type=cache,target=target \
|
||||
cargo build --release && mv ./target/release/quick-start ./gabrielsimmerdotcom
|
||||
cargo build --release
|
||||
|
||||
# Runtime image
|
||||
FROM debian:bookworm-slim
|
||||
|
|
12
src/cache/mod.rs
vendored
12
src/cache/mod.rs
vendored
|
@ -1,6 +1,6 @@
|
|||
use sqlx::FromRow;
|
||||
use std::sync::{OnceLock, Mutex};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
#[derive(Clone, Debug, FromRow)]
|
||||
pub struct CachedItem {
|
||||
|
@ -25,7 +25,10 @@ fn should_use(_item: &CachedItem) -> bool {
|
|||
static CACHE: OnceLock<Mutex<HashMap<String, CachedItem>>> = OnceLock::new();
|
||||
|
||||
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);
|
||||
match data.get(key) {
|
||||
Some(c) => {
|
||||
|
@ -47,6 +50,9 @@ pub async fn get(key: &String) -> Option<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);
|
||||
}
|
||||
|
|
28
src/main.rs
28
src/main.rs
|
@ -27,12 +27,10 @@ use prost::Message;
|
|||
use rand::seq::SliceRandom;
|
||||
use rss::ChannelBuilder;
|
||||
use serde::Deserialize;
|
||||
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions};
|
||||
use std::collections::HashMap;
|
||||
use std::net::UdpSocket;
|
||||
use std::str::FromStr;
|
||||
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 tower_http::services::ServeDir;
|
||||
|
||||
|
@ -137,12 +135,13 @@ async fn main() -> Result<(), sqlx::Error> {
|
|||
Some(items::payload::Msg::Cache(cache)) => {
|
||||
cache::set(
|
||||
cache.key.clone(),
|
||||
CachedItem{
|
||||
CachedItem {
|
||||
content_type: cache.content_type.clone(),
|
||||
content: cache.content.clone(),
|
||||
cached: cache.timestamp,
|
||||
}
|
||||
).await;
|
||||
},
|
||||
)
|
||||
.await;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
@ -292,7 +291,7 @@ fn gossiper(
|
|||
dbg!(&peer);
|
||||
let _result = match r_socket.send_to(&payload, &peer) {
|
||||
Ok(_r) => {
|
||||
// dbg!(r);
|
||||
// dbg!(r);
|
||||
}
|
||||
Err(e) => {
|
||||
dbg!(e);
|
||||
|
@ -506,12 +505,15 @@ async fn cached_page<T>(
|
|||
.try_into()
|
||||
.unwrap();
|
||||
let content = String::from_utf8(res).unwrap();
|
||||
cache::set(path.clone(), CachedItem{
|
||||
content_type: contenttype.to_owned(),
|
||||
content: content.clone(),
|
||||
cached: current_time,
|
||||
})
|
||||
.await;
|
||||
cache::set(
|
||||
path.clone(),
|
||||
CachedItem {
|
||||
content_type: contenttype.to_owned(),
|
||||
content: content.clone(),
|
||||
cached: current_time,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
let gossip = items::Payload {
|
||||
peer: "".to_owned(),
|
||||
msg: Some(items::payload::Msg::NewCache(items::NewCache {
|
||||
|
|
Loading…
Reference in a new issue