mirror of
https://github.com/SinTan1729/chhoto-url
synced 2024-12-26 07:38:36 -06:00
Count number of hits
This commit is contained in:
parent
046c6d63a4
commit
618fd0e53a
6 changed files with 102 additions and 67 deletions
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
|
@ -8,7 +8,7 @@ version = "0.5.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57a7559404a7f3573127aab53c08ce37a6c6a315c374a31070f3c91cd1b4a7fe"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bitflags 1.3.2",
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"futures-sink",
|
||||
|
@ -30,7 +30,7 @@ dependencies = [
|
|||
"actix-utils",
|
||||
"actix-web",
|
||||
"askama_escape",
|
||||
"bitflags",
|
||||
"bitflags 1.3.2",
|
||||
"bytes",
|
||||
"derive_more",
|
||||
"futures-core",
|
||||
|
@ -54,7 +54,7 @@ dependencies = [
|
|||
"actix-utils",
|
||||
"ahash 0.8.3",
|
||||
"base64",
|
||||
"bitflags",
|
||||
"bitflags 1.3.2",
|
||||
"brotli",
|
||||
"bytes",
|
||||
"bytestring",
|
||||
|
@ -283,6 +283,12 @@ version = "1.3.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "487f1e0fcbe47deb8b0574e646def1c903389d95241dd1bbcc6ce4a715dfc0c1"
|
||||
|
||||
[[package]]
|
||||
name = "block-buffer"
|
||||
version = "0.10.4"
|
||||
|
@ -420,6 +426,18 @@ dependencies = [
|
|||
"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]]
|
||||
name = "flate2"
|
||||
version = "1.0.25"
|
||||
|
@ -520,6 +538,18 @@ name = "hashbrown"
|
|||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "hermit-abi"
|
||||
|
@ -606,6 +636,16 @@ version = "0.2.140"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "local-channel"
|
||||
version = "0.1.3"
|
||||
|
@ -815,7 +855,7 @@ version = "0.2.16"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bitflags 1.3.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -835,6 +875,20 @@ version = "0.6.29"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "rustc_version"
|
||||
version = "0.4.0"
|
||||
|
@ -918,7 +972,7 @@ dependencies = [
|
|||
"actix-files",
|
||||
"actix-web",
|
||||
"regex",
|
||||
"sqlite",
|
||||
"rusqlite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -946,36 +1000,6 @@ dependencies = [
|
|||
"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]]
|
||||
name = "syn"
|
||||
version = "1.0.109"
|
||||
|
@ -1128,6 +1152,12 @@ dependencies = [
|
|||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.4"
|
||||
|
|
|
@ -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 a new issue