SlideShare uma empresa Scribd logo
1 de 31
Baixar para ler offline
https://www.google.com/url?
sa=i&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FWebAssembly&psig=AOvVaw01ph2uPKCm79kmbF
AW&ust=1594860562007000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCOCK-
dyEzuoCFQAAAAAdAAAAABAD
Alvaro Viebrantz


Google Developer Expe
rt
for IoT and Founding Engineer at golioth.io


aviebrantz.com


@alvaroviebrantz
Building REST APIs using
gRPC and Go
CURRENT STATE OF REST APIS
What is a REST API ?
Usually follow some conventions
• API using the most of the HTTP Spec


• HTTP Verbs to de
fi
ne methods


• Leverage HTTP Response codes


• URL to de
fi
ne resource location


• Content-Type/Accept Headers to de
fi
ne serialization/format
HTTP Method Action URL
GET List Books https://example.com/api/books
GET Get a book https://example.com/api/books/{id}
POST Create a book https://example.com/api/books
PUT Update a book https://example.com/api/books/{id}
DELETE Delete a book https://example.com/api/books/{id}
JSON over HTTP
Most REST APIs are just JSON being transferred over HTTP
• Human Readable


• Native to Javascript and browsers. JSON is supported natively everywhere


• Problems


• Poorly typed - How do I know which types to send to the API ?


• Expensive marshaling


• Not type safe and hand made validation


• Some solutions - Add more docs on top of it


• OpenAPI / Swagger docs


• JSONAPI Spec
SO THE SOLUTION IS TO


WRITE MORE YAML/JSON ?


YES


😞 😞 😞
gRPC API
What is a gRPC API ?
High performance Remote Procedure Call framework
• Uses HTTP/2 for transport


• Bi-directional streaming between client and server


• Build on top of Protocol Bu
ff
ers


• Binary format to transfer data


• Fast and low overhead


• IDL to de
fi
ne messages


• Single Source of Truth


• Type-safe


• Generators for multiple programming languages and platforms
grpc.io
Building an API with gRPC
Steps for building an API
• De
fi
ne a service in a .proto
fi
le using Protocol
Bu
ff
ers IDL


• Generate server and client stub using the
Protobuf compiler


• Extend/Implement your business logic in the
language that you choose for the server


• Call the service using the


generated client
grpc.io
Protocol Buffers
Language neutral structured data definition
De
fi
ning Services
Using the same IDL from Protobuf
Compiling and implementing Services
Just fill with you business logic
Use the generated client
No need to write code to connect your app to your server
Problems with gRPC
Of course, everything has pros and cons
• Not supported directly in the browser


• You need a proxy to convert from HTTP/2 to HTTP/1.1


• gRPC-Web, Envoy Proxy and others solve that


• That alone is already a big issue.


• A lot of public facing APIs are expected to be a HTTP+JSON API
USING THE BEST OF BOTH WORLDS


gRPC Gateway
gRPC-Gateway
github.com/grpc-ecosystem/grpc-gateway
• Protocol bu
ff
ers plugin


• Translates HTTP+JSON requests to
gRPC back and forth


• HTTP Status codes


• HTTP Headers


• Add annotations to your proto
fi
les
and you are good to go


• Generates Swagger/OpenAPI
de
fi
nitions


• Currently generates server code in
Go, but in theory can generate to
other languages in the future
Demo


TV Time API
github.com/alvarowolfx/go-grpc-rest-api-demo
TV Time API
Track your favorite Movies and TV Shows
• Create an account


• Login ( get a JWT Token )


• List catalog entries


• Create catalog entry 🔒


• Add/Remove from your favorite list 🔒


• List Favorites 🔒
github.com/alvarowolfx/go-grpc-rest-api-demo
gRPC Services
Auth, Users and Catalog services
gRPC Services
Auth, Users and Catalog services
OpenAPI/Swagger Annotations
Extra annotations to allow the same gRPC server to be accessed via a REST API
OpenAPI/Swagger Annotations
Annotations to add more metadata to Swagger output
OpenAPI/Swagger Security Requirements
Let us mark which methods need to be called with authentication
Authentication Interceptor
github.com/grpc-ecosystem/go-grpc-middleware
OpenAPI/Swagger Output
You can try it out on Swagger UI
OpenAPI/Swagger Output
You can try it out on other UI
Demo
github.com/alvarowolfx/go-grpc-rest-api-demo
References
References
• Talk Demo - github.com/alvarowolfx/go-grpc-rest-api-demo


