Coder Social home page Coder Social logo

grpc-rest-service's Introduction



Gnostic-gRPC and gRPC-Gateway Example with Generated SDKs

Example project using Gnostic to generate gRPC and gRPC-Gateway code, and the generated SDKs for Go, Python, and Node.js.


Requirements

Getting Started

  1. Click on the "Open in Dev Containers" badge above to open the project in a Dev Container. If this does not work, clone the repository by following your usual git workflow, then open it in VS Code.
  2. Optional: Copy .env.template to .env and add your OpenAI API key.
  3. Rebuid the container by running the Remote-Containers: Rebuild Container command.
  4. Open a terminal in VS Code and run the following command:
go run main.go
  1. Open a new terminal and run the following command:
grpcurl -plaintext -d '{"drink_name_request": {"name": "Pan Galactic Gargle Blaster"}}' localhost:50051 bar.Bar/GetDrink | sed 's/%3A/:/g; s/%2F/\//g; s/%3D/=/g; s/%3F/?/g; s/%26/\&/g' | jq
  1. You should see a JSON response with the drink details.

How to Change the API

  1. Edit bar.yaml.
  2. Run the following command to generate the a protobuf binary:
gnostic --pb-out=. bar.yaml
  1. Run the following command to generate the gRPC protobuf file with annotations:
gnostic-grpc -input bar.pb -output .
  1. Run the following command to generate the gRPC and gRPC-Gateway code:
buf generate
  1. Run the following command to generate the SDKs:
speakeasy generate sdk \
    --schema bar.yaml \
    --lang typescript \
    --out ./bar-sdk-typescript
  1. Rebuild the container by running the Remote-Containers: Rebuild Container command.

  2. Continue from step 4 in the Getting Started section.

Testing

We can test the gRPC and gRPC-Gateway services using grpcurl and curl.

Testing with gRPCurl

Run the following commands to test the gRPC service:

# Create a drink
grpcurl -plaintext -d '{"ingredients_request": {"ingredients": ["Gin", "Tonic water", "Lime juice"]}}' localhost:50051 bar.Bar/CreateDrink

# Get the drink
grpcurl -plaintext -d '{"drink_name_request": {"name": "Pan Galactic Gargle Blaster"}}' localhost:50051 bar.Bar/GetDrink

Testing with cURL

Run the following commands to test the gRPC-Gateway service:

# Create a drink
curl -X POST -H "Content-Type: application/json" -d '{"ingredients": ["Gin", "Tonic water", "Lime juice"]}' http://localhost:8080/create-drink

# Get the drink
curl -X POST -H "Content-Type: application/json" -d '{"name": "Pan Galactic Gargle Blaster"}' http://localhost:8080/get-drink

License

This project is licensed under the MIT License - see the LICENSE file for details.

grpc-rest-service's People

Contributors

nielthiart avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.