Microservices in Go - Heroku and PostgreSQL P5 - Deploy to Heroku

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody welcome back this is part five and the final part for this series micro services and go with heroku and postgres we've finished building our microservice and now we're gonna provision a postgres add-on and deploy onto heroku make sure you hit the like comment and subscribe button for the channel uh helps me out a ton and lets me know you'd like to see more thanks for watching let's get into the video all right i've gone ahead and signed into heroku here and we're just on the landing page where we see apps let's create a new app for our microservice let's give it a name i'm just going to call this devtopics.psql and we're going to click create app and this will set us up here the only remaining piece we need to do let's go into resources here and this is where we can search for all of heroku's add-ons i believe we click this here and if you'd like um you can browse through and see all the offerings they have for data stores they've got several kinds of db's they've got redis for caching they've got my sequel i believe they have a add-on as well we want this for a good postgres one so let's go back we'll just type in postgres and we'll select heroku postgres here and there's a couple of different plans you can do starting out what i really love about heroku is that you can get most stuff for free so click the hobby dev for free and we'll submit order uh when you do that you'll need to have a credit card on file i've already added one this is totally free you're not going to get charged to build anything and we see here attached as database so that's attached to the app and what that means is we go into settings under config bars here you click reveal config vars and you'll see we have this database url attached to our app now so every time we deploy to devtopics psql and our application starts up we're going to have this database url environment variable and we can see here it's in this format it's a little bit different than what we've seen with our docker compose and the last little remaining piece of code the last code change we'll need to do here is getting our getting a database connection to work with this type of url so i'm going to go ahead and copy this and now let's jump back into golan's and get to work all right so we're back here in go land so remember i believe in part one long time ago we set up database url this environment variable um and if you remember heroku provisioned us this database url environment variable same one whoops quit that so we want to now actually start using the center app right now it's wired up to point to db but it's actually not even used anywhere inside of our application inside of db.go we just have this hard-coded to use this string db which because we're running inside of docker db actually is the address of this container it's the actual like address if you're running this a local network you see some kind of like ip address right you see something more like that but we're in docker lane so we see db so we want to have two different types we're going to need two different types of connections one for local one if we're in prod so let's separate that out here so we can do var ops either way we're going to end up with this opts object pg dot options and we want to have an error some kind of optional error as well okay so either way we're going to end up with this opts and an error and we're going to use the odds all right so let's take this i'm going to cut that out and just put option okay so if we're in production and for this i'm going to make a new environment variable and we'll call it enb just environment and if we're in production that will be set to prod so we'll set this on heroku if we're in production we're going to do one thing otherwise that means we're in dev so we're just going to say anything that has an environment other than production is just dead that's what we're going to decide you might have different environments maybe you have staging tests whatever that's up to you so alts in the case we're just running in dev we just want our old box just like that and it'll get put down here and we'll connect with that okay i see one error here it's not the first time we've defined error anymore otherwise if we're in prod if we're in prod we actually want to make use of this database url from the environment variable so we can do that with os.getin and database url that gives us our database url that heroku has set for us now how do we use that so it's it's in that super long string format right it looks kind of like this right it's in this format so obviously where's the user where's the password they're all in here but we don't want to have to like manually cut little pieces of this out so pg the library we're using gopg has a nice helper for this it's called parse url so it takes this kind of long format url and it parses it into options that can use connect so it kind of does all the heavy lifting for us so let's take this pass it in so that's the url and this gives us an opt and also an error now if we do get that error we just want to return out of this function so let's handle the error and otherwise we're all set up so otherwise we're going to connect to the database there okay so this only happens we only use the special parse url when we're in prod so let's actually set that in heroku so let's say end and we'll say prod and we'll just hit add all right so we should be good to go and just to test this out we don't have the environment variable set here right we don't have any set so let's just do docker compose we'll just restart this here make sure everything is still working as expected so this should still be using this local database here cool so we see we're still able to connect to the local database inside of docker compose and we're able to run our migrations so everything's working as expected let's bring this down okay so the last piece here is to actually deploy this onto heroku so let's get going with that all right so deploying is pretty straightforward we can go over to the deploy tab and we'll scroll down a little bit here so i already have the roku cli installed if you don't click this link and just follow the guide to install it so we are going to be using the container registry we built our microservice with docker and they have a nice little guide right here so broker log in i already ran this one and they just make sure that we have docker running locally we do so the next step we just log into the container registry cool login succeeded and now we can just push up our docker image all right so we made it um that took a couple minutes for me so no worries if this is slow if that's a little bit slow for you it'll finish okay so we pushed our container the next step is to release it so with hurricane's container registry you can push up a bunch you'll have a bunch of containers with different versions and then you choose uh you can release just the latest version or specific versions of that container okay so let's run release and this is actually going to start up our app for the first time it's it doesn't care about this docker compose anymore um it built this docker file and i pushed that docker file up to reviews container registry so this will release it um this is kind of actually deploying our microservice for the first time so let's run that cool so we see releases uh releasing images to web um all good there so next step um we'll go to more and click view logs so this is um a place you'll be spending a lot of time uh a lot of time at on heroku um just viewing your apps logs all right so we see release b5 was created by devtopics that's us and let's see here sorry not v5 um this was some previous testing stuff here so this one failed um but we see here release v6 was created deployed web and scaled up so we actually scaled up a little micro service one one server and i was running it and we started the process and we see that version is two so our migration scripts are up and running i've already ran this one time previously one time previously just testing things out so if this is the first time you're running it you should see that migrated from version 0 to 2 but here we've already migrated our versions so it's that version two and we see our little log statement we're up and running so let's test it out so we go into settings um here's our url this is where our app lives and let's hop back in and we can actually curl stuff from here so curl um and let's try that all right so we just curled our homes endpoint and if you remember we go into api here uh slash homes just the root one here um just the root one with a get with the get method is uh retrieves all homes um but if we do homes slash like a home id we should get like a 404 not found yeah so cpg no result uh no rows in results set so that's what we're looking for and let's create a home so this is that request body we've been working with this whole time let's go ahead and do that so curl post and we want this for our url and it's rec.json and let's go ahead and parse this type this into jq and let's see what we get okay so yeah that would have failed we did holmes slash 123 so we don't have a handwood for that so let's fix that and let's run it all right so that time um things worked out so before we probably got like a endpoint not found or something exception um here when we have the right endpoints uh we get success true no error and we see there's our home we just made let's create another one price save that and let's run it again there's id2 there's our other home and let's test out agent id so let's do 500. so remember we just have one agent right now and that has an id of one so any other agent should fail so let's try to create with an agent that doesn't exist yeah so we tried to insert a record on a row on a table homes and it violated the foreign key constraint homes agent id foreign key so that one failed so now if we do get all homes there's all of our homes and we see it's working just like we expected so that's it that's our that's our app right there that's our brief little micro service with postgregs and you can see just how quick that was um really deploying that so once we got all the coding done right that was a long part writing out the microservice itself that's the long part but actually like making an app provisioning our postcards resource setting up setting up all the environment variables and then finally actually deploying our app right that was very quick um so that's kind of the power there of heroku it lets you get up and running just really really fast you don't have to worry about for example with amazon web services setting up an iam role getting the api tokens um access to specific resources all that kind of stuff don't have to worry about any of it heroku is just a simple way to hack something together and get it going so if you're just testing out a little startup idea or just a weekend project use roku and when you start scaling and running into issues like that after you've proved your idea and proved your concept then it's probably a good idea to move over to a bigger host and a bigger provider all right so that closes up this series micro services and go with heroku and postgresql hope you learned a bunch here in the series uh thank you so much for watching and please again like comment and subscribe to the channel really helps me out gives me a lot of encouragement to keep going and making more videos for you i hope you're able to learn something from here and thank you for watching and i will see you next time
Info
Channel: devtopics
Views: 2,334
Rating: undefined out of 5
Keywords: microservice, golang, microservices, heroku, api, backend, software, docker, docker-compose, docker compose, deployment, deploy, postgresql, postgres, go
Id: qeElSTaEB6Y
Channel Id: undefined
Length: 14min 23sec (863 seconds)
Published: Tue Sep 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.