Replace axum with Rocket
Axum is shit and doesn't work
This commit is contained in:
1 parent
ace0f4927a
commit
996504373e
7 files changed
+982
-467
No files matched your search
+82
-29
@@ -6,16 +6,16 @@ mod subsonic;
|
||||
mod track;
|
||||
|
||||
use crate::daemon::update_playlists_daemon;
|
||||
use crate::models::{PartialTrackedPlaylist, TrackedPlaylist};
|
||||
use crate::subsonic::SubsonicBuilder;
|
||||
use axum::extract::State;
|
||||
use axum::routing::{get, put};
|
||||
use axum::{extract, Json, Router};
|
||||
use crate::models::{CreateTrackedPlaylist, PartialTrackedPlaylist, TrackedPlaylist};
|
||||
use crate::subsonic::{Subsonic, SubsonicBuilder};
|
||||
use config::Config;
|
||||
use rocket::serde::json::{json, Json};
|
||||
use rocket::{get, post, routes, serde::json::Value as JsonValue, State};
|
||||
use serde::Serialize;
|
||||
use sqlx::postgres::PgPool;
|
||||
use sqlx::Error;
|
||||
use std::sync::OnceLock;
|
||||
use std::str::FromStr;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub static CONFIG: OnceLock<Config> = OnceLock::new();
|
||||
|
||||
@@ -41,23 +41,32 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.await?;
|
||||
sqlx::migrate!().run(&database).await?;
|
||||
|
||||
let media_client = SubsonicBuilder::new()
|
||||
.base(CONFIG.get().unwrap().get::<String>("subsonic.base")?)
|
||||
.username(CONFIG.get().unwrap().get::<String>("subsonic.username")?)
|
||||
.password(CONFIG.get().unwrap().get::<String>("subsonic.password")?)
|
||||
.build()?;
|
||||
let media_client = Arc::new(
|
||||
SubsonicBuilder::new()
|
||||
.base(CONFIG.get().unwrap().get::<String>("subsonic.base")?)
|
||||
.username(CONFIG.get().unwrap().get::<String>("subsonic.username")?)
|
||||
.password(CONFIG.get().unwrap().get::<String>("subsonic.password")?)
|
||||
.build()?,
|
||||
);
|
||||
|
||||
let _media_client = media_client.clone();
|
||||
tokio::spawn(async move {
|
||||
update_playlists_daemon(&media_client).await.unwrap();
|
||||
update_playlists_daemon(_media_client).await.unwrap();
|
||||
});
|
||||
|
||||
let app = Router::new()
|
||||
.route("/playlists", get(all_playlists))
|
||||
.route("/playlists", put(create_tracking_playlist))
|
||||
.with_state(database);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("localhost:3000").await?;
|
||||
axum::serve(listener, app).await?;
|
||||
rocket::build()
|
||||
.manage(media_client)
|
||||
.manage(database)
|
||||
.mount(
|
||||
"/",
|
||||
routes![
|
||||
create_tracking_playlist,
|
||||
add_tracking_playlist,
|
||||
all_playlists,
|
||||
],
|
||||
)
|
||||
.launch()
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -67,8 +76,16 @@ struct JsonError {
|
||||
error: String,
|
||||
}
|
||||
|
||||
impl From<Error> for JsonError {
|
||||
fn from(value: Error) -> Self {
|
||||
impl From<sqlx::Error> for JsonError {
|
||||
fn from(value: sqlx::Error) -> Self {
|
||||
Self {
|
||||
error: value.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<uuid::Error> for JsonError {
|
||||
fn from(value: uuid::Error) -> Self {
|
||||
Self {
|
||||
error: value.to_string(),
|
||||
}
|
||||
@@ -81,19 +98,55 @@ enum Response<T> {
|
||||
Error(JsonError),
|
||||
}
|
||||
|
||||
#[post("/create", data = "<create_playlist>")]
|
||||
async fn create_tracking_playlist(
|
||||
State(pool): State<PgPool>,
|
||||
extract::Json(partial): extract::Json<PartialTrackedPlaylist>,
|
||||
media_client: &State<Arc<Subsonic>>,
|
||||
pool: &State<PgPool>,
|
||||
create_playlist: Json<CreateTrackedPlaylist>,
|
||||
) -> Json<Response<TrackedPlaylist>> {
|
||||
match partial.record(&pool).await {
|
||||
match media_client
|
||||
.create_playlist(create_playlist.playlist_name.clone())
|
||||
.await
|
||||
{
|
||||
Ok(created_playlist) => match Uuid::from_str(&created_playlist.id) {
|
||||
Ok(playlist_id) => {
|
||||
let partial = PartialTrackedPlaylist {
|
||||
playlist_id,
|
||||
playlist_size: create_playlist.playlist_size,
|
||||
tracking_user: create_playlist.tracking_user.clone(),
|
||||
tracking_type: create_playlist.tracking_type.clone(),
|
||||
};
|
||||
|
||||
match partial.record(pool.inner()).await {
|
||||
Ok(playlist) => Json(Response::Success(playlist)),
|
||||
Err(e) => Json(Response::Error(e.into())),
|
||||
}
|
||||
}
|
||||
|
||||
Err(e) => Json(Response::Error(e.into())),
|
||||
},
|
||||
|
||||
Err(_) => Json(Response::Error(JsonError {
|
||||
error: "Couldn't create Subsonic playlist".to_string(),
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/add", data = "<playlist>")]
|
||||
async fn add_tracking_playlist(
|
||||
pool: &State<PgPool>,
|
||||
playlist: Json<PartialTrackedPlaylist>,
|
||||
) -> Json<Response<TrackedPlaylist>> {
|
||||
match playlist.record(pool.inner()).await {
|
||||
Ok(playlist) => Json(Response::Success(playlist)),
|
||||
Err(e) => Json(Response::Error(e.into())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn all_playlists(State(pool): State<PgPool>) -> Json<Vec<TrackedPlaylist>> {
|
||||
match TrackedPlaylist::all(&pool).await {
|
||||
Ok(playlists) => Json(playlists),
|
||||
Err(_) => Json(vec![]),
|
||||
#[get("/")]
|
||||
async fn all_playlists(pool: &State<PgPool>) -> JsonValue {
|
||||
match TrackedPlaylist::all(pool.inner()).await {
|
||||
Ok(playlists) => json!(playlists),
|
||||
Err(_) => json!([]),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user