mirror of
https://github.com/minoplhy/chhoto-url.git
synced 2024-11-22 09:16:46 +00:00
Count number of hits
This commit is contained in:
parent
046c6d63a4
commit
618fd0e53a
4
.vscode/settings.json
vendored
Normal file
4
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"editor.language.colorizedBracketPairs": [],
|
||||||
|
"editor.bracketPairColorization.enabled": true
|
||||||
|
}
|
100
actix/Cargo.lock
generated
100
actix/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -8,5 +8,5 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4"
|
actix-web = "4"
|
||||||
actix-files = "0.6.2"
|
actix-files = "0.6.2"
|
||||||
sqlite = "0.30.4"
|
rusqlite = "0.29.0"
|
||||||
regex = "1.7.3"
|
regex = "1.7.3"
|
||||||
|
@ -1,45 +1,47 @@
|
|||||||
use sqlite::{open, Row};
|
use rusqlite::Connection;
|
||||||
|
|
||||||
pub fn find_url(shortlink: &str) -> String {
|
pub fn find_url(shortlink: &str) -> String {
|
||||||
let db = open("./urls.sqlite").expect("Unable to open database!");
|
let db = Connection::open("./urls.sqlite").expect("Unable to open database!");
|
||||||
|
|
||||||
let query = "SELECT long_url FROM urls WHERE short_url = ?";
|
let mut statement = db
|
||||||
|
.prepare_cached("SELECT long_url FROM urls WHERE short_url = ?1")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let statement: Vec<Row> = db
|
let links = statement
|
||||||
.prepare(query)
|
.query_map([shortlink], |row| Ok(row.get("long_url")?))
|
||||||
.unwrap()
|
.unwrap();
|
||||||
.into_iter()
|
|
||||||
.bind((1, shortlink))
|
|
||||||
.unwrap()
|
|
||||||
.map(|row| row.unwrap())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let mut longlink = "";
|
let mut longlink = "".to_string();
|
||||||
if statement.len() == 1 {
|
for link in links {
|
||||||
longlink = statement[0].read::<&str, _>("long_url");
|
longlink = link.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
String::from(longlink)
|
add_hit(shortlink);
|
||||||
|
longlink
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getall() -> Vec<String> {
|
pub fn getall() -> Vec<String> {
|
||||||
let db = open("./urls.sqlite").expect("Unable to open database!");
|
let db = Connection::open("./urls.sqlite").expect("Unable to open database!");
|
||||||
let query = "SELECT * FROM urls";
|
let mut statement = db.prepare_cached("SELECT * FROM urls").unwrap();
|
||||||
|
|
||||||
let statement: Vec<Row> = db
|
let mut data = statement.query([]).unwrap();
|
||||||
.prepare(query)
|
|
||||||
.unwrap()
|
|
||||||
.into_iter()
|
|
||||||
.map(|row| row.unwrap())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let mut links: Vec<String> = Vec::new();
|
let mut links: Vec<String> = Vec::new();
|
||||||
for row in statement {
|
while let Some(row) = data.next().unwrap() {
|
||||||
let short_url = row.read::<&str, _>("short_url");
|
let short_url: String = row.get("short_url").unwrap();
|
||||||
let long_url = row.read::<&str, _>("long_url");
|
let long_url: String = row.get("long_url").unwrap();
|
||||||
let hits = row.read::<i64, _>("hits");
|
let hits: i64 = row.get("hits").unwrap();
|
||||||
links.push(format!("{short_url},{long_url},{hits}"));
|
links.push(format!("{short_url},{long_url},{hits}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
links
|
links
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_hit(shortlink: &str) -> () {
|
||||||
|
let db = Connection::open("./urls.sqlite").expect("Unable to open database!");
|
||||||
|
db.execute(
|
||||||
|
"UPDATE urls SET hits = hits + 1 WHERE short_url = ?1",
|
||||||
|
[shortlink],
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
@ -23,8 +23,7 @@ async fn getall() -> HttpResponse {
|
|||||||
// Get the site URL
|
// Get the site URL
|
||||||
#[get("/api/siteurl")]
|
#[get("/api/siteurl")]
|
||||||
async fn siteurl() -> HttpResponse {
|
async fn siteurl() -> HttpResponse {
|
||||||
let site_url = env::var("site_url").unwrap_or(String::from("unset"));
|
let site_url = env::var("site_url").unwrap_or("unset".to_string());
|
||||||
println!("{site_url}");
|
|
||||||
HttpResponse::Ok().body(site_url)
|
HttpResponse::Ok().body(site_url)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,7 +37,7 @@ async fn error404() -> impl Responder {
|
|||||||
#[get("/{shortlink}")]
|
#[get("/{shortlink}")]
|
||||||
async fn link_handler(shortlink: web::Path<String>) -> impl Responder {
|
async fn link_handler(shortlink: web::Path<String>) -> impl Responder {
|
||||||
let longlink = utils::get_longurl(shortlink);
|
let longlink = utils::get_longurl(shortlink);
|
||||||
if longlink == String::from("") {
|
if longlink == "".to_string() {
|
||||||
Redirect::to("/err/404")
|
Redirect::to("/err/404")
|
||||||
} else {
|
} else {
|
||||||
Redirect::to(longlink).permanent()
|
Redirect::to(longlink).permanent()
|
||||||
|
@ -6,7 +6,7 @@ pub fn get_longurl(shortlink: web::Path<String>) -> String {
|
|||||||
if validate_link(&shortlink) {
|
if validate_link(&shortlink) {
|
||||||
database::find_url(shortlink.as_str())
|
database::find_url(shortlink.as_str())
|
||||||
} else {
|
} else {
|
||||||
String::from("")
|
"".to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user