mirror of https://github.com/zer0bin-dev/zer0bin
feat: ✨ Total pastes badge
This commit is contained in:
parent
619b327ec2
commit
fd84b62cbd
|
@ -14,7 +14,7 @@ use config::Config;
|
|||
|
||||
use sqlx::{postgres::PgPoolOptions, PgPool};
|
||||
|
||||
use crate::routes::{get_paste, get_stats, get_version_badge, new_paste};
|
||||
use crate::routes::{get_paste, get_stats, get_total_pastes_badge, get_version_badge, new_paste};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
|
@ -64,7 +64,11 @@ async fn main() -> io::Result<()> {
|
|||
.service(get_paste)
|
||||
.service(new_paste), // .service(get_raw_paste),
|
||||
)
|
||||
.service(web::scope("/b").service(get_version_badge))
|
||||
.service(
|
||||
web::scope("/b")
|
||||
.service(get_version_badge)
|
||||
.service(get_total_pastes_badge),
|
||||
)
|
||||
})
|
||||
.bind(address)?
|
||||
.run()
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use actix_web::{
|
||||
get,
|
||||
post,
|
||||
get, post,
|
||||
web::{self},
|
||||
HttpResponse, Responder,
|
||||
};
|
||||
|
@ -228,3 +227,35 @@ pub async fn get_version_badge() -> impl Responder {
|
|||
|
||||
HttpResponse::Ok().content_type("image/svg+xml").body(badge)
|
||||
}
|
||||
|
||||
#[get("/t")]
|
||||
pub async fn get_total_pastes_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("total pastes")
|
||||
.message(&count.unwrap().to_string())
|
||||
.color_parse("#31748f")
|
||||
.label_color_parse("#191724")
|
||||
.style(Style::FlatSquare)
|
||||
.build()
|
||||
.expect("Failed to build badge")
|
||||
.svg();
|
||||
|
||||
HttpResponse::Ok().content_type("image/svg+xml").body(badge)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue