Building A Cat Fact JSON API With MongoDB And Golang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what are boys and girls and everything in between in this video we're going to scrape we're going to fetch some facts about cats we're going to store them into a database and on top of that we're going to build a simple Json API so we can retrieve um simultaneously because we're going to spin up the cat facts in another go routine asynchronously and on top of that we're going to build a Json API that can fetch all the facts we are scraping but before we continue if you're not subscribed yet to my channel and like the content consider subscribing give me a thumbs up leave some questions in the comments and jump into the Discord community and for the people that really want to level up as a go back-end systems distribute engineer check out my patreon page um let me know what you think about that let's go so basically I have this uh beautiful uh readme already typed out for you guys so it's going to be a little bit easier for the mongodb we are going to use Docker of course because we're not gonna hassle with installing that on our machine so we're gonna use Docker so the first thing I'm going to do is I'm gonna Yankee this line I'm gonna paste it in wait let me paste it in again Docker PS that should be fine so mongodb is already running then we have this the dependencies we need for a using Mongol a driver for golang is basically these two guys we need to install so I'm gonna basically uh yank the first one paste that in uh okay the screen then I'm going to yank the second one copy the second one paste that in what's going on ish it's CS code with the clever Shenanigans so I'm gonna copy it manually hey it is what it is sometimes so we're gonna say copy here boom paste it in and as usual I'm gonna basically put uh this simple project on GitHub and the link will be in the description so you can just follow along okay so we have the piece on very important it's basically the Json representation of our document in mongodb so we have uh Docker running we have these two things installed that's perfectly fine so what we're going to do is I'm going to grab this um this gold and quick start I basically pasted in here because hey I cannot remember all the time um how we're going to initialize the client so let's go to main let's paste it in and we're gonna see that we have connect and all that Shenanigans going on the problem is even if I save vs code will not import it but that's not big of a deal so I'm going to open up my readme and I'm gonna basically try to copy this and do it manually actually okay it's not working do it again that's fine delete these go get thingies some um quotes boom all right that's fine let me delete the screen just like that okay cool the next thing we need is this options thingy which is basically another package it's going to be options just like that and it's all fine so now we have this client heat so let me see uh if it's gonna work we're gonna say fmt print the line if there are no errors just like that uh go run main.go and let's see I think that should be good all right cool the next thing we're going to do is uh we're going to make a simple cat fact worker that basically going to spin up asynchronously alongside your Json API and it's basically going to fetch effects about cats hey so we're going to say type um I'm going to call it work it or maybe let's make it more explicit cat fact or work it uh that's going to be a structure and we're gonna actually embed a client not embed but just add a client to it and it's going to be a collides just like that and we're going to make a Constructor so we're going to say new cat effect uh worker I'm gonna say C I'm gonna give it an argument it's gonna be the client pointed and we're gonna return um a pointer to the cat Factor right nothing too fancy uh let's return cat fact worker here with the clients it's going to be C and we call it a day so let's make a function heat on our cat Factor so we're going to say funk let's do call it w or actually C W from CFW maybe get back to worker a H it's going to be the scat Factory thingy and we're going to say start uh let's return another heat why not so the first thing we're going to do is because we have access to our client we are going to get our collection from our database so we're going to say call that's going to be a CFW client for some reason I have this dyslexic client typing not quite sure why it's uh been there for all my life so CFW clients we're going to say database uh let's call this uh cat fact that's fine and yeah like this and then we're gonna call collection on this and our collection is going to call facts something like that right the next thing we're going to need is I take it a Time taker because uh I'm gonna show you real quick what you're gonna do so we have thunder client Heat so let's do https double print slash slash um cat fact and Ninja I think it's fact boom yes so basically each time we're gonna call this it's going to spit out a random fact and we are gonna make a loop that basically is gonna pull that end point and each time we have a new fact we are going to store that and to our database right so we need a time we need a ticket Heat and the ticket is important because we don't want to constantly keep pulling that endpoint because that would be nasty so we're gonna give some some time maybe two seconds or something and each two seconds we're gonna pull or something else you can choose but make sure you don't overload the service right so we're gonna say take it it's gonna be a time new ticket and I'm gonna say two seconds uh time seconds just like that and then we're gonna make a for Loop right and each time you're gonna wait for the ticket is channel right so each two seconds this channel is going to trigger and then we're gonna block until it's it's getting triggered each two seconds and then we're gonna Loop and continue right cool so what we're gonna do is we are going to say that the response error is going to be http get and like I said we're going to make this service this simple project very lean and mean right that's my my um my advice try to make things work first and then you can make it better right and if you have a lot of traction you can make it faster but don't do all these three things together because that will make sure you never complete a project right so HTTP gets I'm going to hard code this real quick we're going to say https uh CAD facts Nina uh Slash fact that's it if there is an errors I'm gonna return the edge and then we're gonna say varsh the fact or maybe the cat fact you just fetched that's going to be a v sum dot m and that's basically what the hell is that yes so B slot m is going to be uh also from the package we actually installed so instead of heat we're going to say bison and bison is um I think it's by Nettie serialized object notation not quite sure it's basically Json but um hey go Google it it's basically some kind of adjacent representation of your document uh binary mongodb kind of serialization look it up uh if you really want to actually want to know in depth what it is right so but consider bson just like some kind of a Json document right and this is basically if you're going to check what it is uh it's a primitive M that is basically not what we want to see but a matter of fact this is basically just um a map of strength to any right or a map to string interface like back in the days right for the ogs right this is basically what based on M is right so then we're going to say if f is going to be Json new decoder and that's going to be the response body uh decode me this cat fact right we're gonna decode everything into the cat facts and we're going to say if there is an errors and that error is not nil we're gonna just return the adult Heat fine and let us print out this cat fact thingy real quick to see if it's working print uh Alan are we going to say cat effect just print out the whole shebang so we have this clientish we're going to say that the worker is going to be a new cat Factor because we're going to put the client in and then we're gonna say workers start and normally and this will block you right so go run may not go call declare not used of course of course of course let's basically do this real quick so the compiler is happy right you can see each time each two seconds it's basically grabbing as a cat fact from the cat fact API hey thanks God something exists like that and that's cool so that's already working so now we have our call what we're going to do here is uh instead of printing it out we're gonna store it to our mongodb so I think it's something like the response may be the result actually a result error is going to be called uh insert one context let's call it context to do normally you could give a context background or something else but hey I'm just gonna um do to do and call it a date and the next thing we're going to do is basically insert our document and it's going to be the cat fact just like that and then we're going to have this error we're going to say if the error is not null we're gonna return an errors and otherwise we have this result and to be honest I don't care about the result right okay cool that's that so we have a record and uh we're gonna basically say go hit right because we're gonna spin up our uh Json API and if we don't specify go before work is start and it's basically going to block on this for Loop and then we cannot we will never reach Heat this code right okay cool so that's that's already set the next thing we're gonna do is we're going to make a server real quick uh server is going to be a structure and we're going to do the same thing we're going to copy the client the client because we're going to fetch uh we need access to the client because we're going to fetch um or get facts we distort in uh from our work right so um what we're going to do is Let's Make A Constructor a new server uh C is going to be the client actually it's going to be a client and we're going to return a pointer to the server we're going to say return me This Server and the client is going to be the C all that now you're going to make a handlet uh some some handles some API Handler uh we are going to call each time we're gonna try to fetch effect that's going to be S server you're going to call this handle get all facts or something just like that it's going to be a w it's going to be an HTTP response writer response writer then we're gonna have an R which is an HTTP 0.2 an HTTP request We're not gonna make this handle handlers more idiomatic with the network and all that stuff we're just going to use the simple things because it's going to make it work real quick uh consider this like some kind of an idea a business idea a very simple business idea and you want to quickly prototype something out to show to your hey I don't know CTO CEO whatever a friend your mom dad whatever so uh handle all facts what we're going to do here is um let us do the same thing here we're gonna fetch our collection and then we're gonna say something like I think it's good search is going to be coal gets find can we do no we need a context that's for sure context to do again and now we need to specify a query the question rather is can we do no because we don't want a credit we want to fetch them all I'm not quite sure if there is an errors and that error is not nil um let's just make it dirty and um look fatal out this Edge real quick so we're going to see if at is going to be the good source all context to do uh what's going to be the results that's going to be results there's going to be a slice of bison m uh let's lock fatal Heat you could also Panic that I think Panic is just a nasty representation and I'm gonna say um W I had to actually right we're gonna write the header real quick that's going to be status 200 we could do HTTP status okay just like that and then we're going to say w we're going to actually do this Json new encoder W and codes the results that's what we want and to be honest if you really want to do good you're going to say w headers at I'm going to say content type and it's going to be application Json right just like that that's actually a perfect fine perfectly fine all right so that should be good the next thing we're going to do is uh to be honest maybe we could hey guess what it is so we have this we have our go work a start so it's going to work it's perfectly fine and then we're going to say HTTP um handlers Handler funk I think handle thing by the way I'm going to say slash facts because first of all gonna boot up our server we're gonna say server is a new server we're going to give him the client the mongodb client so we have access and then we're going to say our Handler is going to be server get all facts and then we're going to boot up our server we're going to say HTTP listen and surf uh let's say Port 3000 and then we're gonna say nil here because we don't need any handlet all right cool so let's recap we have our client here we're going to make a new a new mongodb client basically the localhost uh 27017 is the classic default mode Port then we have a worker which is basically a cat fact worker we can start that up in another go routine so it can basically keep pulling an uh store new cat facts then we're gonna put up our server and we're going to expose uh a route an API route which is basically just going to be slash facts and we're gonna boot up the server heat with HTTP listen and surf on Port 3000 let's see if it's gonna work boom all right the next thing we're gonna do is open up this new request heat and instead of doing cat facts we're gonna call to our Local Host localhost 3000 facts uh document this nil okay cool that's the thing that I was thinking so the problem is what we need to do here is instead of um where is nil here instead of call find nil I think we need to do something like the query and that's going to be a b some M just like that I'm going to make an empty one right I'm gonna put this Grady inside of it I think I think that's uh should work perfectly fine not quite sure just sometimes you need to try and to around with things to actually explore right uh it's all good it's working let's do it again all right you see so we have this cat facts here it's perfectly working um I think we all had to line 50 if we do it again we should have more exactly 72 because we are continuously scraping more cat facts right um all right that's basically it we um did some basic API uh calling a get API get calling and golang we basically booted up uh or mongodb storage insert some documents retrieve some documents very lean and mean and maybe in the next episode if I come back to this I'm going to refactor it because there's a lot of things we can do we could use interfaces to make our application uh more maintainable more readable and all that stuff and extendable and testable but I think for the beginner cooling Engineers if you want to learn Googling are you coming from another language this is a very nice way to start to see what's possible and so you can actually start playing around with this the link of this repository will be in the description if you like these videos consider subscribing to my channel give me a thumbs up leave some questions in the comments check my YouTube channel because there's a lot of more advanced content for you for every single level of Engineers and for the people that really want to be a Savage goaling engineered microservices distributed systems blockchains check out my patreon page thanks for watching and I see you in the next video news
Info
Channel: Anthony GG
Views: 11,304
Rating: undefined out of 5
Keywords: mongodb, mongodb tutorial, mongodb tutorial for beginners, golang mongodb, golang programming tutorial, go language tutorial, golang for beginners, golang, golang tutorial, programming, learn golang, go, go programming, go programming language, go tutorial, golang tutorial for beginners, go programming tutorial, go tutorial for beginners, go for beginners, go programming language tutorial, golang 2022, learn mongodb, golang tutorials
Id: iak56rgR05A
Channel Id: undefined
Length: 18min 38sec (1118 seconds)
Published: Wed Dec 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.