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

Count number of hits

This commit is contained in:
Sayantan Santra 2023-04-03 15:46:22 -05:00
parent 046c6d63a4
commit 618fd0e53a
6 changed files with 102 additions and 67 deletions

4
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,4 @@
{
"editor.language.colorizedBracketPairs": [],
"editor.bracketPairColorization.enabled": true
}

100
actix/Cargo.lock generated
View file

@ -8,7 +8,7 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57a7559404a7f3573127aab53c08ce37a6c6a315c374a31070f3c91cd1b4a7fe" checksum = "57a7559404a7f3573127aab53c08ce37a6c6a315c374a31070f3c91cd1b4a7fe"
dependencies = [ dependencies = [
"bitflags", "bitflags 1.3.2",
"bytes", "bytes",
"futures-core", "futures-core",
"futures-sink", "futures-sink",
@ -30,7 +30,7 @@ dependencies = [
"actix-utils", "actix-utils",
"actix-web", "actix-web",
"askama_escape", "askama_escape",
"bitflags", "bitflags 1.3.2",
"bytes", "bytes",
"derive_more", "derive_more",
"futures-core", "futures-core",
@ -54,7 +54,7 @@ dependencies = [
"actix-utils", "actix-utils",
"ahash 0.8.3", "ahash 0.8.3",
"base64", "base64",
"bitflags", "bitflags 1.3.2",
"brotli", "brotli",
"bytes", "bytes",
"bytestring", "bytestring",
@ -283,6 +283,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "487f1e0fcbe47deb8b0574e646def1c903389d95241dd1bbcc6ce4a715dfc0c1"
[[package]] [[package]]
name = "block-buffer" name = "block-buffer"
version = "0.10.4" version = "0.10.4"
@ -420,6 +426,18 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "fallible-iterator"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "fallible-streaming-iterator"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.25" version = "1.0.25"
@ -520,6 +538,18 @@ name = "hashbrown"
version = "0.12.3" version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
dependencies = [
"ahash 0.7.6",
]
[[package]]
name = "hashlink"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69fe1fcf8b4278d860ad0548329f892a3631fb63f82574df68275f34cdbe0ffa"
dependencies = [
"hashbrown",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
@ -606,6 +636,16 @@ version = "0.2.140"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
[[package]]
name = "libsqlite3-sys"
version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afc22eff61b133b115c6e8c74e818c628d6d5e7a502afea6f64dee076dd94326"
dependencies = [
"pkg-config",
"vcpkg",
]
[[package]] [[package]]
name = "local-channel" name = "local-channel"
version = "0.1.3" version = "0.1.3"
@ -815,7 +855,7 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
dependencies = [ dependencies = [
"bitflags", "bitflags 1.3.2",
] ]
[[package]] [[package]]
@ -835,6 +875,20 @@ version = "0.6.29"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
[[package]]
name = "rusqlite"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "549b9d036d571d42e6e85d1c1425e2ac83491075078ca9a15be021c56b1641f2"
dependencies = [
"bitflags 2.0.2",
"fallible-iterator",
"fallible-streaming-iterator",
"hashlink",
"libsqlite3-sys",
"smallvec",
]
[[package]] [[package]]
name = "rustc_version" name = "rustc_version"
version = "0.4.0" version = "0.4.0"
@ -918,7 +972,7 @@ dependencies = [
"actix-files", "actix-files",
"actix-web", "actix-web",
"regex", "regex",
"sqlite", "rusqlite",
] ]
[[package]] [[package]]
@ -946,36 +1000,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "sqlite"
version = "0.30.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b1908664131c21a38e5b531344d52a196ec338af5bf44f7fa2c83d539e9561d"
dependencies = [
"libc",
"sqlite3-sys",
]
[[package]]
name = "sqlite3-src"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1815a7a02c996eb8e5c64f61fcb6fd9b12e593ce265c512c5853b2513635691"
dependencies = [
"cc",
"pkg-config",
]
[[package]]
name = "sqlite3-sys"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d47c99824fc55360ba00caf28de0b8a0458369b832e016a64c13af0ad9fbb9ee"
dependencies = [
"libc",
"sqlite3-src",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.109" version = "1.0.109"
@ -1128,6 +1152,12 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"

View file

@ -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"

View file

@ -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();
}

View file

@ -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()

View file

@ -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()
} }
} }