Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rodoufu/rest2grpc
Convert HTTP REST to gRPC
https://github.com/rodoufu/rest2grpc
bridge express-js gateway grpc grpc-gateway rest rest-api restful
Last synced: about 2 months ago
JSON representation
Convert HTTP REST to gRPC
- Host: GitHub
- URL: https://github.com/rodoufu/rest2grpc
- Owner: rodoufu
- License: mit
- Created: 2020-11-30T18:10:16.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-01-22T23:03:26.000Z (almost 4 years ago)
- Last Synced: 2024-11-10T20:24:13.846Z (2 months ago)
- Topics: bridge, express-js, gateway, grpc, grpc-gateway, rest, rest-api, restful
- Language: TypeScript
- Homepage:
- Size: 46.9 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rest2grpc
Available at https://www.npmjs.com/package/rest2grpc
## Example
Let's say we have a simple gRPC backend service that we want to expose as a REST endpoint,
something like this protos file (`Example.proto`):
```proto
syntax = "proto3";package example;
service Example {
rpc SayHello (SayHelloRequest) returns (SayHelloResponse) {}
}message SayHelloRequest {
string name = 1;
}message SayHelloResponse {
string msg = 1;
}
```We can use the following configuration (`Example.yaml`) to do so:
```yaml
http:
rules:
- selector: example.Example.SayHello
get: /sayHelloGet
post: /sayHelloPost
```That tells rest2grpc to create 2 endpoints:
- `/sayHelloGet` answering to GET requests.
- `/sayHelloPost` answering to POST requests.Both requests are translated into gRPC and sent to the namespace `example`, service `Example`,
and call the method `SayHello`.Now to call everything you only need to:
```ts
import {Rest2gRPCServer} from 'rest2grpc';
(async () => {
const address = 'localhost:50051';let configFile = `${__dirname}/Example.yaml`;
const restServer = new Rest2gRPCServer(console);
const protoPath = [`${__dirname}/protos`];
const protoFile = "Example.proto";
await restServer.register(configFile, protoPath, protoFile, address);restServer.start(3000);
})();
```## References
- https://github.com/grpc-ecosystem/grpc-gateway