• grpc.io


• github.com/grpc-ecosystem/grpc-gateway


• github.com/johanbrandhorst/grpc-gateway-boilerplate


• Videos


• https://www.youtube.com/watch?v=AaqZvsrULi4


• https://www.youtube.com/watch?v=Zf9G2KzYs7w


• https://www.youtube.com/watch?v=Pq1paKC-fXk
github.com/alvarowolfx/go-grpc-rest-api-demo
Thanks!




aviebrantz.com


youtube.com/alvaroviebrantz


twitch.tv/alvaroviebrantz


Alvaro Viebrantz


Google Developer Expe
rt
for IoT and Founding Engineer at golioth.io


aviebrantz.com


@alvaroviebrantz

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

PF開発に使えるAOSPのツール達
PF開発に使えるAOSPのツール達PF開発に使えるAOSPのツール達
PF開発に使えるAOSPのツール達
 
REST vs gRPC: Battle of API's
REST vs gRPC: Battle of API'sREST vs gRPC: Battle of API's
REST vs gRPC: Battle of API's
 
gRPC - RPC rebirth?
gRPC - RPC rebirth?gRPC - RPC rebirth?
gRPC - RPC rebirth?
 
iFunEngine: 30분 만에 게임 서버 만들기
iFunEngine: 30분 만에 게임 서버 만들기iFunEngine: 30분 만에 게임 서버 만들기
iFunEngine: 30분 만에 게임 서버 만들기
 
gRPC Design and Implementation
gRPC Design and ImplementationgRPC Design and Implementation
gRPC Design and Implementation
 
Zookeeper 활용 nifi clustering
Zookeeper 활용 nifi clusteringZookeeper 활용 nifi clustering
Zookeeper 활용 nifi clustering
 
[야생의 땅: 듀랑고] 서버 아키텍처 - SPOF 없는 분산 MMORPG 서버
[야생의 땅: 듀랑고] 서버 아키텍처 - SPOF 없는 분산 MMORPG 서버[야생의 땅: 듀랑고] 서버 아키텍처 - SPOF 없는 분산 MMORPG 서버
[야생의 땅: 듀랑고] 서버 아키텍처 - SPOF 없는 분산 MMORPG 서버
 
Keycloak for Science Gateways - SGCI Technology Sampler Webinar
Keycloak for Science Gateways - SGCI Technology Sampler WebinarKeycloak for Science Gateways - SGCI Technology Sampler Webinar
Keycloak for Science Gateways - SGCI Technology Sampler Webinar
 
Whoops! I Rewrote It in Rust
Whoops! I Rewrote It in RustWhoops! I Rewrote It in Rust
Whoops! I Rewrote It in Rust
 
AS45679 on FreeBSD
AS45679 on FreeBSDAS45679 on FreeBSD
AS45679 on FreeBSD
 
Introduction to gRPC
Introduction to gRPCIntroduction to gRPC
Introduction to gRPC
 
OpenID Connect Explained
OpenID Connect ExplainedOpenID Connect Explained
OpenID Connect Explained
 
What is gRPC introduction gRPC Explained
What is gRPC introduction gRPC ExplainedWhat is gRPC introduction gRPC Explained
What is gRPC introduction gRPC Explained
 
GPD-WIN、Windows10タブレットに各種Linuxディストリを入れて改造してみた 2017年度名古屋版
GPD-WIN、Windows10タブレットに各種Linuxディストリを入れて改造してみた 2017年度名古屋版GPD-WIN、Windows10タブレットに各種Linuxディストリを入れて改造してみた 2017年度名古屋版
GPD-WIN、Windows10タブレットに各種Linuxディストリを入れて改造してみた 2017年度名古屋版
 
gRPC 프레임워크를 만들며 알아보는 파이썬 - 파이콘2020
gRPC 프레임워크를 만들며 알아보는 파이썬  - 파이콘2020gRPC 프레임워크를 만들며 알아보는 파이썬  - 파이콘2020
gRPC 프레임워크를 만들며 알아보는 파이썬 - 파이콘2020
 
192.0.0.4 on android
192.0.0.4 on android192.0.0.4 on android
192.0.0.4 on android
 
gRPC and Microservices
gRPC and MicroservicesgRPC and Microservices
gRPC and Microservices
 
