Compare commits

...

2 commits

Author SHA1 Message Date
Gabriel Simmer d098beccae
Formatting, rename binary
Some checks failed
Fly Deploy / Deploy app (push) Failing after 37s
2023-10-23 20:11:46 +01:00
Gabriel Simmer 5393ed3832
Refactor to remove lazy_static for OnceLock+Mutex 2023-10-23 20:11:16 +01:00
5 changed files with 63 additions and 69 deletions

61
Cargo.lock generated
View file

@ -1037,6 +1037,36 @@ dependencies = [
"slab", "slab",
] ]
[[package]]
name = "gabrielsimmerdotcom"
version = "0.1.0"
dependencies = [
"async-trait",
"axum",
"clap 4.3.21",
"comrak",
"crossbeam",
"file-format",
"frontmatter",
"futures",
"hex",
"hyper",
"maud",
"orgize",
"prost",
"prost-build",
"rand",
"rss",
"serde",
"serde_dhall",
"sha2",
"sqlx",
"time",
"tokio",
"tower",
"tower-http",
]
[[package]] [[package]]
name = "generic-array" name = "generic-array"
version = "0.14.7" version = "0.14.7"
@ -2007,37 +2037,6 @@ dependencies = [
"prost", "prost",
] ]
[[package]]
name = "quick-start"
version = "0.1.0"
dependencies = [
"async-trait",
"axum",
"clap 4.3.21",
"comrak",
"crossbeam",
"file-format",
"frontmatter",
"futures",
"hex",
"hyper",
"lazy_static 1.4.0",
"maud",
"orgize",
"prost",
"prost-build",
"rand",
"rss",
"serde",
"serde_dhall",
"sha2",
"sqlx",
"time",
"tokio",
"tower",
"tower-http",
]
[[package]] [[package]]
name = "quick-xml" name = "quick-xml"
version = "0.30.0" version = "0.30.0"

View file

@ -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" ] }
@ -16,7 +14,6 @@ hyper = { version = "0.14", features = ["full"] }
tower-http = { version = "0.4.1", features = ["fs", "add-extension", "auth", "compression-full", "trace"] } tower-http = { version = "0.4.1", features = ["fs", "add-extension", "auth", "compression-full", "trace"] }
sha2 = "0.10.7" sha2 = "0.10.7"
hex = "0.4" hex = "0.4"
lazy_static = "1.4.0"
futures = "0.3.28" futures = "0.3.28"
comrak = "0.1" comrak = "0.1"
orgize = { git = "https://git.gmem.ca/arch/orgize.git", branch = "org-images" } orgize = { git = "https://git.gmem.ca/arch/orgize.git", branch = "org-images" }

View file

@ -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

33
src/cache/mod.rs vendored
View file

@ -1,12 +1,6 @@
use async_trait::async_trait;
use sqlx::FromRow; use sqlx::FromRow;
use std::{
fmt,
time::{SystemTime, UNIX_EPOCH},
};
use std::collections::HashMap; use std::collections::HashMap;
use tokio::sync::Mutex; use std::sync::{Mutex, OnceLock};
#[derive(Clone, Debug, FromRow)] #[derive(Clone, Debug, FromRow)]
pub struct CachedItem { pub struct CachedItem {
@ -16,7 +10,7 @@ pub struct CachedItem {
} }
/// Determine whether we should actually use the cached item or not. /// Determine whether we should actually use the cached item or not.
fn should_use(item: &CachedItem) -> bool { fn should_use(_item: &CachedItem) -> bool {
// let current_time: i64 = SystemTime::now() // let current_time: i64 = SystemTime::now()
// .duration_since(UNIX_EPOCH) // .duration_since(UNIX_EPOCH)
// .expect("SystemTime before UNIX EPOCH!") // .expect("SystemTime before UNIX EPOCH!")
@ -28,12 +22,13 @@ fn should_use(item: &CachedItem) -> bool {
true true
} }
lazy_static! { static CACHE: OnceLock<Mutex<HashMap<String, CachedItem>>> = OnceLock::new();
static ref CACHE: Mutex<HashMap<String, CachedItem>> = Mutex::new(HashMap::new());
}
pub async fn get(key: &String) -> Option<CachedItem> { pub async fn get(key: &String) -> Option<CachedItem> {
let data = CACHE.lock().await; 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) => {
@ -48,12 +43,16 @@ pub async fn get(key: &String) -> Option<CachedItem> {
} }
} }
async fn rm(key: String) { // async fn rm(key: String) {
let mut data = CACHE.lock().await; // let mut data = CACHE.get().unwrap().clone();
data.remove(&key); // data.remove(&key);
} // let _ = CACHE.set(data);
// }
pub async fn set(key: String, item: CachedItem) { pub async fn set(key: String, item: CachedItem) {
let mut data = CACHE.lock().await; let mut data = CACHE
.get_or_init(|| Mutex::new(HashMap::new()))
.lock()
.unwrap();
data.insert(key, item); data.insert(key, item);
} }

View file

@ -1,6 +1,3 @@
#[macro_use]
extern crate lazy_static;
// Include the `items` module, which is generated from items.proto. // Include the `items` module, which is generated from items.proto.
pub mod items { pub mod items {
include!(concat!(env!("OUT_DIR"), "/gabrielsimmerdotcom.gossip.rs")); include!(concat!(env!("OUT_DIR"), "/gabrielsimmerdotcom.gossip.rs"));
@ -30,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;
@ -144,8 +139,9 @@ async fn main() -> Result<(), sqlx::Error> {
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;
} }
_ => {} _ => {}
} }
@ -509,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(),