configure grpc
This commit is contained in:
@@ -4,8 +4,14 @@ mod app;
|
||||
mod components;
|
||||
mod env;
|
||||
mod route;
|
||||
mod rpc;
|
||||
mod views;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
pub const RPC_HOST: &'static str = "http://localhost:8081";
|
||||
#[cfg(not(debug_assertions))]
|
||||
pub const RPC_HOST: &'static str = "https://grpc.aoba.app:8443";
|
||||
|
||||
fn main() {
|
||||
dioxus::launch(App);
|
||||
}
|
||||
|
||||
49
client/src/rpc.rs
Normal file
49
client/src/rpc.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
use std::sync::RwLock;
|
||||
|
||||
use tonic::service::{interceptor::InterceptedService, Interceptor};
|
||||
use tonic_web_wasm_client::Client;
|
||||
|
||||
use crate::{rpc::azki::az_ki_client::AzKiClient, RPC_HOST};
|
||||
|
||||
pub mod azki {
|
||||
tonic::include_proto!("azki");
|
||||
}
|
||||
|
||||
static RPC_CLIENT: RpcConnection = RpcConnection {
|
||||
azki: RwLock::new(None),
|
||||
jwt: RwLock::new(None),
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct RpcConnection {
|
||||
azki: RwLock<Option<AzKiClient<InterceptedService<Client, AuthInterceptor>>>>,
|
||||
jwt: RwLock<Option<String>>,
|
||||
}
|
||||
|
||||
impl RpcConnection {
|
||||
pub fn get_client(&self) -> AzKiClient<InterceptedService<Client, AuthInterceptor>> {
|
||||
self.ensure_client();
|
||||
return self.azki.read().unwrap().clone().unwrap();
|
||||
}
|
||||
|
||||
fn ensure_client(&self) {
|
||||
if self.azki.read().unwrap().is_none() {
|
||||
let wasm_client = Client::new(RPC_HOST.into());
|
||||
let aoba_client = AzKiClient::with_interceptor(wasm_client.clone(), AuthInterceptor);
|
||||
*self.azki.write().unwrap() = Some(aoba_client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AuthInterceptor;
|
||||
impl Interceptor for AuthInterceptor {
|
||||
fn call(&mut self, mut request: tonic::Request<()>) -> Result<tonic::Request<()>, tonic::Status> {
|
||||
if let Some(jwt) = RPC_CLIENT.jwt.read().unwrap().clone() {
|
||||
request
|
||||
.metadata_mut()
|
||||
.insert("authorization", format!("Bearer {jwt}").parse().unwrap());
|
||||
}
|
||||
return Ok(request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user