Building a Scalable Rest API with Golang: Dockerization and Browser Showcase

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] foreign [Music] and today I'm going to make a restful API using golang and I will dockerize it using Docker containers all right so the first thing that I'm going to do is to create a file here first of all directory and that would be go rest API like this and once I do this what I will do I will create a file here by the name of main.co and after that the next thing is going to be like Docker file and then the third file that I'm going to create is the here in the terminal like what you can say go rest API and inside it I will write git sorry go mode in it like this so yeah these are the three files that will be created and now what I will do I will first of all write some go code here and the first code that I'm going to write is package Main and once it is done what I will do I will write a type structure of sorry type task trucked and inside it I will write three variables the first one is the ID the second one is the what you can say title the third one is going to be the status all right these are the three strings that will be used here so now let's go down and write a variable that will be a slice and this will be task unlike all these tasks so all the variables that are present in this task structure will be present in this slice variable all right so yeah so after that let's write function that will be task Handler [Music] and this one is going to be like first of all right response writer and then request and at the end what I will do I will write switch our method is equal is equal to like what you can say case there will be multiple cases the first case is the get so if the request is gotten then what it will do it will write Json Json in new encode new encoder W that will be right and in code what you can say tasks so let's write tasks here instead of writing tasks so yeah what it will do if the request is gotten from the user then if you what it will do it will uh encode this uh into the Json format these tasks the variables that are present in this task structure these tasks variable in which the data will be present they will be encoded into the Json format and they will be returned to the client but let's say there is another case like post if the request is posted then what it will do it will first of all take I will write body and write iotl read all if the request is posted then it will read all the data from there like first of all task uh sorry a read body it will read the body of that request if the request is posted then what it will do I will create a further what you can say variable and that is task so after that let's write what you can say Json and Marshall all right Jason unmarshall is going to be body and that Ampersand task so what it will do if the request is posted then first of all it will read the content of that request from that body and after that it will unmarshall that body into the new uh from that task structure all right so it will unmercial the data from in the Json format into this uh from this task structure into this body and after that once it is done what I will do I will write task and append a new uh sorry let's write tasks here instead of a task a pen task into this task so once this uh data is unmarshalled from the Json format it will be again uh what you can say appended into this uh task structure as a new task structure and after that it will be encoded there like this from the new uh Json format from the new task from the previous task structure it will go to the new task structure that I have given it here like this so let's write w uh encode and then the task like this all right so from the previous task structure it will go to the new task structure and it will be encoded in that Json format so this is how it will work or if the request is posted but if the request is gotten then it will directly directly uh encode it into the Json format so let's write function tasks Handler previously is what it was task Handler but now it is tasks Handler so let's write the same response writer in the request request and now what I will do I will write a switch now what you can say our method is equal to like uh inside this what I will write I will create a case that is gotten if the request is gotten then what it will do it will write an ID like this one so the ID will be fetched so first of all let's write stream prefix shrimp prefix or URL a path and then the tasks so what it will do it will basically take the URL and from that URL it will remove the tasks and only give me the ID of that all right so it will only give the task ID instead of it it will just remove these tasks and the URL for and it will return me this ID that I will fetch it from there and after that let's write four now I will print out the IDS and use it further so what I will do I will first of all write the for Loop and that would be item is equal to range tasks and the next thing is going to be like if statement if item ID is equal to ID itself so if the this ID is given if this ID is equal to the item ID that is fetched from the task if both of them are equal then it will write Json new encoder and encode item so in this case it will encode that item into into the Json format if both of these IDs are equal all right so let's say if I have given one ID number one then it will fetch the item according to that ID if I give the item ID number two then it will give me the data according to that ID number two so yeah this is how it will work so now let's write return and let's write HTTP error let's see if the error is occurred so what I will do I will write a HTTP error like a task is not found like this task not found and the next thing is going to be like what you can say HTTP [Music] HTTP status not found like this so it does not font this is how it will work but now let's see if the get request is itself is not working then what it will do I will write a default case like this HTTP error then right like this invalid request method if the HDR if the get request is not working so let's write HTTP status method and not allowed like this and now let's write the main function in which these functions will be executed so yeah let's write tasks so now this is going to be the task so the first choice that I'm going to give and that will be like what you can say append tasks into this task there will be task appended and the task ID first of all is going to be like one and let's say if the task ID task ID is given now let's write uh what you can say our title title is going to be uh task one and the next thing is going to be status and this will going show me pending this is this pending status of this task so let's write what you can say HTTP sorry HTTP handle func yeah this is how it will work and this is going to be tasks HTTP no sorry what is this function is Task Handler task Handler is the first function and now the second function that I'm going to use is Handler funk for this task without any ID so here the ID will be given but here no the ID will not be given so I will write tasks headland so here what I have done is that if the request is gotten only in this function the request will be got and there will not be any ID of it all right so as you can see here that I have written the task without using any ID or without using this sign so it means that it will only accept the data if that data is given to it it will not accept any kind of other IDs instead in this task Handler functionality it will accept the other ID is also like this ID that I have given it all right so this important point and now let's write um fmt print line server listening to Port 800 like Earth 8 000 right and let's write what you can say HTTP listen serve 800 8000 sorry email so yeah this is how it will work so first of all it will take the idea and the second one it will not take the ID all right so yeah and in both of these functionalities it will work so let's save this and now let's try to go run main dot go so server listening to Port so I need to first of all remove this file because maybe due to this it is giving me an error so let's execute this all right so why it is giving me an error HTTP listen and serve yeah so this is a problem that I face and I need to write like this and now this is how it will work so let's clear this and now if I write go run main.go now it will work fine server listening to Port 8000 so let's go back and print what you can say remove this and create open another Tab and live right eight thousand like this now page not found but now if I write tasks you will see the task is also not found so if I write 1 it will give me the ID you can see that I give the ID number one it has given me this ID here but if I type and this tasks only it will give me the error the task node font so I need to work on this also so let's close this first of all and let's check it so so here is a problem that I faced and the problem is this in this name so let's remove the s from here and put the name as here like this all right so this is how it will work so let's clear this and write go around me dot go and now if I refresh this as you can see here this is only tasks without any ID so as you can see here that it is giving me this uh data without giving any ID but if I touch a specific ID it will give me the data according to the specific ID also all right so this is how it will work so now uh the functionality is running by in this go main.go file but now I need to dockerize this so how can I do this I deleted the file like first of all I will create this again Docker file and let's write uh from golang like this and delete so I will take the latest go link image and once it is done what I will do I will write run what you can say make dirt and like the application app or directory so I will create a app directory and all the resources from the uh this uh file will be transferred into this app directory after that let's write what you can say add it means that I add all the resources from the main.go file to this app directory all right and once it is done what I will do I will write a working directory like this one so the working directory will not be now the main directory instead it will now be the app directory that I recently created and once I do this after that what I will do I will write uh run and go build o main dot so what this will do it will create an executable file this mean I mean it will create an executable file from this main.go file and after that let's run this executable file by using CMD executable that will be present inside this app and then mean so the inside this app directory there is a executable file that is that is present by the name of Main and this will be executed once this is executed it will be builded and now I can run now I will be able to run this without running this go run dot main code main.go file all right so yeah let's save this and now let's clear this let's write docker first of all let's remove this and open a new terminal go rest API and now let's write Docker build uh t and Docker ability and now let's write go rest API and Dot so that will be taken dot means the other data or the resources will be taken from this uh main directory all right so it will take some time so let's wait for this so now you can see here that the docker image is finally built so what I will do now I will write Docker run and then d uh for making them in the detached mode and then a p for the port and the port is eight thousand and this like and the first one is the ear thousand and this is the ear thousand like this support now number like as you can see here eight thousand so after that let's write get sorry go rest API so let's run this and now it is run now as you can see here that I'm not building uh what I will do I will not run go run me.go and if I go to the Chrome browser and refresh this it will still give me this message why because it is now built on the docker it is now uh without any without running the go run main.go you can still execute this like this all right so it is now running on the docker side not on the uh go link side all right so this is how it will work so now this is completely built now so what I will do now I will write a CD go back and after that let's write git add and git commit and what I will do I have created a rest rest API application and dockerized it so yeah this is how to grid push origin me so now this is done now let's go to the uh what you can say GitHub repository and yeah hello world this is sorry get go rest API so this is the application that is currently uploaded here and you can check this result here all right so yeah so now I hope you understood and liked this video so if you liked it then make sure to subscribe to my channel and if you have any questions then ask those questions in the comment section below I'll be happy to answer all of them so till then goodbye [Music]
Info
Channel: Code Roamer
Views: 248
Rating: undefined out of 5
Keywords: devops, projects, project, ibilalkayy, freecodecamp, technology, golang, software, freecodecamp.org, coding, programming, tech, languages, tutorial, course, tasks, traversy media, git, mysql, mongodb, mentorship, cobra library, command line interface, application, scholarship, call for proposal, promotion, twitter spaces, events, monokle, kubeshop, kubernetes, business, startup, session, motivation, books, bootstrap, investor, investment
Id: YWPe8LwDvuY
Channel Id: undefined
Length: 17min 25sec (1045 seconds)
Published: Thu Jul 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.