Skip to content

Commit

Permalink
expose headers on server
Browse files Browse the repository at this point in the history
  • Loading branch information
crwen authored and KKould committed Dec 17, 2024
1 parent f6e005e commit c472644
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 0 additions & 2 deletions tonbo_net_client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub struct TonboClient {
}

impl TonboClient {
// #[cfg(not(feature = "wasm"))]
pub async fn connect(addr: String) -> Result<TonboClient, ClientError> {
#[cfg(not(feature = "wasm"))]
let conn = TonboRpcClient::connect(addr).await?;
Expand Down Expand Up @@ -112,7 +111,6 @@ impl TonboClient {
.encode(&mut Cursor::new(&mut bytes))
.await?;

let record_ref = record.as_record_ref();
let _ = self
.conn
.insert(Request::new(InsertReq { record: bytes }))
Expand Down
5 changes: 5 additions & 0 deletions tonbo_net_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ fusio = { git = "https://github.com/tonbo-io/fusio.git", rev = "216eb446fb0a0c6e
] }
futures-core = "0.3"
futures-util = "0.3"
http = "1.2.0"
prost = { version = "0.13" }
thiserror = "2"
tonbo = { path = "../", package = "tonbo" }
tonic = { version = "0.12" }
tower-http = { version = "0.6.2", default-features = false, features = [
"cors",
] }
tonic-web = "0.12.3"
itertools = "0.13.0"

[dev-dependencies]
Expand Down
28 changes: 28 additions & 0 deletions tonbo_net_server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
sync::Arc,
};

use ::http::HeaderName;
use async_stream::stream;
use futures_core::Stream;
use futures_util::StreamExt;
Expand All @@ -16,6 +17,8 @@ use tonbo::{
DB,
};
use tonic::{transport::Server, Code, Request, Response, Status};
use tonic_web::GrpcWebLayer;
use tower_http::cors::{AllowOrigin, CorsLayer};

use crate::{
proto::{
Expand All @@ -25,6 +28,11 @@ use crate::{
ServerError,
};

const DEFAULT_EXPOSED_HEADERS: [&str; 3] =
["grpc-status", "grpc-message", "grpc-status-details-bin"];
const DEFAULT_ALLOW_HEADERS: [&str; 4] =
["x-grpc-web", "content-type", "x-user-agent", "grpc-timeout"];

pub async fn service(addr: String, db: DB<DynRecord, TokioExecutor>) -> Result<(), ServerError> {
let service = TonboService {
inner: Arc::new(db),
Expand All @@ -33,6 +41,26 @@ pub async fn service(addr: String, db: DB<DynRecord, TokioExecutor>) -> Result<(
println!("addr: {}", addr);

Server::builder()
.accept_http1(true)
.layer(
CorsLayer::new()
.allow_origin(AllowOrigin::mirror_request())
.expose_headers(
DEFAULT_EXPOSED_HEADERS
.iter()
.cloned()
.map(HeaderName::from_static)
.collect::<Vec<HeaderName>>(),
)
.allow_headers(
DEFAULT_ALLOW_HEADERS
.iter()
.cloned()
.map(HeaderName::from_static)
.collect::<Vec<HeaderName>>(),
),
)
.layer(GrpcWebLayer::new())
.add_service(TonboRpcServer::new(service))
.serve(addr)
.await?;
Expand Down

0 comments on commit c472644

Please sign in to comment.