configure grpc
This commit is contained in:
6
client/.dockerignore
Normal file
6
client/.dockerignore
Normal file
@@ -0,0 +1,6 @@
|
||||
**/target
|
||||
**/dist
|
||||
LICENSES
|
||||
LICENSE
|
||||
temp
|
||||
README.md
|
||||
619
client/Cargo.lock
generated
619
client/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "azki"
|
||||
name = "azki-client"
|
||||
version = "0.1.0"
|
||||
authors = ["Amatsugu <khamraj@kaisei.app>"]
|
||||
edition = "2021"
|
||||
@@ -7,19 +7,21 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
dioxus = { version = "0.7.1", features = ["router"] }
|
||||
dioxus = { version = "0.7.3", features = ["router"] }
|
||||
serde = "1.0.219"
|
||||
serde_repr = "0.1.20"
|
||||
tonic = { version = "*", default-features = false, features = [
|
||||
tonic = { version = "0.14.2", default-features = false, features = [
|
||||
"codegen",
|
||||
"prost",
|
||||
] }
|
||||
prost = "0.13"
|
||||
tonic-web-wasm-client = "0.7"
|
||||
prost = "0.14"
|
||||
prost-types = "0.14"
|
||||
tonic-web-wasm-client = "0.8"
|
||||
web-sys = { version = "0.3.77", features = ["Storage", "Window"] }
|
||||
tokio = "1.49.0"
|
||||
tonic-prost = "0.14.2"
|
||||
|
||||
[build-dependencies]
|
||||
tonic-build = { version = "*", default-features = false, features = ["prost"] }
|
||||
tonic-prost-build = { version = "0.14.2"}
|
||||
dotenv = "0.15.0"
|
||||
|
||||
[features]
|
||||
|
||||
@@ -4,15 +4,14 @@ use std::fs::File;
|
||||
use std::io::Write;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// tonic_build::configure()
|
||||
// .build_server(false)
|
||||
// .build_client(true)
|
||||
// .compile_protos(
|
||||
// &[
|
||||
// "",
|
||||
// ],
|
||||
// &[""],
|
||||
// )?;
|
||||
tonic_prost_build::configure()
|
||||
.build_server(false)
|
||||
.build_client(true)
|
||||
.build_transport(false)
|
||||
.compile_protos(
|
||||
&["../AZKiServer/Protos/azki.proto", "../AZKiServer/Protos/types.proto"],
|
||||
&["../AZKiServer"],
|
||||
)?;
|
||||
forward_env();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -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