1
0
Fork 0
mirror of https://github.com/SinTan1729/chhoto-url synced 2024-12-26 23:58:35 -06:00

Display list of links

This commit is contained in:
Sayantan Santra 2023-04-03 11:55:27 -05:00
parent b9d76b6734
commit a1f73c8a9d
6 changed files with 36 additions and 7 deletions

View file

@ -14,9 +14,10 @@ COPY ./actix/resources ./resources
RUN cargo build --release RUN cargo build --release
FROM gcr.io/distroless/cc-debian10 FROM frolvlad/alpine-glibc:latest
EXPOSE 2000 EXPOSE 2000
RUN apk add sqlite-libs
WORKDIR /opt WORKDIR /opt

1
actix/Cargo.lock generated
View file

@ -919,7 +919,6 @@ dependencies = [
"actix-web", "actix-web",
"regex", "regex",
"sqlite", "sqlite",
"sqlite3-src",
] ]
[[package]] [[package]]

View file

@ -10,7 +10,3 @@ actix-web = "4"
actix-files = "0.6.2" actix-files = "0.6.2"
sqlite = "0.30.4" sqlite = "0.30.4"
regex = "1.7.3" regex = "1.7.3"
[dependencies.sqlite3-src]
version="0.4.0"
features=["bundled"]

View file

@ -21,3 +21,25 @@ pub fn find_url(shortlink: &str) -> String {
String::from(longlink) String::from(longlink)
} }
pub fn getall() -> Vec<String> {
let db = open("./urls.sqlite").expect("Unable to open database!");
let query = "SELECT * FROM urls";
let statement: Vec<Row> = db
.prepare(query)
.unwrap()
.into_iter()
.map(|row| row.unwrap())
.collect();
let mut links: Vec<String> = 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::<i64, _>("hits");
links.push(format!("{short_url},{long_url},{hits}"));
}
links
}

View file

@ -2,7 +2,7 @@ use actix_files::{Files, NamedFile};
use actix_web::{ use actix_web::{
get, get,
web::{self, Redirect}, web::{self, Redirect},
App, HttpServer, Responder, App, HttpResponse, HttpServer, Responder,
}; };
mod database; mod database;
mod utils; mod utils;
@ -13,6 +13,11 @@ mod utils;
// Return all active links // Return all active links
#[get("/api/all")]
async fn getall() -> HttpResponse {
HttpResponse::Ok().body(utils::getall())
}
// 404 error page // 404 error page
#[get("/err/404")] #[get("/err/404")]
async fn error404() -> impl Responder { async fn error404() -> impl Responder {
@ -36,6 +41,7 @@ async fn main() -> std::io::Result<()> {
App::new() App::new()
.service(link_handler) .service(link_handler)
.service(error404) .service(error404)
.service(getall)
.service(Files::new("/", "./resources/").index_file("index.html")) .service(Files::new("/", "./resources/").index_file("index.html"))
}) })
.bind(("0.0.0.0", 2000))? .bind(("0.0.0.0", 2000))?

View file

@ -14,3 +14,8 @@ fn validate_link(link: &str) -> bool {
let re = Regex::new("[a-z0-9-_]+").unwrap(); let re = Regex::new("[a-z0-9-_]+").unwrap();
re.is_match(link) re.is_match(link)
} }
pub fn getall() -> String {
let links = database::getall();
links.join("\n")
}