Getting Started with Vapor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to azam shar weekly so i am starting to work on my brand new course which will be about micro services using vapor framework so i just wanted to introduce you very basics of vapor what exactly is vapor and how you can install it on your machine and to get started so vapor is a server-side swift framework this means that you can create your own server and interact with it all right now obviously there are many different technologies that you can use to create your own server like you can use asp.net no django you can use ruby on rails so now all of this stuff is available using vapor and vapor is not something that just came out i mean it has been around for a couple of years but they just added support for the acing and of 8 which is swift 5.5 features so we'll be looking into very basic stuff how to get started with vapor and eventually i'm working on the course for micro services we'll be using vapor for that and that course will be available sometime next year all right so the first thing you need to do is to install vapor and in order to install vapor i'm just going to go to the documentation and make sure that you have export 13.1 and if you want to use the async and await feature make sure that you are using mac os monterey so make sure that you're using that or else you will not be able to use those async and await features in vapor so starting vapor is pretty simple just use brew install vapor and that's it you're done it will automatically install vapor make sure that you have export 13.1 that's very important all right now i have already installed vapor so what i am going to do is i'm simply going to jump onto my desktop and going to go ahead and create a brand new vapor application so vapor new hello vapor or you can write any name you want i'm not using any template to build the application so it's going to ask me that hey if i want to use fluent which is for database i'm going to say no if you want to use leaf which is to create server side pages i'm just going to say no and that's pretty much it now i can go ahead and jump into that particular folder and run vapor xcode so that i can open it up in xcode so now xcode will be launching 13.1 and i will be able to see what this particular project contains now the first time you are going to be launching this this is going to take a little bit of time all right because all of these packages and dependencies they need to be fetched so that's why it's going to take a little bit of time but looks like it's working fine if i open up my you can see that this is a vapor project right over here and it has many different files over here let's go ahead and take a look at a bunch of them so the test folder is for writing test all right we have a package file which contains all the different packages that we have we have the app folder and the run folder the app folder contains controller which is empty we have a configure file which kind of configures the routes and launches the app or basically the configuration of the app the routes contain all of our routes so basically if we go to the route it's going to return us it works if we go to root slash hello it's going to return as hello world and we have the main file that sets up everything and that launches vapor now in order to run your vapor server we will go ahead and click on that play button and it will hopefully give us some sort of a url right over here which is going to indicate that our server is running and it's going to give us a url of our server so that's pretty cool that just writing no lines at all we were able to start our own server so now i can go to this url and check out my server that is written in swift so let's go back to our code over here okay might have to open up a new window and go to that server and awesome it actually says it works whenever we go to the root url it is automatically saying it works so this means whenever we go to the root url it is going to this particular code and running that and returning it works if i go to hello then it should return me hello world so let's go to hello and it returns me hello world so it looks like our server is up and running this is already super exciting that we were able to create our server using swift so we are not really leaving the world of swift and ios to create our server which we can but we are living inside a bubble of ios and we are using swift language to create our server also apart from creating our ios application now let's go ahead and take a look at a couple of different things over here inside this function routes we get past the application or app so this particular component or the object app contains many different functions that we can use to reply to the request that the user is doing so if i say app dot get so this is going to be the one that is capturing the get request there are two well there are many kind of requests but the most common is get and post so if i say get over here and i don't really supply that any url then it's considered root now i can go say request in and now we need to return something so i'm just going to say this is the root now let's go ahead and run this okay now if i go back over here and i go to hello url what do you think is going to happen it's not found because if you look at the code over here there is no route for hello there is one route which is right over here which is root route this is called the route route because you can see that i'm not putting a slash or whatever anything it's just going to like google.com cnn.com all right so this means if i just go to this route we should get this is the route okay so it looks like it's working because i do get a reply from the server which is simply saying this is the route which is pretty cool now what happens if i want to go to a particular route let's say movies well app let's just stop the server now dot get movies and what do we want to return from over here well i'm just to return movies that's it that's pretty much it just a string called movies so now if i go to a route which is my localhost 8080 which is 127.0.0.1.8080 movies i should get movies just the text called movies slash movies and looks like it's working all right that's fine but what about if i wanted to go and get horror movies there we go well in that case you can simply say horror so this means that this particular url well first of all the first one this one is going to match slash movies we already have checked that out what do you think this is going to match movie slash horror let's go ahead and run it and check it out the horror is one of the genres of the movies so now i can go to horror and well it returns me movie so maybe i should have changed the result that we are sending it back also to something so i'm going to change it back to horror movies instead of simply movies and looks like it's working what about if i simply go to movies well then i get movies what if out if i simply go to the root url well i got the root but what about if we are building some sort of a website where there are many different genres i mean one of them is horror i guess the other one can be comedy or kids right so if i have action that's another genre then i should create another route collection and i should probably return action movies and this will work perfectly fine but you can already see a problem right i mean if i have 10 different genres horror action kids fiction fantasy you know drama documentary i mean this will come out to be like eight or ten different routes so instead of creating these different routes what we can do is use something called a route parameter all right so in the route parameter we're going to create a route where we will say that instead of passing horror over here we're going to be passing an actual genre i mean that's not going to be the actual url without with the codes or with the colon over here but this can be substituted this can be like injected this is the route parameter this is dynamic so let's see that how we can add the route parameter into our code which is right there so instead of saying horror i'm just going to go ahead and put genre but with a colon the colon is indicating that this is a route parameter so this particular route is now going to match everything that conforms to the genre meaning something that you can pass in now that something can be anything you can pass in horror and that is perfectly fine so it's going to start matching all of these different routes action kids because if you look at the url that we are developing over here the first part is the movies the movies comes over here which is great it's matching and the second part that comes in the genre so the genre is anything that comes after movies which comes out to be all of these different things this also means that you can simply pass in a b c d and that's also fine because it does match this particular route now one of the question will be well okay that's fine but inside this particular route that you have how do you access the genre how do you access horror or action or kids or whatever the user is passing how do you access that from the url well you can easily access it from the request dot parameters dot get and then you say genre and the reason that you're saying genre over here is that that dynamic variable that can be substituted by the user in the url that is called genre if i would have called this category name then guess what should i have called this over here category name but i think genre is a nice word because that is exactly what we're passing we are going to get that actual genre i'm going to put it into a variable if i'm not able to find the genre then i can probably perform an abort abort and not found if i do find genre then i can go ahead and return genre in the closure i do have to specify that i will be returning a string okay now let's go ahead and run this again and i'm going to go ahead and pass in some genre so i'm going to say kids and you can see that whatever the genre i'm passing is going to get returned what about if i pass in comedy comedy so it looks like we are able to access the genre with the code that we have written so that's pretty cool i think right now one other final thing for this very introductory vapor video is the post request post request is done whenever you want to create something right so if you are trying to create a customer you're trying to create a user you're trying to create your account you know anytime there is something that you want to do to create to post something back to the server that is where the post request comes into play so let's say that we are trying to create a particular movie so a app dot post the route movies that's perfectly fine now you might say hold on a second i thought you already use the route called movies up there well if you look closely you will realize that this particular route on line number 11 that we created is a get route but the route or the that we're creating online number 29 is a post route so they're completely different and they are invoked triggered differently so it's okay if the the actual wording of the route is same you will see that we'll have to invoke it in a very different manner so if you're posting a particular movie if you're trying to create a movie that movie will have a title and let's say that that movie will have description like a body or something so what i'm going to do is i'm going to go ahead and create a new folder i will call it dtos which stand for data transfer objects and i'm going to go ahead and create a new file and i will call it movie dto dot swift movie dto which is content so that's another thing that is added in vapor or that's part of vapor is that just like you can go ahead and make sure that your class is decodable and encodable you always use a codable protocol whenever you're using vapor you can also use content protocol which conor protocol if you look at the definition of it it conforms to codable request decodable response encodable and something called async request decodable and encodable so that's definitely going to help us when we are using async and await and that's why i'm just going to go ahead and confirm to the content which also conforms to codable decodable and all that stuff all right and the first thing will be the title and let's say the second thing will be the body of that particular movie now we're getting a movie but now the question is how are we getting the movie well we're getting the movie in the request so let's go ahead and access the request async throws this is the new stuff that has been added and what are we going to return from this post we're just going to return a post object well not the sorry the post object the movie object or the movie dtu in other words now this request will also contains the actual content so request might be filled out on some sort of iphone application or form or even a web application and when you send the request the request will have a body that will contain the title and the body which is going to map over here title and the body i mean if you want to change it something else you can say name and and the year of the movie or the genre of the movie or whatever it doesn't really matter all right so this means that whenever we are treating a new movie we need to pass in the name of the movie and the year of the movie and now we can go ahead and take the request the content which is the actual body of the movie and decode it to a particular uh model that we have which in this case is movie dto so basically we are going to take a body which consists of the key and the value pairs and we are going to decode it into a type that we already have which in this case is movie dto this can blow up so we're just going to pass it with try and then let's call it movie i'm not sure why i'm calling it post all the time and then go ahead and return the movie so this means that whenever you perform a post request then it's going to extract out the movie dto or map it to the movie dto and going to return the detail back to the client now this is all available in mac os 12 or newer so we need to make a change and that change will be in package.json or package. swift and we are going to change the mac os from version 10 to 15 to something else so let's say v12 and now we can go back let's go ahead and build this application again okay so it's building correctly now the problem is well okay this is a post request how are we going to do a post request unlike the get request which we have been doing just by typing the url that will always give you or trigger the get request make sure the server is running but how do we make sure that the post request is working well for the post request we will have to use some tools any tool now one of those tools is called postman it is available for free so you can always go ahead and download it and that's not obviously the only tool there are a lot of tools like insamia and their thunderclient and all that stuff you can use but this is easy to use also so i'm going to create a request the first thing is it's not a get request i'm trying to do a post request so there we go i also want to get the url so let's go and copy part of the url at least and part of the url is this one slash movies headers well we will be sending json so we have to tell the server that we are interested in set as sending json and for the body i'm just going to select raw which means that i will type json myself now this is the most important part because if you look at the server right over here the server is taking the content which is the body of the request and decoding it to a model called movie dto if you look at the model movie dto that particular model consists of two different properties name and the year so it means that the json that you are decoding into movie dto over here must contains those two fields which is name and the year so if i go over here and write a key called title and whatever it is lord of the rings then this is completely useless because the server is not able to decode this because the model that you're decoding into does not contain a property call title it contains the property called name so you should probably send json with a key name so let's go ahead and call it name and the other one was here and i don't really know the year so i'm just going to say 2001 or something all right let's go ahead and send it and we send the same thing exact back so this is working because that is exactly what we are trying to do we decode it to the model which is movie model and we simply return the same exact movie it is automatically converted to json thanks to being using a content as a conforming to content so it automatically converts it back to json so this is like a very basic setup when you are using vapor and obviously in my course i will go much more detail into how to integrate it with databases and how to create microservices and all that stuff but this will at least get you started with server side swift using vapor and installing vapor so hope you have enjoyed this very basic tutorial thank you hey if you like this video and want to support my channel then check out my courses on udemy i have a lot of different courses on udemy ranging from sif ui mvvm design pattern cif ui cookbook core data async and await swift for intermediate advanced developer mvvm design using surf ui and a lot more and i just got released one of my new courses which is about testament development you can definitely check out the links in the youtube description i will also include a coupon link for the test driven development course that will be valid for a couple of days for three four days and then you can use the referral links all right so make sure you check out these courses you are definitely going to enjoy these courses these are great courses and you will keep on learning thank you so much for your continuous support
Info
Channel: azamsharp
Views: 501
Rating: undefined out of 5
Keywords: vapor ios, ios development, server side swift vapor, server side swift, vapor, swift
Id: h_G8-kHn6r8
Channel Id: undefined
Length: 23min 38sec (1418 seconds)
Published: Mon Nov 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.