feat: 🚧 Badges

This commit is contained in:
ThatOneCalculator 2022-03-22 14:37:53 -07:00
parent 5948dcfda5
commit e9c4842719
3 changed files with 106 additions and 16 deletions

52
backend/Cargo.lock generated Executable file → Normal file
View File

@ -71,7 +71,7 @@ dependencies = [
"http",
"httparse",
"httpdate",
"itoa",
"itoa 1.0.1",
"language-tags",
"local-channel",
"log",
@ -182,7 +182,7 @@ dependencies = [
"encoding_rs",
"futures-core",
"futures-util",
"itoa",
"itoa 1.0.1",
"language-tags",
"log",
"mime",
@ -274,6 +274,7 @@ dependencies = [
"actix-governor",
"actix-rt",
"actix-web",
"badge-maker",
"chrono",
"nanoid",
"serde",
@ -281,12 +282,37 @@ dependencies = [
"sqlx",
]
[[package]]
name = "badge-maker"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "383647c1ae7388c2801ef995f6de2f9551a361ad637f2a0e4fea41c51dd37b81"
dependencies = [
"aho-corasick",
"bincode",
"itoa 0.4.8",
"lazy_static",
"regex",
"seahash",
"serde",
"thiserror",
]
[[package]]
name = "base64"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]]
name = "bincode"
version = "1.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
dependencies = [
"serde",
]
[[package]]
name = "bitflags"
version = "1.3.2"
@ -819,7 +845,7 @@ checksum = "31f4c6746584866f0feabcc69893c5b51beef3831656a968ed7ae254cdc4fd03"
dependencies = [
"bytes",
"fnv",
"itoa",
"itoa 1.0.1",
]
[[package]]
@ -873,6 +899,12 @@ dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4"
[[package]]
name = "itoa"
version = "1.0.1"
@ -1364,6 +1396,12 @@ dependencies = [
"untrusted",
]
[[package]]
name = "seahash"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b"
[[package]]
name = "semver"
version = "1.0.6"
@ -1396,7 +1434,7 @@ version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95"
dependencies = [
"itoa",
"itoa 1.0.1",
"ryu",
"serde",
]
@ -1408,7 +1446,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd"
dependencies = [
"form_urlencoded",
"itoa",
"itoa 1.0.1",
"ryu",
"serde",
]
@ -1533,7 +1571,7 @@ dependencies = [
"hex",
"hmac",
"indexmap",
"itoa",
"itoa 1.0.1",
"libc",
"log",
"md-5",
@ -1652,7 +1690,7 @@ version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "004cbc98f30fa233c61a38bc77e96a9106e65c88f2d3bef182ae952027e5753d"
dependencies = [
"itoa",
"itoa 1.0.1",
"libc",
"num_threads",
"time-macros",

View File

@ -14,6 +14,8 @@ actix-web = "4.0.0-rc.1"
actix-cors = "0.6.0"
actix-governor = "0.3.0"
badge-maker = "0.2.1"
sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "postgres", "chrono"] }
chrono = { version = "0.4.19", features = ["serde"] }

View File

@ -4,8 +4,9 @@ use actix_web::{
HttpResponse, Responder,
};
use chrono::Duration;
use badge_maker::BadgeBuilder;
use chrono::Duration;
use nanoid::nanoid;
use sqlx::{postgres::PgRow, types::chrono::Utc, Row};
@ -42,11 +43,63 @@ pub async fn get_stats(state: web::Data<AppState>) -> impl Responder {
success: true,
data: GetStatsResponse {
count: count.unwrap(),
version
version,
},
})
}
#[get("/s/version")]
pub async fn get_version_badge(state: web::Data<AppState>) -> impl Responder {
let count: Result<i64, sqlx::Error> = sqlx::query(r#"SELECT COUNT(*) FROM pastes"#)
.try_map(|row: PgRow| row.try_get::<i64, _>("count"))
.fetch_one(&state.pool)
.await;
if let Err(e) = count {
eprintln!("Error occurred while retrieving paste count: {:?}", e);
return HttpResponse::InternalServerError().json(ApiResponse {
success: false,
data: ApiError {
message: "Error occurred while retrieving paste count, please try again."
.to_string(),
},
});
}
let badge = BadgeBuilder::new()
.label("paste count")
.message(&count.unwrap().to_string())
.color_parse("#31748f")
.build()?
.svg();
HttpResponse::Ok().ApiResponse {
success: true,
content_type: "image/svg",
body: badge
}
}
#[get("/s/version")]
pub async fn get_count_badge(state: web::Data<AppState>) -> impl Responder {
let version = env!("CARGO_PKG_VERSION").to_string();
let badge = BadgeBuilder::new()
.label("version")
.message(&version)
.color_parse("#31748f")
.build()?
.svg();
HttpResponse::Ok().ApiResponse {
success: true,
content_type: "image/svg",
body: badge
}
}
#[get("/{id}")]
pub async fn get_paste(state: web::Data<AppState>, id: web::Path<String>) -> impl Responder {
let id = id.into_inner();
@ -186,14 +239,11 @@ pub async fn new_paste(
if state.config.logging.on_post_paste {
println!("[POST] id={} length={}", id, content.len());
}
HttpResponse::Ok().json(ApiResponse {
success: true,
data: NewPasteResponse {
id,
content,
},
})},
success: true,
data: NewPasteResponse { id, content },
})
}
Err(e) => {
eprintln!("Error occurred while creating paste: {:?}", e);