NDC12_Lockless게임서버설계와구현
NDC12_Lockless게임서버설계와구현NDC12_Lockless게임서버설계와구현
NDC12_Lockless게임서버설계와구현
 
【Interop Tokyo 2022】ここが見どころ!ジュニパーのShowNetにおける取組みご紹介
【Interop Tokyo 2022】ここが見どころ!ジュニパーのShowNetにおける取組みご紹介【Interop Tokyo 2022】ここが見どころ!ジュニパーのShowNetにおける取組みご紹介
【Interop Tokyo 2022】ここが見どころ!ジュニパーのShowNetにおける取組みご紹介
 
Power-up services with gRPC
Power-up services with gRPCPower-up services with gRPC
Power-up services with gRPC
 

Semelhante a Building REST APIs using gRPC and Go

Semelhante a Building REST APIs using gRPC and Go (20)

Building a REST API Microservice for the DevNet API Scavenger Hunt
Building a REST API Microservice for the DevNet API Scavenger HuntBuilding a REST API Microservice for the DevNet API Scavenger Hunt
Building a REST API Microservice for the DevNet API Scavenger Hunt
 
Crafting APIs
Crafting APIsCrafting APIs
Crafting APIs
 
マイクロサービスバックエンドAPIのためのRESTとgRPC
マイクロサービスバックエンドAPIのためのRESTとgRPCマイクロサービスバックエンドAPIのためのRESTとgRPC
マイクロサービスバックエンドAPIのためのRESTとgRPC
 
API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)
 
API Conference 2021
API Conference 2021API Conference 2021
API Conference 2021
 
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
Survival Strategies for API Documentation: Presentation to Southwestern Ontar...
 
Developing Brilliant and Powerful APIs in Ruby & Python
Developing Brilliant and Powerful APIs in Ruby & PythonDeveloping Brilliant and Powerful APIs in Ruby & Python
Developing Brilliant and Powerful APIs in Ruby & Python
 
JHipster Conf 2018 : Connect your JHipster apps to the world of APIs with Ope...
JHipster Conf 2018 : Connect your JHipster apps to the world of APIs with Ope...JHipster Conf 2018 : Connect your JHipster apps to the world of APIs with Ope...
JHipster Conf 2018 : Connect your JHipster apps to the world of APIs with Ope...
 
Middleware in Golang: InVision's Rye
Middleware in Golang: InVision's RyeMiddleware in Golang: InVision's Rye
Middleware in Golang: InVision's Rye
 
Cqrs api
Cqrs apiCqrs api
Cqrs api
 
Web Services and Android - OSSPAC 2009
Web Services and Android - OSSPAC 2009Web Services and Android - OSSPAC 2009
Web Services and Android - OSSPAC 2009
 
Swagger - make your API accessible
Swagger - make your API accessibleSwagger - make your API accessible
Swagger - make your API accessible
 
REST API Basics
REST API BasicsREST API Basics
REST API Basics
 
PyBCN 2020
PyBCN 2020PyBCN 2020
PyBCN 2020
 
WordPress Rest API
WordPress Rest APIWordPress Rest API
WordPress Rest API
 
API Design in the Modern Era - Architecture Next 2020
API Design in the Modern Era - Architecture Next 2020API Design in the Modern Era - Architecture Next 2020
API Design in the Modern Era - Architecture Next 2020
 
Eran Stiller: API design in the modern era - architecture next 2020
Eran Stiller: API design in the modern era - architecture next 2020 Eran Stiller: API design in the modern era - architecture next 2020
Eran Stiller: API design in the modern era - architecture next 2020
 
Apigility introduction v2 (glasgow php)
Apigility introduction v2 (glasgow php)Apigility introduction v2 (glasgow php)
Apigility introduction v2 (glasgow php)
 
PHP, OAuth, Web Services and YQL
PHP, OAuth, Web Services and YQLPHP, OAuth, Web Services and YQL
PHP, OAuth, Web Services and YQL
 
Understanding APIs.pptx
Understanding APIs.pptxUnderstanding APIs.pptx
Understanding APIs.pptx
 

Mais de Alvaro Viebrantz

Ambiente de CI/CD com Google Cloud
Ambiente de CI/CD com Google CloudAmbiente de CI/CD com Google Cloud
Ambiente de CI/CD com Google Cloud
Alvaro Viebrantz
 

Mais de Alvaro Viebrantz (20)

