From a1f73c8a9dff39a9a6a782a3369891cd6e92955b Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Mon, 3 Apr 2023 11:55:27 -0500 Subject: [PATCH] Display list of links --- Dockerfile | 3 ++- actix/Cargo.lock | 1 - actix/Cargo.toml | 4 ---- actix/src/database.rs | 22 ++++++++++++++++++++++ actix/src/main.rs | 8 +++++++- actix/src/utils.rs | 5 +++++ 6 files changed, 36 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 397528e..1c5934b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,9 +14,10 @@ COPY ./actix/resources ./resources RUN cargo build --release -FROM gcr.io/distroless/cc-debian10 +FROM frolvlad/alpine-glibc:latest EXPOSE 2000 +RUN apk add sqlite-libs WORKDIR /opt diff --git a/actix/Cargo.lock b/actix/Cargo.lock index d2b7191..af0f0a6 100644 --- a/actix/Cargo.lock +++ b/actix/Cargo.lock @@ -919,7 +919,6 @@ dependencies = [ "actix-web", "regex", "sqlite", - "sqlite3-src", ] [[package]] diff --git a/actix/Cargo.toml b/actix/Cargo.toml index 6166161..e13a27f 100644 --- a/actix/Cargo.toml +++ b/actix/Cargo.toml @@ -10,7 +10,3 @@ actix-web = "4" actix-files = "0.6.2" sqlite = "0.30.4" regex = "1.7.3" - -[dependencies.sqlite3-src] -version="0.4.0" -features=["bundled"] \ No newline at end of file diff --git a/actix/src/database.rs b/actix/src/database.rs index ddcf0b8..97987e5 100644 --- a/actix/src/database.rs +++ b/actix/src/database.rs @@ -21,3 +21,25 @@ pub fn find_url(shortlink: &str) -> String { String::from(longlink) } + +pub fn getall() -> Vec { + let db = open("./urls.sqlite").expect("Unable to open database!"); + let query = "SELECT * FROM urls"; + + let statement: Vec = db + .prepare(query) + .unwrap() + .into_iter() + .map(|row| row.unwrap()) + .collect(); + + let mut links: Vec = Vec::new(); + for row in statement { + let short_url = row.read::<&str, _>("short_url"); + let long_url = row.read::<&str, _>("long_url"); + let hits = row.read::("hits"); + links.push(format!("{short_url},{long_url},{hits}")); + } + + links +} diff --git a/actix/src/main.rs b/actix/src/main.rs index 68c494e..9197361 100644 --- a/actix/src/main.rs +++ b/actix/src/main.rs @@ -2,7 +2,7 @@ use actix_files::{Files, NamedFile}; use actix_web::{ get, web::{self, Redirect}, - App, HttpServer, Responder, + App, HttpResponse, HttpServer, Responder, }; mod database; mod utils; @@ -13,6 +13,11 @@ mod utils; // Return all active links +#[get("/api/all")] +async fn getall() -> HttpResponse { + HttpResponse::Ok().body(utils::getall()) +} + // 404 error page #[get("/err/404")] async fn error404() -> impl Responder { @@ -36,6 +41,7 @@ async fn main() -> std::io::Result<()> { App::new() .service(link_handler) .service(error404) + .service(getall) .service(Files::new("/", "./resources/").index_file("index.html")) }) .bind(("0.0.0.0", 2000))? diff --git a/actix/src/utils.rs b/actix/src/utils.rs index 18182ba..b28382a 100644 --- a/actix/src/utils.rs +++ b/actix/src/utils.rs @@ -14,3 +14,8 @@ fn validate_link(link: &str) -> bool { let re = Regex::new("[a-z0-9-_]+").unwrap(); re.is_match(link) } + +pub fn getall() -> String { + let links = database::getall(); + links.join("\n") +}