mirror of
https://github.com/minoplhy/chhoto-url.git
synced 2024-11-22 01:06: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]
|
||||
actix-web = "4"
|
||||
actix-files = "0.6.2"
|
||||
sqlite = "0.30.4"
|
||||
rusqlite = "0.29.0"
|
||||
regex = "1.7.3"
|
||||
|
@ -1,45 +1,47 @@
|
||||
use sqlite::{open, Row};
|
||||
use rusqlite::Connection;
|
||||
|
||||
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
|
||||
.prepare(query)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.bind((1, shortlink))
|
||||
.unwrap()
|
||||
.map(|row| row.unwrap())
|
||||
.collect();
|
||||
let links = statement
|
||||
.query_map([shortlink], |row| Ok(row.get("long_url")?))
|
||||
.unwrap();
|
||||
|
||||
let mut longlink = "";
|
||||
if statement.len() == 1 {
|
||||
longlink = statement[0].read::<&str, _>("long_url");
|
||||
let mut longlink = "".to_string();
|
||||
for link in links {
|
||||
longlink = link.unwrap();
|
||||
}
|
||||
|
||||
String::from(longlink)
|
||||
add_hit(shortlink);
|
||||
longlink
|
||||
}
|
||||
|
||||
pub fn getall() -> Vec<String> {
|
||||
let db = open("./urls.sqlite").expect("Unable to open database!");
|
||||
let query = "SELECT * FROM urls";
|
||||
let db = Connection::open("./urls.sqlite").expect("Unable to open database!");
|
||||
let mut statement = db.prepare_cached("SELECT * FROM urls").unwrap();
|
||||
|
||||
let statement: Vec<Row> = db
|
||||
.prepare(query)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|row| row.unwrap())
|
||||
.collect();
|
||||
let mut data = statement.query([]).unwrap();
|
||||
|
||||
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");
|
||||
while let Some(row) = data.next().unwrap() {
|
||||
let short_url: String = row.get("short_url").unwrap();
|
||||
let long_url: String = row.get("long_url").unwrap();
|
||||
let hits: i64 = row.get("hits").unwrap();
|
||||
links.push(format!("{short_url},{long_url},{hits}"));
|
||||
}
|
||||
|
||||
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("/api/siteurl")]
|
||||
async fn siteurl() -> HttpResponse {
|
||||
let site_url = env::var("site_url").unwrap_or(String::from("unset"));
|
||||
println!("{site_url}");
|
||||
let site_url = env::var("site_url").unwrap_or("unset".to_string());
|
||||
HttpResponse::Ok().body(site_url)
|
||||
}
|
||||
|
||||
@ -38,7 +37,7 @@ async fn error404() -> impl Responder {
|
||||
#[get("/{shortlink}")]
|
||||
async fn link_handler(shortlink: web::Path<String>) -> impl Responder {
|
||||
let longlink = utils::get_longurl(shortlink);
|
||||
if longlink == String::from("") {
|
||||
if longlink == "".to_string() {
|
||||
Redirect::to("/err/404")
|
||||
} else {
|
||||
Redirect::to(longlink).permanent()
|
||||
|
@ -6,7 +6,7 @@ pub fn get_longurl(shortlink: web::Path<String>) -> String {
|
||||
if validate_link(&shortlink) {
|
||||
database::find_url(shortlink.as_str())
|
||||
} else {
|
||||
String::from("")
|
||||
"".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user