-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add routes for boosted ride activity (#4)
* Remove ls from dockerfile Fix issue with spotify history repeat inserting * spotify: Add recent listens route with limit param github: Add github route for pinned repos spotify: Stop from querying db every second, *clueless* * Add boosted api hooks Add boosted api endpoints Clean up json responses
- Loading branch information
1 parent
1ba3080
commit 8b30c04
Showing
13 changed files
with
221 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use actix_web::{web, Scope}; | ||
|
||
use crate::services; | ||
|
||
pub fn boosted_factory() -> Scope { | ||
web::scope("/boosted").service(services::boosted::routes::ride_stats) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod factory; | ||
pub mod routes; | ||
pub mod structs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use actix_web::{get, http::Error, web, HttpResponse}; | ||
use envconfig::Envconfig; | ||
use redis::aio::ConnectionManager; | ||
use serde_json::json; | ||
|
||
use crate::{ | ||
config::Config, services::boosted::structs::BoostedStats, ServerState, | ||
}; | ||
|
||
#[get("/stats")] | ||
async fn ride_stats( | ||
state: web::Data<ServerState>, | ||
) -> Result<HttpResponse, Error> { | ||
let config = Config::init_from_env().unwrap(); | ||
let valkey = &mut state.valkey.clone(); | ||
|
||
let in_ride = redis::cmd("GET") | ||
.arg("boosted/in-ride") | ||
.query_async::<ConnectionManager, String>(&mut valkey.cm) | ||
.await | ||
.unwrap_or(String::from("false")) | ||
== "true"; | ||
|
||
let client = reqwest::Client::new(); | ||
let res = client | ||
.get(format!("{}/v1/users/stats", config.boosted_api_endpoint)) | ||
.header("Authorization", config.boosted_api_token) | ||
.send() | ||
.await | ||
.unwrap(); | ||
|
||
let json = res.json::<BoostedStats>().await.unwrap(); | ||
|
||
Ok(HttpResponse::Ok().json(json!({"boosted": { | ||
"riding": in_ride, | ||
"latest_ride": json.latest_ride, | ||
"stats": json.stats | ||
}}))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct BoostedStats { | ||
pub latest_ride: RideStats, | ||
pub stats: Stats, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct RideStats { | ||
pub started_at: String, | ||
pub ended_at: String, | ||
pub duration: f64, | ||
pub distance: f64, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct Stats { | ||
pub boards: Boards, | ||
pub rides: ValueEntry, | ||
pub duration: ValueEntry, | ||
pub distance: ValueEntry, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct Boards { | ||
pub distance: f64, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct ValueEntry { | ||
pub day: f64, | ||
pub week: f64, | ||
pub month: f64, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use actix_web::{http::Error, post, web, HttpRequest, HttpResponse}; | ||
use envconfig::Envconfig as _; | ||
use redis::aio::ConnectionManager; | ||
|
||
use crate::{ | ||
config::Config, | ||
services::hooks::structs::{BoostedHookPayload, BoostedHookType}, | ||
ServerState, | ||
}; | ||
|
||
#[post("/boosted")] | ||
async fn execute( | ||
req: HttpRequest, | ||
state: web::Data<ServerState>, | ||
payload: web::Json<BoostedHookPayload>, | ||
) -> Result<HttpResponse, Error> { | ||
let config = Config::init_from_env().unwrap(); | ||
|
||
let auth_header = req.headers().get("authorization"); | ||
if auth_header.is_none() { | ||
return Ok(HttpResponse::BadRequest().finish()); | ||
} | ||
|
||
let auth_header = auth_header.unwrap().to_str().unwrap(); | ||
|
||
if auth_header != config.boosted_hook_token { | ||
return Ok(HttpResponse::BadRequest().finish()); | ||
} | ||
|
||
let valkey = &mut state.valkey.clone(); | ||
|
||
match payload.hook_type { | ||
BoostedHookType::RideStarted => { | ||
let _ = redis::cmd("SET") | ||
.arg("boosted/in-ride") | ||
.arg("true") | ||
.query_async::<ConnectionManager, String>(&mut valkey.cm) | ||
.await; | ||
} | ||
BoostedHookType::RideEnded => { | ||
let _ = redis::cmd("DEL") | ||
.arg("boosted/in-ride") | ||
.query_async::<ConnectionManager, String>(&mut valkey.cm) | ||
.await; | ||
} | ||
BoostedHookType::RideDiscarded => { | ||
let _ = redis::cmd("DEL") | ||
.arg("boosted/in-ride") | ||
.query_async::<ConnectionManager, String>(&mut valkey.cm) | ||
.await; | ||
} | ||
} | ||
|
||
Ok(HttpResponse::NoContent().finish()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use actix_web::{web, Scope}; | ||
|
||
use crate::services; | ||
|
||
pub fn hooks_factory() -> Scope { | ||
web::scope("/hooks").service(services::hooks::boosted::execute) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod boosted; | ||
pub mod factory; | ||
pub mod structs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use chrono::{DateTime, FixedOffset}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub enum BoostedHookType { | ||
#[serde(rename = "ride_started")] | ||
RideStarted, | ||
#[serde(rename = "ride_ended")] | ||
RideEnded, | ||
#[serde(rename = "ride_discarded")] | ||
RideDiscarded, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct RideSummary { | ||
pub ride_id: i64, | ||
pub distance: f64, | ||
pub max_speed: f64, | ||
pub avg_speed: f64, | ||
pub elevation_gain: Option<f64>, | ||
pub elevation_loss: Option<f64>, | ||
pub ride_points: usize, | ||
pub start_time: DateTime<FixedOffset>, | ||
pub end_time: DateTime<FixedOffset>, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct RideStartedHookBody { | ||
pub ride_id: i64, | ||
pub started_at: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct RideEndedHookBody { | ||
pub ride_id: i64, | ||
pub summary: RideSummary, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct RideDiscardedHookBody { | ||
pub ride_id: i64, | ||
pub code: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct BoostedHookPayload { | ||
pub hook_type: BoostedHookType, | ||
pub body: serde_json::Value, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
pub mod analytics; | ||
pub mod base; | ||
pub mod blog; | ||
pub mod boosted; | ||
pub mod github; | ||
pub mod hooks; | ||
pub mod spotify; | ||
pub mod uploads; | ||
pub mod weather; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters