Skip to content

Commit

Permalink
move everything out of app
Browse files Browse the repository at this point in the history
  • Loading branch information
vnghia committed Sep 14, 2024
1 parent 6aadba3 commit 9be448f
Show file tree
Hide file tree
Showing 24 changed files with 105 additions and 149 deletions.
1 change: 0 additions & 1 deletion nghe-backend/src/app/common/mod.rs

This file was deleted.

36 changes: 0 additions & 36 deletions nghe-backend/src/app/mod.rs

This file was deleted.

16 changes: 0 additions & 16 deletions nghe-backend/src/app/state/mod.rs

This file was deleted.

33 changes: 18 additions & 15 deletions nghe-backend/src/app/auth/mod.rs → nghe-backend/src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use nghe_api::auth::{Auth, BinaryRequest};
use nghe_api::common::Endpoint;
use uuid::Uuid;

use super::state::{App, Database};
use crate::database::Database;
use crate::orm::users;
use crate::Error;

Expand Down Expand Up @@ -52,24 +52,27 @@ async fn authenticate(
}
}

async fn json_authenticate<S>(query: &str, state: &S) -> Result<(App, Uuid, users::Role), Error>
async fn json_authenticate<S>(
query: &str,
state: &S,
) -> Result<(Database, Uuid, users::Role), Error>
where
App: FromRef<S>,
Database: FromRef<S>,
{
let auth: Auth = serde_html_form::from_str(query)
.map_err(|_| Error::SerializeRequest("invalid auth parameters"))?;

let app = App::from_ref(state);
let (id, role) = authenticate(&app.database, auth).await?;
let database = Database::from_ref(state);
let (id, role) = authenticate(&database, auth).await?;

Ok((app, id, role))
Ok((database, id, role))
}

#[async_trait::async_trait]
impl<S, R> FromRequest<S> for GetUser<R>
where
S: Send + Sync,
App: FromRef<S>,
Database: FromRef<S>,
R: Endpoint + Authorize + Send,
{
type Rejection = Error;
Expand All @@ -93,7 +96,7 @@ where
impl<S, R> FromRequest<S> for PostUser<R>
where
S: Send + Sync,
App: FromRef<S>,
Database: FromRef<S>,
R: Endpoint + Authorize + Send,
{
type Rejection = Error;
Expand All @@ -116,7 +119,7 @@ where
impl<S, R, const NEED_AUTH: bool> FromRequest<S> for BinaryUser<R, NEED_AUTH>
where
S: Send + Sync,
App: FromRef<S>,
Database: FromRef<S>,
R: Endpoint + Authorize + Send,
{
type Rejection = Error;
Expand All @@ -129,8 +132,8 @@ where
let BinaryRequest::<R> { auth, request } = bitcode::decode(&bytes)
.map_err(|_| Error::SerializeRequest("invalid request body"))?;

let app = App::from_ref(state);
let (id, role) = authenticate(&app.database, auth).await?;
let database = Database::from_ref(state);
let (id, role) = authenticate(&database, auth).await?;
Ok(Self { id, request: request.authorize(role)? })
} else {
Ok(Self {
Expand Down Expand Up @@ -245,7 +248,7 @@ mod tests {
.unwrap();

let test_request =
GetUser::<Request>::from_request(http_request, &mock.state()).await.unwrap();
GetUser::<Request>::from_request(http_request, mock.state()).await.unwrap();
assert_eq!(user.user.id, test_request.id);
assert_eq!(request, test_request.request);
}
Expand Down Expand Up @@ -274,7 +277,7 @@ mod tests {
.unwrap();

let test_request =
PostUser::<Request>::from_request(http_request, &mock.state()).await.unwrap();
PostUser::<Request>::from_request(http_request, mock.state()).await.unwrap();
assert_eq!(user.user.id, test_request.id);
assert_eq!(request, test_request.request);
}
Expand All @@ -298,7 +301,7 @@ mod tests {
.unwrap();

let test_request =
BinaryUser::<Request, true>::from_request(http_request, &mock.state()).await.unwrap();
BinaryUser::<Request, true>::from_request(http_request, mock.state()).await.unwrap();
assert_eq!(user.user.id, test_request.id);
assert_eq!(request, test_request.request);
}
Expand All @@ -318,7 +321,7 @@ mod tests {
.unwrap();

let test_request =
BinaryUser::<Request, false>::from_request(http_request, &mock.state()).await.unwrap();
BinaryUser::<Request, false>::from_request(http_request, mock.state()).await.unwrap();
assert_eq!(Uuid::default(), test_request.id);
assert_eq!(request, test_request.request);
}
Expand Down
2 changes: 1 addition & 1 deletion nghe-backend/src/config/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use derivative::Derivative;
use serde::Deserialize;
use serde_with::serde_as;

use crate::app::state::Key;
use crate::database::Key;

#[serde_as]
#[derive(Derivative, Deserialize)]
Expand Down
File renamed without changes.
40 changes: 0 additions & 40 deletions nghe-backend/src/filesystem/filesystem.rs

This file was deleted.

41 changes: 39 additions & 2 deletions nghe-backend/src/filesystem/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
mod common;
pub mod entry;
mod filesystem;
pub mod local;
pub mod path;
pub mod s3;

use std::borrow::Cow;

use color_eyre::eyre::OptionExt;
pub use common::{Impl, Trait};
pub use entry::Entry;
pub use filesystem::Filesystem;
use nghe_api::common::filesystem;

use crate::{config, Error};

#[derive(Debug, Clone)]
pub struct Filesystem {
local: local::Filesystem,
s3: Option<s3::Filesystem>,
}

impl Filesystem {
pub async fn new(tls: &config::filesystem::Tls, s3: &config::filesystem::S3) -> Self {
let local = local::Filesystem;
let s3 = if s3.enable { Some(s3::Filesystem::new(tls, s3).await) } else { None };
Self { local, s3 }
}

pub fn to_impl(&self, filesystem_type: filesystem::Type) -> Result<Impl<'_>, Error> {
Ok(match filesystem_type {
filesystem::Type::Local => Impl::Local(Cow::Borrowed(&self.local)),
filesystem::Type::S3 => Impl::S3(Cow::Borrowed(
self.s3.as_ref().ok_or_eyre("S3 filesystem is not enabled")?,
)),
})
}

#[cfg(test)]
pub fn local(&self) -> local::Filesystem {
self.local
}

#[cfg(test)]
pub fn s3(&self) -> s3::Filesystem {
self.s3.as_ref().unwrap().clone()
}
}
29 changes: 25 additions & 4 deletions nghe-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,38 @@
#![feature(let_chains)]
#![feature(try_blocks)]

mod app;
mod auth;
pub mod config;
mod database;
mod error;
mod filesystem;
mod media;
pub mod migration;
mod orm;
mod route;
mod schema;

pub use app::build;
use error::Error;

#[cfg(test)]
mod test;

use axum::body::Body;
use axum::http::Request;
use axum::Router;
use error::Error;
use tower_http::trace::TraceLayer;

pub async fn build(config: config::Config) -> Router {
let filesystem =
filesystem::Filesystem::new(&config.filesystem.tls, &config.filesystem.s3).await;

Router::new()
.merge(route::music_folder::router(filesystem))
.merge(route::permission::router())
.merge(route::user::router())
.with_state(database::Database::new(&config.database))
.layer(TraceLayer::new_for_http().make_span_with(|request: &Request<Body>| {
tracing::info_span!(
"request", method = %request.method(), uri = %request.uri()
)
}))
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ pub use nghe_api::music_folder::add::{Request, Response};
use nghe_proc_macro::handler;
use uuid::Uuid;

use crate::app::route::permission;
use crate::app::state::Database;
use crate::database::Database;
use crate::error::Error;
use crate::filesystem::{self, Filesystem, Trait as _};
use crate::orm::music_folders;
use crate::route::permission;

async fn handler_impl<'fs>(
database: &Database,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use diesel_async::RunQueryDsl;
pub use nghe_api::permission::add::{Request, Response};
use nghe_proc_macro::handler;

use crate::app::state::Database;
use crate::database::Database;
use crate::orm::{music_folders, user_music_folder_permissions, users};
use crate::Error;

Expand Down Expand Up @@ -75,7 +75,7 @@ mod tests {
use rstest::rstest;

use super::*;
use crate::app::test::permission::{count, reset};
use crate::test::route::permission::{count, reset};
use crate::test::{mock, Mock};

#[rstest]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
pub mod add;

#[cfg(test)]
pub mod test;

nghe_proc_macro::build_router! {
modules = [add]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use diesel_async::RunQueryDsl;
pub use nghe_api::user::create::{Request, Response};
use nghe_proc_macro::handler;

use crate::app::route::permission;
use crate::app::state::Database;
use crate::database::Database;
use crate::orm::users;
use crate::route::permission;
use crate::Error;

#[handler(role = admin)]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nghe_api::user::Role;
use nghe_proc_macro::handler;

use super::create;
use crate::app::state::Database;
use crate::database::Database;
use crate::orm::users;
use crate::Error;

Expand Down
1 change: 0 additions & 1 deletion nghe-backend/src/test/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use nghe_api::common::filesystem;
use typed_path::Utf8TypedPathBuf;

use crate::filesystem::path;
Expand Down
Loading

0 comments on commit 9be448f

Please sign in to comment.