@@ -2,6 +2,7 @@ use crate::config::AppData;
22use crate :: endpoints:: ApiError ;
33use actix_web:: { HttpResponse , Responder , get, web} ;
44use serde:: Deserialize ;
5+ use utoipa:: { IntoParams , ToSchema } ;
56
67use std:: fs;
78use std:: path:: Path ;
@@ -10,9 +11,18 @@ use urlencoding;
1011const LABEL_COLOR : & str = "#0c0811" ;
1112const STAT_COLOR : & str = "#5f3d84" ;
1213
13- #[ derive( Deserialize ) ]
14+ #[ derive( Deserialize , Clone , Copy , PartialEq , Eq , ToSchema ) ]
15+ #[ serde( rename_all = "snake_case" ) ]
16+ pub enum StatusBadgeStat {
17+ Version ,
18+ GdVersion ,
19+ GeodeVersion ,
20+ Downloads ,
21+ }
22+
23+ #[ derive( Deserialize , IntoParams ) ]
1424pub struct StatusBadgeQuery {
15- pub stat : String ,
25+ pub stat : StatusBadgeStat ,
1626}
1727
1828#[ utoipa:: path(
@@ -21,7 +31,7 @@ pub struct StatusBadgeQuery {
2131 tag = "mods" ,
2232 params(
2333 ( "id" = String , Path , description = "Mod ID" ) ,
24- ( "stat" = String , Query , description = "Stat to display: version, gd_version, geode_version, downloads" )
34+ StatusBadgeQuery
2535 ) ,
2636 responses(
2737 ( status = 302 , description = "Redirect to Shields.io badge" ) ,
@@ -31,40 +41,36 @@ pub struct StatusBadgeQuery {
3141) ]
3242#[ get( "/v1/mods/{id}/status_badge" ) ]
3343pub async fn status_badge (
34- _data : web:: Data < AppData > ,
44+ data : web:: Data < AppData > ,
3545 id : web:: Path < String > ,
3646 query : web:: Query < StatusBadgeQuery > ,
3747) -> Result < impl Responder , ApiError > {
38- let ( stat, label, svg_path) = match query. stat . as_str ( ) {
39- "version" => (
48+ let ( stat, label, svg_path) = match query. stat {
49+ StatusBadgeStat :: Version => (
4050 "payload.versions[0].version" ,
4151 "Version" ,
4252 "static/mod_version.svg" ,
4353 ) ,
44- "gd_version" => (
54+ StatusBadgeStat :: GdVersion => (
4555 "payload.versions[0].gd.win" ,
4656 "Geometry Dash" ,
4757 "static/mod_gd_version.svg" ,
4858 ) ,
49- "geode_version" => (
59+ StatusBadgeStat :: GeodeVersion => (
5060 "payload.versions[0].geode" ,
5161 "Geode" ,
5262 "static/mod_geode_version.svg" ,
5363 ) ,
54- "downloads" => (
64+ StatusBadgeStat :: Downloads => (
5565 "payload.download_count" ,
5666 "Downloads" ,
5767 "static/mod_downloads.svg" ,
5868 ) ,
59- _ => return Err ( ApiError :: BadRequest ( "Invalid stat parameter" . into ( ) ) ) ,
6069 } ;
6170 let svg = fs:: read_to_string ( Path :: new ( svg_path) )
6271 . map_err ( |_| ApiError :: BadRequest ( format ! ( "Could not read SVG file: {}" , svg_path) ) ) ?;
63- let api_url = format ! (
64- "{}/v1/mods/{}?abbreviate=true" ,
65- "http://api.geode-sdk.org" , id
66- ) ;
67- let mod_link = format ! ( "https://geode-sdk.org/mods/{}" , id) ;
72+ let api_url = format ! ( "{}/v1/mods/{}?abbreviate=true" , data. app_url( ) , id) ;
73+ let mod_link = format ! ( "{}/mods/{}" , data. front_url( ) , id) ;
6874 let svg_data_url = format ! ( "data:image/svg+xml;utf8,{}" , urlencoding:: encode( & svg) ) ;
6975 let shields_url = format ! (
7076 "https://img.shields.io/badge/dynamic/json?url={}&query={}&label={}&labelColor={}&color={}&link={}&style=plastic&logo={}" ,
0 commit comments