Add http routes for separate rating and userid
This commit is contained in:
@@ -1,3 +1,8 @@
|
|||||||
|
use std::sync::{
|
||||||
|
atomic::{AtomicI64, Ordering},
|
||||||
|
Arc,
|
||||||
|
};
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, State},
|
extract::{Path, State},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
@@ -12,18 +17,23 @@ use crate::database::Database;
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct SharedState {
|
struct SharedState {
|
||||||
database: Database,
|
database: Database,
|
||||||
|
rating: Arc<AtomicI64>,
|
||||||
mpris_sender: Sender<(String, String)>,
|
mpris_sender: Sender<(String, String)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn http_serve(database: &Database, mpris_producer: Sender<(String, String)>) {
|
pub async fn http_serve(database: &Database, mpris_producer: Sender<(String, String)>) {
|
||||||
let database = database.clone();
|
let database = database.clone();
|
||||||
|
let rating = Arc::new(AtomicI64::new(0));
|
||||||
let shared_state = SharedState {
|
let shared_state = SharedState {
|
||||||
database,
|
database,
|
||||||
|
rating,
|
||||||
mpris_sender: mpris_producer,
|
mpris_sender: mpris_producer,
|
||||||
};
|
};
|
||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/", get(root))
|
.route("/", get(root))
|
||||||
|
.route("/rating/{rating}", get(cache_rating_only))
|
||||||
|
.route("/userid/{user_id}", get(add_userid))
|
||||||
.route("/{user_id}/{rating}", get(add_rating))
|
.route("/{user_id}/{rating}", get(add_rating))
|
||||||
.with_state(shared_state);
|
.with_state(shared_state);
|
||||||
|
|
||||||
@@ -56,3 +66,29 @@ async fn add_rating(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn cache_rating_only(Path(rating): Path<i64>, State(shared): State<SharedState>) -> Response {
|
||||||
|
shared.rating.store(rating, Ordering::Relaxed);
|
||||||
|
(StatusCode::OK, "Done.").into_response()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn add_userid(Path(user_id): Path<i64>, State(shared): State<SharedState>) -> Response {
|
||||||
|
let mut mpris_rx = shared.mpris_sender.subscribe();
|
||||||
|
|
||||||
|
// Get the current interpret and track from the watch channel
|
||||||
|
let (interpret, track) = (*mpris_rx.borrow_and_update()).clone();
|
||||||
|
|
||||||
|
let rating = shared.rating.load(Ordering::Relaxed);
|
||||||
|
|
||||||
|
match shared
|
||||||
|
.database
|
||||||
|
.user_add_rating(user_id, &interpret, &track, rating)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => (StatusCode::OK, "Done.").into_response(),
|
||||||
|
Err(e) => {
|
||||||
|
println!("HTTP error: {e}");
|
||||||
|
(StatusCode::BAD_REQUEST, e.to_string()).into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user