grpc-web-server
A gRPC web proxy library and app written in Rust with a built in HTTP web server
George-Madeley
release
public
George-Madeley
release
public
A gRPC-Web proxy and web server implemented in Rust, with first-class Rust and C/C++ APIs.
This project solves a packaging and toolchain gap:
Source layout is split by domain:
src/server/proxy.rs: upstream gRPC transport bridge.server.rs: HTTP server, gRPC-Web routing, CORS policy, observability, TLS serving.handle.rs: lifecycle handle (start, stop, wait, is_running) for background runtime management.ffi.rs: C-compatible API for server configuration and lifecycle.src/tls/ca.rs: certificate authority creation/writing.leaf.rs: leaf certificate issuance/writing.ffi.rs: C-compatible API for certificate generation.src/bin/grpc-web-server.rs: standalone runtime executable.gen-certs.rs: standalone certificate generation tool.gen/include/server.h: generated C/C++ header for server FFI.tls.h: generated C/C++ header for TLS FFI.Run the server directly:
cargo run --bin grpc-web-server -- \
--http-address 127.0.0.1:8080 \
--grpc-address 127.0.0.1:50051Generate development certificates:
cargo run --bin gen-certs -- ./certsUse the server API from Rust:
use grpc_web_server::server::server::{CorsPolicy, GrpcWebServer, GrpcWebServerOptions};
let options = GrpcWebServerOptions {
http_address: "127.0.0.1:8080".to_string(),
grpc_address: "127.0.0.1:50051".to_string(),
static_dir: None,
grpc_ca_cert: None,
grpc_proxy_key: None,
grpc_proxy_cert: None,
http_key: None,
http_cert: None,
cors_policy: CorsPolicy::default(),
};
let server = GrpcWebServer::new(options)?;
server.start(std::future::pending()).await?;
# Ok::<(), Box<dyn std::error::Error>>(())Use TLS helpers from Rust:
use grpc_web_server::tls;
use rcgen::ExtendedKeyUsagePurpose;
let ca = tls::ca::make("grpc-web-dev-ca")?;
tls::ca::write(std::path::Path::new("./certs"), &ca)?;
let leaf = tls::leaf::issue(
&ca,
"grpc-server",
vec!["localhost".to_string(), "127.0.0.1".to_string()],
ExtendedKeyUsagePurpose::ServerAuth,
)?;
tls::leaf::write(std::path::Path::new("./certs"), "server", &leaf)?;
# Ok::<(), Box<dyn std::error::Error>>(())Generated headers:
gen/include/server.hgen/include/tls.hServer lifecycle API (from server.h):
createstartwaitis_runningstopdestroyTLS API (from tls.h):
create_certificate_authoritywrite_certificate_authorityissue_leaf_certificatewrite_leaf_certificatedestroy_certificate_authoritydestroy_leaf_certificateOwnership rule for all FFI handles:
create* / issue* is owned by the caller.destroy* function.GrpcWebCorsOptions in server.h supports:
allow_any_origin),allow_any_method),allow_any_header),allow_credentials).Validation is strict:
This repository includes CMakeLists.txt and CMakePresets.json and uses Corrosion to import the Rust crate.
Typical flow:
cmake --preset "gRPC Web Server (Debug)"
cmake --build --preset "gRPC Web Server (Debug)"or for release:
cmake --preset "gRPC Web Server (Release)"
cmake --build --preset "gRPC Web Server (Release)"If you want to embed this crate in another CMake project, add as a subdirectory:
add_subdirectory(grpc-web-server)Please note that the CMake code uses the Corrosion library to get CMake to build the Rust library.
When using the generated FFI APIs from C/C++:
server.h and/or tls.h,Headers are generated via build.rs into gen/include/ during Cargo builds.
If you change FFI signatures or structs, rebuild the crate:
cargo buildThen consume updated server.h and tls.h from gen/include/.
A gRPC web proxy library and app written in Rust with a built in HTTP web server
0
20
0
0