From f657a61d55b31399ff5758970c10f4cd165ad493 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Sun, 29 Dec 2024 19:35:56 +0100 Subject: [PATCH] remove db --- .dockerignore | 3 ++- .gitignore | 1 + docker-compose.yml | 16 +--------------- migrations/0001_favicons.sql | 5 ----- src/archive.rs | 8 ++------ src/favicon.rs | 10 ++-------- src/main.rs | 6 +----- src/pages/mod.rs | 25 ++++++++++++++----------- 8 files changed, 23 insertions(+), 51 deletions(-) delete mode 100644 migrations/0001_favicons.sql diff --git a/.dockerignore b/.dockerignore index b92223f..fe9a3d8 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,3 @@ websites -target \ No newline at end of file +target +favicon \ No newline at end of file diff --git a/.gitignore b/.gitignore index 73e591f..87ddbda 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /websites +/favicon \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 53da942..67564e0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,27 +3,13 @@ services: build: . ports: - "8080:8000" - depends_on: - - postgres volumes: - ./websites:/websites + - ./favicon:/favicon environment: - - "DATABASE_URL=postgres://user:pass@postgres/webarc" - "RUST_LOG=info" - "ROCKET_ADDRESS=0.0.0.0" # Rewrite links to point back to the archive itself - "ROUTE_INTERNAL=true" # Download missing routes on demand - "DOWNLOAD_ON_DEMAND=true" - - postgres: - image: timescale/timescaledb:latest-pg16 - restart: always - ports: - - 5432:5432 - volumes: - - ./db:/var/lib/postgresql/data/ - environment: - - POSTGRES_USER=user - - POSTGRES_PASSWORD=pass - - POSTGRES_DB=webarc diff --git a/migrations/0001_favicons.sql b/migrations/0001_favicons.sql deleted file mode 100644 index 544553f..0000000 --- a/migrations/0001_favicons.sql +++ /dev/null @@ -1,5 +0,0 @@ - -CREATE TABLE site_favicon ( - domain VARCHAR(500) NOT NULL PRIMARY KEY, - favicon bytea NOT NULL -); diff --git a/src/archive.rs b/src/archive.rs index d46cbdd..74cf768 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -1,8 +1,4 @@ -use std::{ - fmt::format, - fs::read_to_string, - path::{Path, PathBuf}, -}; +use std::path::PathBuf; pub fn read_dir(dir: &PathBuf) -> Vec { let mut list = Vec::new(); @@ -216,7 +212,7 @@ impl WebsiteArchive { // redownload after threshold fn run_command(cmd: &[&str]) { - let mut cmd_setup = std::process::Command::new(cmd[0].clone()); + let mut cmd_setup = std::process::Command::new(cmd[0]); let cmd_setup = cmd_setup .args(cmd.into_iter().skip(1).collect::>()) .stdout(std::process::Stdio::inherit()) diff --git a/src/favicon.rs b/src/favicon.rs index 6653f68..70f05e6 100644 --- a/src/favicon.rs +++ b/src/favicon.rs @@ -1,6 +1,3 @@ -use based::get_pg; - - pub async fn download_favicon(domain: &str) -> Option> { let mut favicon_url = url::Url::parse(&format!("https://{}", domain)).ok()?; favicon_url.set_path("/favicon.ico"); @@ -21,10 +18,7 @@ pub async fn download_favicon(domain: &str) -> Option> { pub async fn download_favicons_for_sites(sites: Vec) { for site in sites { if let Some(fav) = download_favicon(&site).await { - sqlx::query("INSERT INTO site_favicon VALUES ($1, $2)") - .bind(site) - .bind(fav) - .execute(get_pg!()).await.unwrap(); + std::fs::write(std::path::Path::new("./favicon").join(site), fav).unwrap(); } } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 28ec422..9aaa595 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,14 @@ use archive::WebsiteArchive; -use based::get_pg; use rocket::routes; mod archive; -mod pages; mod favicon; +mod pages; #[rocket::launch] async fn launch() -> _ { env_logger::init(); - let pg = get_pg!(); - sqlx::migrate!("./migrations").run(pg).await.unwrap(); - let arc = WebsiteArchive::new("./websites"); favicon::download_favicons_for_sites(arc.domains()).await; diff --git a/src/pages/mod.rs b/src/pages/mod.rs index 74e6e40..ad64349 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -1,10 +1,11 @@ -use std::path::PathBuf; +use std::{io::Read, path::PathBuf}; use based::{ - get_pg, page::Shell, request::{assets::DataResponse, respond_html, RawResponse, RequestContext, StringResponse} + page::Shell, + request::{assets::DataResponse, respond_html, RequestContext, StringResponse}, }; use maud::{html, PreEscaped}; -use rocket::{get, Data, State}; +use rocket::{get, State}; use crate::archive::{PathEntry, WebsiteArchive}; @@ -26,15 +27,17 @@ pub async fn render_page(content: PreEscaped, ctx: RequestContext) -> St #[get("/favicon/")] pub async fn favicon_route(domain: &str) -> Option { - let fav: Option<(Vec,)> = sqlx::query_as("SELECT favicon FROM site_favicon WHERE domain = $1") - .bind(domain) - .fetch_optional(get_pg!()).await.unwrap(); + let mut buf = Vec::new(); + std::fs::File::open(std::path::Path::new("./favicon").join(domain)) + .ok()? + .read_to_end(&mut buf) + .ok()?; - if let Some(fav_data) = fav { - return Some(DataResponse::new(fav_data.0, "image/x-icon", Some(60 * 60 * 24 * 5))); - } - - None + Some(DataResponse::new( + buf, + "image/x-icon", + Some(60 * 60 * 24 * 5), + )) } #[get("/")]