-
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.
frontend: add applications shell (#657)
* add applications shell * add login redirect * add authorization header * add user info handler * add user info shell * refactor root and shell * prevent client is none
- Loading branch information
Showing
19 changed files
with
496 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 @@ | ||
use nghe_proc_macro::api_derive; | ||
|
||
use super::Role; | ||
|
||
#[api_derive(fake = true)] | ||
#[endpoint(path = "userInfo", internal = true)] | ||
pub struct Request; | ||
|
||
#[api_derive] | ||
#[derive(Clone)] | ||
pub struct Response { | ||
pub username: String, | ||
pub email: String, | ||
pub role: Role, | ||
} |
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,4 +1,5 @@ | ||
pub mod create; | ||
pub mod info; | ||
mod role; | ||
pub mod setup; | ||
|
||
|
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,37 @@ | ||
use diesel::{ExpressionMethods, QueryDsl, SelectableHelper}; | ||
use diesel_async::RunQueryDsl; | ||
pub use nghe_api::user::info::{Request, Response}; | ||
use nghe_proc_macro::handler; | ||
use uuid::Uuid; | ||
|
||
use crate::Error; | ||
use crate::database::Database; | ||
use crate::orm::users; | ||
|
||
#[handler(internal = true)] | ||
pub async fn handler(database: &Database, user_id: Uuid) -> Result<Response, Error> { | ||
users::table | ||
.filter(users::id.eq(user_id)) | ||
.select(users::Info::as_select()) | ||
.first(&mut database.get().await?) | ||
.await | ||
.map(users::Info::into) | ||
.map_err(Error::from) | ||
} | ||
|
||
#[cfg(test)] | ||
#[coverage(off)] | ||
mod tests { | ||
use rstest::rstest; | ||
|
||
use super::*; | ||
use crate::test::{Mock, mock}; | ||
|
||
#[rstest] | ||
#[tokio::test] | ||
async fn test_handler(#[future(awt)] mock: Mock) { | ||
let user = mock.user(0).await; | ||
let user_info = handler(mock.database(), user.id()).await.unwrap(); | ||
assert_eq!(user.username(), user_info.username); | ||
} | ||
} |
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,9 +1,11 @@ | ||
pub mod create; | ||
mod info; | ||
mod setup; | ||
|
||
nghe_proc_macro::build_router! { | ||
modules = [ | ||
create(internal = true), | ||
info(internal = true), | ||
setup(internal = true), | ||
], | ||
} |
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 |
---|---|---|
@@ -1,16 +1,71 @@ | ||
use anyhow::Error; | ||
use codee::string::{FromToStringCodec, OptionCodec}; | ||
use concat_string::concat_string; | ||
use gloo_net::http; | ||
use leptos::prelude::*; | ||
use leptos_router::NavigateOptions; | ||
use leptos_router::hooks::use_navigate; | ||
use leptos_use::storage::use_local_storage; | ||
use nghe_api::common::{JsonEndpoint, JsonURL}; | ||
use uuid::Uuid; | ||
|
||
pub struct Client; | ||
#[derive(Clone)] | ||
pub struct Client { | ||
authorization: String, | ||
} | ||
|
||
impl Client { | ||
pub async fn json<R: JsonEndpoint>(request: &R) -> Result<R::Response, Error> { | ||
let response = http::Request::post(<R as JsonURL>::URL_JSON).json(request)?.send().await?; | ||
const API_KEY_STORAGE_KEY: &'static str = "api-key"; | ||
|
||
pub const EXPECT_MSG: &'static str = "use_client_redirect should prevent this"; | ||
|
||
pub fn new(api_key: Uuid) -> Self { | ||
Self { authorization: concat_string!("Bearer ", api_key.to_string()) } | ||
} | ||
|
||
pub fn use_api_key() -> (Signal<Option<Uuid>>, WriteSignal<Option<Uuid>>) { | ||
let (read, write, _) = use_local_storage::<Option<Uuid>, OptionCodec<FromToStringCodec>>( | ||
Self::API_KEY_STORAGE_KEY, | ||
); | ||
(read, write) | ||
} | ||
|
||
pub fn use_client() -> Signal<Option<Client>> { | ||
let (read_api_key, _) = Self::use_api_key(); | ||
Signal::derive(move || read_api_key.with(|api_key| api_key.map(Client::new))) | ||
} | ||
|
||
pub fn use_client_redirect() -> (Signal<Option<Client>>, Effect<LocalStorage>) { | ||
let client = Self::use_client(); | ||
let effect = Effect::new(move || { | ||
if client.with(Option::is_none) { | ||
use_navigate()("/login", NavigateOptions::default()); | ||
} | ||
}); | ||
(client, effect) | ||
} | ||
|
||
async fn json_impl<R: JsonEndpoint>( | ||
request: &R, | ||
authorization: Option<&str>, | ||
) -> Result<R::Response, Error> { | ||
let response = http::Request::post(<R as JsonURL>::URL_JSON) | ||
.header("Authorization", authorization.unwrap_or_default()) | ||
.json(request)? | ||
.send() | ||
.await?; | ||
if response.ok() { | ||
Ok(response.json().await?) | ||
} else { | ||
anyhow::bail!("{}", response.text().await?) | ||
} | ||
} | ||
|
||
pub async fn json_no_auth<R: JsonEndpoint>(request: &R) -> Result<R::Response, Error> { | ||
Self::json_impl(request, None).await | ||
} | ||
|
||
pub async fn json<R: JsonEndpoint>(&self, request: &R) -> Result<R::Response, Error> { | ||
Self::json_impl(request, Some(&self.authorization)).await | ||
} | ||
} |
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
Oops, something went wrong.