The BEST gRPC Framework for Golang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to introduce you to torp which is a framework on grpc we're going to follow the readme I'm going to briefly explain what is grpc RPC why it's different than HTTP going go through an example and why you might want to consider this next time you build out your backend architecture all right so I'm over at github.com twitch.tv/ torp and this is a framework made by twitch and let me just read it torp is a framework for service to service communication emphasizing Simplicity and minimalism it generates routing and serialization from API definition files and lets you focus on your application logic instead of thinking about folder R like HTTP methods and paths and Json torp is similar to grpc but without the custom HTTP server and transport implementations what is grpc well this is a framework created by Google and as it says grpc is a modern opsource high performance remote procedure call that's what RPC stands for framework that can run in any environment it can efficiently connect services in and across data centers with pluggable support for load balancing tracing Health checking and authentication it is also applicable in Last Mile of distributed computing to connect devices mobile applications and browsers to backend services and that last part the browser to backend Services is what we're going to focus on with twerp in this video as with any kind of example uh let let's just go straight to the documentations so here it's twitch.tv dog. torp docs and we go through the intro so before going right into this there's basically two components but there's going to be more up to four there is going to be your Proto file and this is what I meant by the definition so you can have you see here there's a few syntax import examples here we can ignore those for now and then you have the actual service with the RPC endpoint called hello so this is the actual route name hello and it's expecting a hello request which is defined right below which only contains the string or this subject field which is a string and it's going to respond with hello response again defined right below message hello response it has one field which is text which is going to be of type string so now we have this type safety across our API layer with Proto Buffs here but however before you do that you definitely need to install a few things you need to have go installed you need to have protocol buffer compil or Proto and you need to install torp as well so I'm going to link all this in the description below all right so here I have a completely empty directory there's there's literally nothing here so I'm going to go ahead and create a new go mod in it I'm going to just call this github.com example this is what they kind of suggest you initialize your app so now we have our go.mod file and before creating a main.go let's actually go back into the docs and look what they recommend for us to do so here start with the Proto definition plat in RPC habd daser service. prototo so we need this Proto file so let's go ahead and make the directory and add our service. Proto file all right so here I have vs code open I'm going to open up the servers. Proto file it's completely empty it's nothing there but let's go ahead and copy the uh servers. Proto file they recommend in their example and walk through it go ahead and copy this let's quickly just walk through everything here so here syntax is pro I think this is allows Proto what you're supposed to install and you can actually check if you have anything installed here so you can do Proto I think it's Das Das version so you can see here I have Li product 3.6.1 so make sure you have that and for my Go version I have go I think it's just Dash version actually just go version yeah Go version 122 so I have the latest Go version okay so quickly breaking this down we have this Hab Dasher service makes hats for clients so we just Define our servers and then within the service you can think of these RPC definitions these like function calls as the routes to our back end so if you use any kind of previous go tutorial you'd have maybe a routes. go file and that would have everything defined you may have used Chi or Jin or maybe just the standard HTTP package from the standard library to Define your routes and it would have like a Handler Funk here that kind of concept doesn't exist with with Proto files instead you have this RPC function called make hat so you just declare the name of it expects a size if you define size here you can see size of hat in inches so it's expecting an argument of inches which of type in 32 and it's going to return a hat you can see right down here our hat is the expected response from our make hat procedure and it's going to respond with three things a inches color and name and the inches is typed in32 color and name are both strings so again I really like the fact that this has a type safety in our API and if you're talking between different you know systems if you have a decoupled front end from a decoupled back end this is going to be very very nice for you so now we have one make hat function defined let's go ahead and actually implement this and generate the Proto files to generate code run the protu compiler pointed at your services. prototo files now I want to warn you if you run this you'll get a direct error I'll show you uh right here so if I open up my editor right and if I just literally paste it I'm going to get an error so there's a quite a bit of a typo in the docs have no fear I'm here to help you out to get that result you're going to go ahead and be in the rot of your directory so outside that/ RPC directory and you're going to run the following command what you need to do is run the product command and the first argument we're going to pass in is going be torpor out and what this is going to generate is those torp files for us which is essentially how the torp framework which is on top of the grpc framework wires everything up from our protuff file to our actual server which we're going to pull in and have all those nice type definitions so we're going to do path it's going to look a little weird and we're going to do sourcecore relative from where we're currently calling this and then we're going to do goore out so this is the just regular grpc framework I'm going to generate those files as well so we're going to do the same thing path equals sourcecore relative and now we need to Define where that service. prototo file is the one we just defined and that is an RPC slabber Dasher service. prototo so if you run this you can see here we don't get anything from our CLI if we open up our project tree you can see here now we have the service. PB .go which is generated from the d-go out flag and then we have the service. torp and you can open these up you can see there there's quite a bit of files here I mean there's large files there's a bunch of stuff that's autogenerated you don't really want to mess with this you can see here we're getting some errors off the bat so these errors that we see here these are kind of just annoying so if I to get rid of these errors you can do a Gog get from the root of your project go getet google. golang.org slpro buff slpro latest and if you run this it should get rid of some of them but now we get some from our tour file so let's go ahead and get rid of those and this is just complaint you can see here at line 11 that we don't have the github.com Twitch tv/ torp in our go. suome so let's go ahead and get this as well you can just do goet github.com Twitch TV sltp awesome so now we don't have any errors we can actually start importing our definitions into a server if you guys like go in this kind of content make sure you click subscribe button it does help the channel a lot a lot of effort goes into these videos and it truly is the best way to support if you enjoy but let's get back to the video all right so going back up you can see if you open the generate T.O file you should see a go interface like this so this is kind of what defines everything for us to use along with code to instantiate clients and servers so now let's implement the server so what we're going to do is basically a very simple go file defined in interhab Das server server. go so let's go ahead and create this and basically copy this down and explain what happens all right so you can see here I have internal Hab Das server and then server. go let's go ahead and do our package declaration here and let's just copy the files from from here so package Hab Das server we already have that let's go ahead and just copy this down all right so let's explain what's going on here so the Imports I don't think are too crazy however the coolest part is this this is github.com example RPC slabber Dasher so this is kind of why I went with the same Mantra of defining my go dood file here as github.com example I'm sure it's not like anything too crazy but this allows us to basically reference whatever is inside our SL RPC slh habd Dasher folder so you can see here this is basically these files here these Dogo files all the way up here package habber Dasher and this is what really contains the magic for us to implement our strs and our requests so a hat is a piece of headwear made by a Hab daser but you can see here this is what we actually care about this are the return expectations our inches color and name and this is how everything get wired via torps if you go all the way back this is how we can pull everything in you see we're going to call it PB we have this server sh which is empty but you can populate this with anything you want I'll actually show you an example after this one so then we have this function called make hat which is a pointer to our server struck which essentially is nothing but you can see here the first argument expects is this context and then this pb. size pointer you can see here this is where we have that definition that inches or even more importantly that int 32 and this again is where that magic for RPC grpc and torp kind of come in you can see here if size of inches is less than zero we're going to return error and torp actually provides us with some nice errors off you know baked in so invalid argument error we can wrap our own errors there a bunch of things that come baked in with torp but if everything goes well we actually return a pointer to our hat and if you can see this you can see inches color and name and we just say we return the same inches from the request essentially the size request and then we have our color and our name okay and so the next part that they kind of want you to do is just to create this command server main.go and actually made a mistake so just grab this go back don't name it package cuz this act as our entry point so just paste this in here you can see we have our package main we have all the external kind of routing configured for our go package so have the server which is going to be just declared from the server definition again this is just a naked or empty server struck and then here we have this habid daser so we can go ahead and run this and actually curl the request for make hat so let's go ahead and open up a new terminal you can see here and all I'm going to do is so I'm at the root of my project we can do go run CMD server main.go we don't have a print message which probably would have been better in hindsight but that's fine but we know it's listening on port 8080 if I go ahead and open up a new terminal I can actually run a curl request to curl the make hat endpoint so the endpoint definition is a little wonky if you've never worked with a protool file it's not intuitive in the sense that the actual name and this definition this tor. example. habit daser makes a huge difference in how you are able to C your quests so basically you can see here torp works over HTTP 1.1 all RPC methods Mount to routes to follow the format post and then prefix SL package do service method so the prefix is torque by default but it is optional and can be configured with other paths like my custom prefix you can see there some examples here so how does this work in our example with our service. Proto definition so basically c-x poost it's a postest we do HTTP localhost 880 slerp and then the next part comes directly from our service. Proto here so if I go all the way up put this down a little bit this is where this tw. example. habd daser comes in so tw. example. habber Dasher and then the name of the service and the name of the service on line 13 is capital H habber Dasher so Doh Dasher and then the path or the function call which is make hat so we do make hat and now we can add our headers so content application Json so went ahead and reformat a little just to make it easier but it's essentially the same thing okay so now if if we submit this you can see here inches 12 color black named Derby and because if we actually look at the uh service. go file it's just randomizing between the five options in our string slice here and between the four options in the other string slice here so color inches 12 color is black named Bower and before it was color black named Derby okay so that was a good kind of cute example of getting started with grpc and torb I want to showcase more of like a maybe a bit more advanced example example I'm not going to go too in detail of it but I want to showcase the format of how you can kind of write your requests and responses so here I have the exact same habber Dasher service with the exact same make hack function I didn't change that uh you can see here I have more function calls have this one called register create post create comment delete comment update comment and then toggle comment reaction so all of these have their own unique expectations so you can see here for register we have this register user request and if we go up wherever it's defined you can see here that we're expecting this email a first name and a password go back the same thing we have this register user response and all this way down and then in our servers. go you can see here we have tons of different definitions of how these functions actually get called like this is the API layer right so we have this create post function here on my API layer uh it does some validation assigns a new uid does some realigning here doesn't really matter this is an example and then down here we actually have a database call to create post and this actually inserts the value into our database so that's all there is to say about grpc RPC and TP in general it's that it's an alternative to http I think it makes writing your backend logic way easier it's faster it's really really nice especially if you have these microservices that have to talk to each other this is such a good protocol to handle that but if this is your first time thinking about torp or even using grpt let me know in the comment section below what did you think did you like this doesn't make sense again everything I talked about here will be available all links in the description below but make sure you comment like And subscribe for more of this type of content and I hope you guys learn something and go build something with grpc empow [Music]
Info
Channel: Melkey
Views: 5,537
Rating: undefined out of 5
Keywords: computer science, software engineer, computer programming, coding, learn to code, machine learning, artificial intelligence, cloud, python, javascript, how to code, go, golang, how to write http services in go, how to learn go, best resources to learn golang, how to get hired in tech with go, programming, software engineering, theprimetime, theprimeagen, fireship, web design, web development, programmer humor, grpc, rpc framework, how to learn programming
Id: TJuda4hJYfo
Channel Id: undefined
Length: 13min 46sec (826 seconds)
Published: Tue May 07 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.