How to build an event-driven backend application in Go with Encore

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
here's an optime monitoring system built with Encore before we jump into the code let's open up the local development dashboard to take a look at the Encore flow diagram representing the architecture of the system you can see from the strong lines between the Monitor service and the two other services that were making API calls between them in this video we'll switch that out to more of an event driven architecture to decouple these services from each other and make the system more reliable so let's take a look at the code all right so what we have here is the Site Services code and on the left we have the source code for the add endpoint and that gets called every time a new site gets added to the database so what we want to do is we want to add a pubs up topic where every time a site gets added a message gets published on that topic and then the monitoring service can subscribe to messages on that topic and then immediately check whether that site is up or down so let's go first thing we're going to do is we're going to create a package level variable let's call it site added topic and to do that we're first going to import The Encore dodev pbub package the pubsub package is part of Encore infrastructure SDK and it Mak makes it extremely easy to create a new pubsub topic all we have to do is call pubs sub. new topic and store that in a package level variable and we need to have a the message type itself so let's use the existing site type next the pops up topic needs a name so let's call it site added and finally we need to specify some configuration for pubs subtop topics all we need to do is to specify the delivery guarantee in this case we specify the messages are delivered at least once so now when I hit save you can see that Encore immediately detects the change and recompiles application and on the right the topic immediately shows up now it's time to start using it with Encore there's no need for additional configuration the topic is ready to be used and you can simply start calling publish on it right away all that's left to do is to add an error check and then we're done we can now hit save and enor will detect the change recompile your application and the flow diagram updates to reflect that the site service is now publishing messages to this topic and now let's update the monitoring service to subscribe to this topic and process messages that are coming from the site service to do that let's head over to the monitoring service exactly like we did before let's start by importing The Encore dodev pops up package then we'll create a new subscription and like before we'll create a package level variable in this case we don't need to give it a name because we don't need to reference refence it the first argument is the pub sub topic itself and here we reference the variable from the site service next we need to give the subscription a name finally all that's left is to provide a subscription config this configuration can contain many things but in this case the only thing we care about is the Handler the the Handler is the function that gets called every time there's a new message on the subscription so let's define it right away in this case let's look at what we already have we have this check end point which is an encore API endpoint this takes a site ID and looks up the site but in our case we already have a site so in our case we can use this check endpoint directly which takes a context and the site object that's the exact signature that the Handler configuration field accepts on our subscription so we can use that directly and with that we hit save and onor automatically updates the flow diagram on the right and we now have a complete pubsub powered backend application let's try it out and see that everything works since we're now publishing a pub sub message every time I site gets added we should expect that when I hit save the site gets immediately checked which is exactly what happens so now all that's left to do is to commit our changes and push them you can either push directly to Encore or you can push to GitHub Encore comes with a built-in CI CD system that automatically builds and tests your application once completed all will move on to provisioning the necessary infrastructure based on the changes that have been made in this case this application had already been deployed which means that the only changes needed are to provision the pubs up topic and subscription that we just added finally enor will deploy the new release to the environment in question and the best of all is that everything works with your own cloud account this means that you can get the best of both worlds you get the incredible developer experience powered by Encore with the scalability and reliability that you're used to from your cloud provider of choice so now that the application has been deployed let's make sure that everything works in production and it does and so with that thank you very much for watching and I'll see you next time
Info
Channel: Encore
Views: 32,462
Rating: undefined out of 5
Keywords:
Id: ipj1HdG4dWA
Channel Id: undefined
Length: 6min 22sec (382 seconds)
Published: Wed Apr 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.