Lädt...

🔧 Defining gRPC APIs with Protobuf - Part 2


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

In Part 1, we set up our project structure. Now, we’ll:
✅ Define User and Order APIs
✅ Generate gRPC code from .proto files.

We have created apis folder so lets navigate to that folder and define our apis there.

cd ecom-grpc/apis

Note: Please replace the github user name and repo in each of the following folder

Step 1: Install Protobuf Compiler

Before writing .proto files, install protoc:

🔹 Mac (Homebrew)

brew install protobuf

🔹 Ubuntu/Linux

sudo apt update && sudo apt install -y protobuf-compiler

🔹 Windows

Download from protobuf releases.

Verify installation:

protoc --version

Step 2: Define User Service API

create user/v1/user.proto file in apis repo

mkdir -p user/v1/
touch user/v1/user.proto
syntax = "proto3";

package user.v1;

option go_package = "github.com/lakhansamani/ecom-grpc-apis/user/v1";

// User Service definition
service UserService {
    // Register API to register a new user
    // Permission: none
    rpc Register(RegisterRequest) returns (RegisterResponse);
    // Login API to login a user
    // Permission: none
    rpc Login(LoginRequest) returns (LoginResponse);
    // Me API to get user details
    // Permission: authenticated user
    rpc Me(MeRequest) returns (MeResponse);
}

// User model
message User {
    string id = 1;
    string name = 2;
    string email = 3;
}

// Register API
message RegisterRequest {
    string name = 1;
    string email = 2;
    string password = 3;
}

message RegisterResponse {
    string user_id = 1;
}

// Login API
message LoginRequest {
    string email = 1;
    string password = 2;
}

message LoginResponse {
    string token = 1;
}

// Me API (token will be taken from gRPC metadata)
message MeRequest {}

message MeResponse {
    User user = 1;
}

New API methods

  • Register → Registers a new user and returns a JWT token.
  • Login → Authenticates a user and returns a JWT token.
  • Me → Returns user details using the JWT token.

🔹 Step 3: Define Order Service API

create order/v1/order.proto file in apis repo

mkdir -p order/v1/
touch order/v1/order.proto
syntax = "proto3";

package order.v1;

option go_package = "github.com/lakhansamani/ecom-grpc-apis/order/v1";

service OrderService {
    // CreateOrder API to create a new order
    // Permission: authenticated user
    rpc CreateOrder(CreateOrderRequest) returns (CreateOrderResponse);
    // GetOrder API to get order details
    // Permission: authenticated user who created the order
    rpc GetOrder(GetOrderRequest) returns (GetOrderResponse);
}

// Order model
message Order {
    string id = 1;
    string user_id = 2;
    string product = 3;
    int32 quantity = 4;
    double unit_price = 5;
}

// CreateOrder request API
message CreateOrderRequest {
    string user_id = 1;
    string product = 2;
    int32 quantity = 3;
    double price = 4;
}

// CreateOrder response API
message CreateOrderResponse {
    Order order = 1;
}

// GetOrder request API
message GetOrderRequest {
    string id = 1;
}

// GetOrder response API
message GetOrderResponse {
    Order order = 1;
}

New API methods

  • CreateOrder - to create order
  • GetOrder - to get order information

🔹 Step 4: Generate gRPC Code

To simplify the process lets create a Makefile at the root of apis repo which we can run to generate the go-grpc code.

touch Makefile
all: build
build:
    protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=require_unimplemented_servers=false:. --go-grpc_opt=paths=source_relative user/v1/user.proto
    protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=require_unimplemented_servers=false:. --go-grpc_opt=paths=source_relative order/v1/order.proto

Run make command to generate the files and push to github.

Code Link

🎯 Next Steps

  • 🚀 Now that we have the User & Order Service protobuf, in Part 3, we will:
  • ✅ Implement User Service.
  • ✅ Implement Order Service.

Stay tuned! 🚀

...

🔧 Browser Client to gRPC Server Routing options: Connect, gRPC-web, gRPC-gateway and more!


📈 45.68 Punkte
🔧 Programmierung

📰 Denial of Service in protobuf und protobuf-python (Gentoo)


📈 39.54 Punkte
📰 IT Security Nachrichten

🔧 Docs That Write Themselves: Scaling With gRPC and Protobuf


📈 34.99 Punkte
🔧 Programmierung

🔧 Performance Benchmarking: gRPC+Protobuf vs. HTTP+JSON


📈 34.99 Punkte
🔧 Programmierung

🔧 gRPC API Gateway: Bridging the Gap Between REST and gRPC


📈 30.45 Punkte
🔧 Programmierung

🔧 Using gRPC in React the Modern Way: From gRPC-web to Connect


📈 30.45 Punkte
🔧 Programmierung

🕵️ grpc/grpc-js Prototype loadPackageDefinition code injection


📈 30.45 Punkte
🕵️ Sicherheitslücken

🔧 ASP.NET Community Standup - June 4th, 2019 - gRPC with the gRPC Team | .NET Community Standups


📈 30.45 Punkte
🔧 Programmierung

🔧 Model Context Protocol : A New Standard for Defining APIs


📈 25.86 Punkte
🔧 Programmierung

🔧 Are Web APIs the same as REST APIs? How Web APIs Improve the Web.


📈 25.02 Punkte
🔧 Programmierung

🔧 What are Web APIs? [1 of 18] | Beginner's Series to: Web APIs | Beginner's Series to: Web APIs


📈 25.02 Punkte
🔧 Programmierung

🔧 Strongly typed web APIs with gRPC


📈 23.56 Punkte
🔧 Programmierung

📰 GraphQL vs gRPC: Which One Creates More Secure APIs?


📈 23.56 Punkte
📰 IT Security Nachrichten

🔧 gRPC vs HTTP APIs


📈 23.56 Punkte
🔧 Programmierung

🔧 REST vs GraphQL vs gRPC: A Simple Guide to Modern APIs


📈 23.56 Punkte
🔧 Programmierung

📰 USN-5769-1: protobuf vulnerabilities


📈 19.77 Punkte
🐧 Unix Server

🔧 Gamechanger Protobuf


📈 19.77 Punkte
🔧 Programmierung

📰 Denial of Service in protobuf (Red Hat)


📈 19.77 Punkte
📰 IT Security Nachrichten

🔧 Why should we use Protobuf in Web API as data transfer protocol.


📈 19.77 Punkte
🔧 Programmierung

📰 Denial of Service in protobuf (Red Hat)


📈 19.77 Punkte
📰 IT Security Nachrichten

📰 Zwei Probleme in protobuf-c (Gentoo)


📈 19.77 Punkte
📰 IT Security Nachrichten

🔧 The Art of Serialization: JSON Sips Tea, Protobuf Chugs Espresso


📈 19.77 Punkte
🔧 Programmierung

📰 Mehrere Probleme in protobuf (SUSE)


📈 19.77 Punkte
📰 IT Security Nachrichten

📰 Security: Zahlenüberlauf in protobuf-c (Red Hat)


📈 19.77 Punkte
🐧 Unix Server

🔧 Protobuf vs JSON: Performance, Efficiency, and API Optimization


📈 19.77 Punkte
🔧 Programmierung

📰 Denial of Service in protobuf (Red Hat)


📈 19.77 Punkte
📰 IT Security Nachrichten

📰 Denial of Service in protobuf (Red Hat)


📈 19.77 Punkte
📰 IT Security Nachrichten