Skip to content

Commit

Permalink
Axum stream and static routes (#578)
Browse files Browse the repository at this point in the history
* Migrate stream routes to Axum

* Cleanup a lot of unnecessary cloning

* Migrate static routes to Axum

* Remove warp

* embed_ui feature added to dim-web build.rs
  • Loading branch information
niamu authored Nov 24, 2023
1 parent af6df1c commit b13b745
Show file tree
Hide file tree
Showing 27 changed files with 551 additions and 1,133 deletions.
68 changes: 3 additions & 65 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions dim-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ license.workspace = true
[features]
vaapi = ["nightfall/vaapi"]

# the build script usually will enable this if `yarn` is installed.
# If enabled explicitly, the build script will panic if something went wrong.
embed_ui = []

[dependencies]
# git dependencies
nightfall = { git = "https://github.com/Dusk-Labs/nightfall", tag = "0.3.12-rc4", default-features = false, features = [
Expand All @@ -36,11 +32,11 @@ serde_derive = "^1.0.125"
serde_json = "^1.0.64"

async-trait = "0.1.50"
axum = { version = "0.6.20" }
cfg-if = "1.0.0"
chrono = { version = "0.4.19", features = ["serde"] }
dia-i18n = "0.10.0"
displaydoc = "0.2.3"
dominant_color = "0.3.0"
futures = "0.3.14"
fuzzy-matcher = "0.3.7"
http = "^0.2.3"
Expand Down Expand Up @@ -77,7 +73,6 @@ tracing-subscriber = { version = "^0.3.10", features = [
] }
url = "2.2.2"
uuid = { version = "1.2.2", features = ["v4"] }
warp = { version = "0.3.3", features = ["tls", "tokio-rustls"] }
xmlwriter = "0.1.0"
xtra = { version = "0.5.1", features = ["tokio", "with-tokio-1"] }

Expand Down
9 changes: 0 additions & 9 deletions dim-core/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
use std::env;
use std::path::Path;

fn main() {
let out_dir = env::var("CARGO_TARGET_DIR").unwrap();
let db_file = format!("{out_dir}/dim_dev.db");
println!("cargo:rustc-env=DATABASE_URL=sqlite://{db_file}");

if Path::new("../ui/build").exists() {
println!("cargo:rustc-cfg=feature=\"embed_ui\"");
} else {
println!("cargo:warning=`ui/build` does not exist.");
println!("cargo:warning=If you wish to embed the webui, run `yarn build` in `ui`.");
}

println!("cargo:rerun-if-changed=ui/build");
println!("cargo:rerun-if-changed=build.rs");
}
81 changes: 35 additions & 46 deletions dim-core/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use axum::response::IntoResponse;
use axum::response::Response;
use dim_database::DatabaseError;
use displaydoc::Display;
use thiserror::Error;

use serde::Serialize;
use serde_json::json;

use nightfall::error::NightfallError;

Expand Down Expand Up @@ -91,44 +92,39 @@ impl From<std::io::Error> for DimError {
}
}

impl warp::reject::Reject for DimError {}

impl warp::Reply for DimError {
fn into_response(self) -> warp::reply::Response {
let status = match self {
impl IntoResponse for DimError {
fn into_response(self) -> Response {
match self {
Self::LibraryNotFound
| Self::NoneError
| Self::NotFoundError
| Self::ExternalSearchError(_) => StatusCode::NOT_FOUND,
| Self::ExternalSearchError(_) => {
(StatusCode::NOT_FOUND, self.to_string()).into_response()
},
Self::StreamingError(_)
| Self::DatabaseError { .. }
| Self::UnknownError
| Self::IOError
| Self::InternalServerError
| Self::UploadFailed
| Self::ScannerError(_) => StatusCode::INTERNAL_SERVER_ERROR,
| Self::ScannerError(_) => {
(StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response()
},
Self::Unauthenticated
| Self::Unauthorized
| Self::InvalidCredentials
| Self::CookieError(_)
| Self::NoToken
| Self::UserNotFound => StatusCode::UNAUTHORIZED,
Self::UsernameNotAvailable => StatusCode::BAD_REQUEST,
| Self::UserNotFound => {
(StatusCode::UNAUTHORIZED, self.to_string()).into_response()
},
Self::UsernameNotAvailable => {
(StatusCode::BAD_REQUEST, self.to_string()).into_response()
},
Self::UnsupportedFile | Self::InvalidMediaType | Self::MissingFieldInBody { .. } => {
StatusCode::NOT_ACCEPTABLE
(StatusCode::NOT_ACCEPTABLE, self.to_string()).into_response()
}
};

let resp = json!({
"error": json!(&self)["error"],
"messsage": self.to_string(),
});

warp::http::Response::builder()
.status(status)
.header("ContentType", "application/json")
.body(serde_json::to_string(&resp).unwrap().into())
.unwrap()
}
}
}

Expand Down Expand Up @@ -173,31 +169,24 @@ impl From<NightfallError> for StreamingErrors {
}
}

impl warp::reject::Reject for StreamingErrors {}

impl warp::Reply for StreamingErrors {
fn into_response(self) -> warp::reply::Response {
let status = match self {
Self::OtherNightfall(NightfallError::ChunkNotDone) => StatusCode::PROCESSING,
Self::NoMediaFileFound(_) | Self::FileDoesNotExist => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR,
};

let resp = json!({
"error": json!(&self)["error"],
"messsage": self.to_string(),
});

warp::http::Response::builder()
.status(status)
.header("ContentType", "application/json")
.body(serde_json::to_string(&resp).unwrap().into())
.unwrap()
}
}

impl From<std::io::Error> for StreamingErrors {
fn from(_: std::io::Error) -> Self {
Self::ProcFailed
}
}

impl IntoResponse for StreamingErrors {
fn into_response(self) -> Response {
match self {
Self::OtherNightfall(NightfallError::ChunkNotDone) => {
(StatusCode::PROCESSING, self.to_string()).into_response()
}
Self::NoMediaFileFound(_) | Self::FileDoesNotExist => {
(StatusCode::NOT_FOUND, self.to_string()).into_response()
}
_ => {
(StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response()
}
}
}
}
2 changes: 0 additions & 2 deletions dim-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ pub mod errors;
pub mod fetcher;
/// Inspect api for Result type
pub mod inspect;
/// Contains our custom logger for rocket
pub mod logger;
/// Sqlite CDC implementation
pub mod reactor;
/// Contains all of the routes exposed by the webapi.
Expand Down
35 changes: 0 additions & 35 deletions dim-core/src/logger.rs

This file was deleted.

Loading

0 comments on commit b13b745

Please sign in to comment.