From d098beccaebb2c2f774f926d07f37775863fc3e9 Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Mon, 23 Oct 2023 20:11:46 +0100 Subject: [PATCH] Formatting, rename binary --- Cargo.toml | 4 +--- Dockerfile | 2 +- src/cache/mod.rs | 12 +++++++++--- src/main.rs | 28 +++++++++++++++------------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4f2e191..547b9b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" ] } diff --git a/Dockerfile b/Dockerfile index 67cf680..398e2d6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/src/cache/mod.rs b/src/cache/mod.rs index fecd5ae..daddae7 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -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>> = OnceLock::new(); pub async fn get(key: &String) -> Option { - 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 { // } 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); } diff --git a/src/main.rs b/src/main.rs index ceff837..f430fee 100644 --- a/src/main.rs +++ b/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( .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 {