Add grpc web layer

This commit is contained in:
sydhds
2025-05-16 14:52:27 +02:00
parent c286dca5d5
commit 17579b210b
4 changed files with 67 additions and 5 deletions

View File

@@ -6,7 +6,8 @@ edition = "2024"
[dependencies]
clap = { version = "4.5.37", features = ["derive"] }
tonic = { version = "0.13", features = ["gzip"] }
tonic-reflection = "0.13"
tonic-reflection = "*"
tonic-web = "*"
prost = "0.13"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
@@ -24,6 +25,8 @@ bytesize = "2.0.1"
rln_proof = { path = "../rln_proof" }
chrono = "0.4.41"
parking_lot = "0.12.3"
tower-http = { version = "0.6.4", features = ["cors"] }
http = "*"
[build-dependencies]
tonic-build = "*"

View File

@@ -29,6 +29,11 @@ use tonic::{
transport::Server,
// codec::CompressionEncoding
};
use tonic_web::{
GrpcWebLayer,
};
use tower_http::cors::{Any, CorsLayer};
use http::Method;
use tracing::{
debug,
// error,
@@ -206,7 +211,10 @@ impl RlnProver for ProverService {
&self,
_request: Request<RegisterUserRequest>,
) -> Result<Response<RegisterUserReply>, Status> {
todo!()
let reply = RegisterUserReply {
status: 0
};
Ok(Response::new(reply))
}
type GetProofsStream = ReceiverStream<Result<RlnProof, Status>>;
@@ -278,6 +286,20 @@ impl GrpcProverService {
//.send_compressed(CompressionEncoding::Gzip)
;
// CORS
let cors = CorsLayer::new()
// Allow `GET`, `POST` and `OPTIONS` when accessing the resource
.allow_methods([
Method::GET,
// http POST && OPTIONS not required for grpc-web
// Method::POST,
// Method::OPTIONS
])
// Allow requests from any origin
// FIXME: config?
.allow_origin(Any)
.allow_headers(Any);
Server::builder()
// service protection && limits
// limits: connection
@@ -288,9 +310,11 @@ impl GrpcProverService {
.max_frame_size(PROVER_SERVICE_HTTP2_MAX_FRAME_SIZE.as_u64() as u32)
// perf: tcp
.tcp_nodelay(true)
// No http 1
.accept_http1(false)
// http 1 layer required for GrpcWebLayer
.accept_http1(true)
// services
.layer(cors)
.layer(GrpcWebLayer::new())
.add_service(reflection_service)
.add_service(r)
.serve(self.addr)

View File

@@ -1,5 +1,5 @@
mod args;
mod epoch_service;
// mod epoch_service;
mod error;
mod grpc_service;
mod registry;