Display list of links
This commit is contained in:
@@ -21,3 +21,25 @@ pub fn find_url(shortlink: &str) -> String {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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))?
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user