BigQuery Performance Improvements Storage API
BigQuery Performance Improvements Storage APIBigQuery Performance Improvements Storage API
BigQuery Performance Improvements Storage API
 
End to End IoT projects with Zephyr.pdf
End to End IoT projects with Zephyr.pdfEnd to End IoT projects with Zephyr.pdf
End to End IoT projects with Zephyr.pdf
 
Carreira de Desenvolvimento
Carreira de DesenvolvimentoCarreira de Desenvolvimento
Carreira de Desenvolvimento
 
Construindo aplicações Cloud Native em Go
Construindo aplicações Cloud Native em GoConstruindo aplicações Cloud Native em Go
Construindo aplicações Cloud Native em Go
 
Prototipação em hackathons
Prototipação em hackathonsPrototipação em hackathons
Prototipação em hackathons
 
TinyML - IoT e Machine Learning
TinyML -  IoT e Machine LearningTinyML -  IoT e Machine Learning
TinyML - IoT e Machine Learning
 
O que projetos de IoT precisam ?
O que projetos de IoT precisam ?O que projetos de IoT precisam ?
O que projetos de IoT precisam ?
 
Ambiente de CI/CD com Google Cloud
Ambiente de CI/CD com Google CloudAmbiente de CI/CD com Google Cloud
Ambiente de CI/CD com Google Cloud
 
Big Query - Escalabilidade Infinita para os seus Dados
Big Query  - Escalabilidade Infinita para os seus DadosBig Query  - Escalabilidade Infinita para os seus Dados
Big Query - Escalabilidade Infinita para os seus Dados
 
Rodando uma API Com Django Rest Framework no Google Cloud
Rodando uma API Com Django Rest Framework  no Google CloudRodando uma API Com Django Rest Framework  no Google Cloud
Rodando uma API Com Django Rest Framework no Google Cloud
 
Edge computing na prática com IoT, Machine Learning e Google Cloud
Edge computing na prática com IoT, Machine Learning e Google CloudEdge computing na prática com IoT, Machine Learning e Google Cloud
Edge computing na prática com IoT, Machine Learning e Google Cloud
 
Edge computing in practice using IoT, Tensorflow and Google Cloud
Edge computing in practice using IoT, Tensorflow and Google CloudEdge computing in practice using IoT, Tensorflow and Google Cloud
Edge computing in practice using IoT, Tensorflow and Google Cloud
 
Iniciando com LoRa, The Things Network e Google Cloud
Iniciando com LoRa, The Things Network e Google CloudIniciando com LoRa, The Things Network e Google Cloud
Iniciando com LoRa, The Things Network e Google Cloud
 
Construindo projetos para o Google Assistant - I/O 2019 Recap São Paulo
Construindo projetos para o Google Assistant - I/O 2019 Recap São PauloConstruindo projetos para o Google Assistant - I/O 2019 Recap São Paulo
Construindo projetos para o Google Assistant - I/O 2019 Recap São Paulo
 
Edge computing na prática com IoT, Machine Learning e Google Cloud
Edge computing na prática com IoT, Machine Learning e Google CloudEdge computing na prática com IoT, Machine Learning e Google Cloud
Edge computing na prática com IoT, Machine Learning e Google Cloud
 
Construindo projetos com Google Assistant e IoT
Construindo projetos com Google Assistant e IoTConstruindo projetos com Google Assistant e IoT
Construindo projetos com Google Assistant e IoT
 
Explorando Go em Ambiente Embarcado
Explorando Go em Ambiente EmbarcadoExplorando Go em Ambiente Embarcado
Explorando Go em Ambiente Embarcado
 
Soluções de IoT usando Arduino e Google Cloud
Soluções de IoT usando Arduino e Google CloudSoluções de IoT usando Arduino e Google Cloud
Soluções de IoT usando Arduino e Google Cloud
 
Soluções de IoT usando Google Cloud e Firebase
Soluções de IoT usando Google Cloud e FirebaseSoluções de IoT usando Google Cloud e Firebase
Soluções de IoT usando Google Cloud e Firebase
 
Criando soluções de IoT usando Javascript de Ponta a Ponta: do Hardware até a...
Criando soluções de IoT usando Javascript de Ponta a Ponta: do Hardware até a...Criando soluções de IoT usando Javascript de Ponta a Ponta: do Hardware até a...
Criando soluções de IoT usando Javascript de Ponta a Ponta: do Hardware até a...
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Building REST APIs using gRPC and Go