Server Side Programming In Swift | Backend Development | Swift | Perfect | Xcode

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to my channel i code i am pallav and today we are going to look at server-side programming in swift i have seen many ios developers or mobile developers in general they try to learn back-end development server-side programming writing web services and for this they start learning node.js go spring boot new languages new frameworks and while doing so they struggle a lot and there's no point of learning something which can be already done by a language that you are already comfortable with and there are a lot of benefits of using something that you already know let's say you are a freelancer now you don't need a back-end guy or even if you are not but you want to implement some cool idea that requires a back-end you don't need someone who knows back-end you can build it on your own so today in this video we will see that how we can use swift at server side and for doing so we will make a dummy application for flights booking it will have two apis one for fetching the list of the flights and one for getting the detail of a selected flight let's see how it will look like we will be having two apis first for fetching the list of flights and this is how it will look like so this is how we'll get the data for showing the list of flights and through this data we can render a screen something like this so this list is being rendered by this data and second will be for fetching the details of a specific flight so we'll be passing the code of the flight here and this is how the data will get so the baggage information the duration of the flight whether the cancellation is available or not and everything else the details will get from this api and with this we can render a screen something like this and apart from building these two apis we'll also see that how can we render a webpage at server side so rendering a web page at server side means the html is being prepared at the server side so server is making calls to the database fetching the data applying the business logic doing the heavy computations and finally after doing all that server prepares html and returns it back to the client that is our browser and browser displays it so for that we are having this web page this html page which will be rendering at server side or should i say that the html for this page will be returned from our server we'll see that how can we do that so this is all what we will be doing in this video let's see that how we'll do it we will use perfect for all the heavy lifting so for example creation of the http server routing all these jobs will be done by perfect perfect is a toolkit that is written in swift and using perfect we can create the http servers we can handle the routing we can connect to the databases like maria mysql we can extend our servers to the messaging queues like kafka zookeeper mosquito etc we can even use perfect for deploying our application directly to the aws ec2 instance using the perfect assistant but in this video we will use perfect only for two things that is creation of the http server and handling the routes but if you want to know more about the perfect this is the official site of perfect you can go there and you can see that what all perfect is capable of and how can you use it you can also look at the documentation and they guys have documented their apis in a very understandable manner so this is how it looks like now let's get started so the first thing that we need for using swift for server side programming is swift tool chain so if you are an ios developer or you are having xcode installed in your machine then swift tool chain is automatically installed but if you are not then you need to install your tool chain and for that you can you can visit this page uh of getting started and it will tell you that how can you install the swift tool chain from swift.org so i'm assuming that you are having xcode or you are having the swift tools installed so let's start the work the first thing that we need is a directory for our project so go to terminal and create a directory navigate to this directory now we'll use swift package manager for initializing the project and at this point in time we are having our project initialized from swift package manager now the next thing that we need to do is add perfect as the dependency but before doing that let's create the xcode project because we'll be using xcode as our id and we'll be doing everything else in the xcode only so this will create the export project for you and now we can open our project index code so here we have a file that is package.swift and in the dependencies array of this package description we will put our dependency for the perfect modules so for that i'll put the github apple link of the perfect modules here in this specific format and you do not worry about these code or or these lines or whatever i'm doing i'll host the entire project on the github you can clone it from there also i'll put these these specific stuff in the description as well so you can copy it from there now that you have mentioned our dependency on perfect in our package description go to the terminal and hit the command swift build this will face the dependencies from the get we see that the perfect modules have been fetched from git but we are getting a warning here that is perfect http server is not used by any target and that is because we have not added our dependency or we have not added perfect to any target so for that let's go back to the xcode and here in the executable target dependencies let's mention perfect now once again we'll run swift build and we'll create our xcode project once again i'll run the command for generating the xcode project once again so that all these dependencies are linked properly and now the xcode is saying that the project has been changed outside the export so whether or not we want to accept the xcode version or the disk question and i'll go for use the version of the disk because i know that i have updated the project from the terminal so now that we have the perfect dependencies installed we have added it to the target let's go to the main.swift where we have this hello world and before giving it a run make sure that you select the flight booking app package here and the device should be your mac so i'll select my mac there is a version here and now i will try to run it i see hello world in the console so it means that it ran successfully and nothing is breaking now let's start our work and instead of this print hello world let's import our perfect dependencies here so perfect lib we'll need perfect http and we need import perfect http server the first thing that we want to do is we want to create our http server so for that let's assign a port to the server and let's start it let's try running it and in the logs we see that starting http server on 0.0.0 colon 8080 8080 is the port that we have assigned to our server so our server will be listing at the port 8080 and this means that we have created our http server successfully now let's add some routes to it and routes are basically the uris which the client will be using for requesting something but before that let's create a directory structure because that is the ocd that i have so i'll create three directories one will be for data where the dummy data the dump will be there the second will be for the controllers the controllers will have all the business logic we are not having much for this dummy application but in the actual application your controllers will have your business logic and in the routes directory you will have your routers for this dummy application we will be having only one router but in an actual project you can have several routers for routing to v1 v2 different versions of your application let's create the directories now in the controllers let's create two files one for the flight listing and one for the flights detail these will be the swift files and make sure you select your application here as the target and remove this open ssl otherwise it will create the cyclic references and will throw the errors let's create one more file for the details i'm creating one file here in the data directory as dummy data and first of all in this dummy data i'll just dump some data here and either you can go for creating your own structure your own json or your own dictionary structure or you can copy this data directly from the github record that i'll mentioning in the description so this is the dummy data that we'll be using for the flights list and this is for the flights dts data if you want you can use the same data for both the routes that is listing and the detail but here i want to written more data for the details route so that is why i am having this second set of data that is in this method get flight details data now that we have our dummy data ready let's go to the controllers and let's set the logic for returning the data so here i have added a class that is flightlesscontroller and added a method that is handleflightlistrequest it will receive two parameters one will be for the http request and the other one will be for the http response we'll use the response object for returning the data so we'll call the method set body on the response object and as the json will pass the flight list data so this is the method that we created in our dummy data this is the get flight list so this will return this data and after setting it to the body i'm setting the headers as the application json and completing it with ok status but if something goes wrong then in the catch block i am treating it as internal server error and returning it on the completed block as the internal server error and all these things i haven't created any enum or anything else for this internal server error or okay all these things are available out of the box from the perfect package so instead of internal server error if you hit control space here you will see that you have all the options for bad request bad gateway accepted conflict and you are free to choose the option as per your logic the way you want to handle the request so for now i am handling it as internal server error in the catch block and in the success block if everything goes well i am returning it as status ok that will be 200 now let's add a similar method for fetching the flight details so here in the flights detail controller similar to the flight list controller i created a class that is flights detail controller and added a method that is handled flights detail request similar to that method here also i'm getting two object for request and response but here i'm doing something different and that is i'm fetching the id from the request so you can assume that the request will be something like this slash details that will be a route and that will be followed by some id of the flight that is one zero zero one one zero zero two or any other number and i want to fetch this particular number this particular id for fetching the details from the details data and for that i'm using the url variables property of the request object and querying for my key that is id in this case so if i get the id i'll use the flights detailed data i'll filter it for that particular id and will return the response otherwise i am treating it as a bad request that the id is missing and i am returning it as the bed request rest looks the same as the flightless method so in the similar way i am setting the body here with the flight details as the json the headers as application json and completing it with the ok status if something goes wrong in this entire do part then it is some internal server error something went wrong so we are done with our controller logics we are done with creating the server the only part missing is connecting the routes and for that let's go in this routes directory and create a file let's create a router i'll name it router in this router i'll create a method that is setup router which will return the routes object so for that i need to import the perfect package first and now i can return the route object from here now let's first create the routes object and add some routes to it and here i'll use this one where i can get the method the http method for now i want to put it as get you can change it to post as well the uri will be say slash slides for the listing and in the request handler will have a closure having request and response and what i'll do is i'll call the get flightless data and will pass the request and response object to it flight list controller handle flight list request request and response let's add one more route for our details method this will also be get and here with details i'll mention id in the braces because this will be a variable that will be fetching from the request and flights detail controller and in flights detail request response and now let's return the routes so now our router is also set up the last piece of code that is remaining is adding these routes to our server so in the main dot swift server dot and routes and here i'll call my method setup router everything seems fine let's try running our server and here we'll get an error i'll explain it to you in a minute we got the error that address already in use and this is because the port on which we want to start our server is already busy from our previous run so in case you get this error what we can do is we can kill the process that is using that port and for that go to the terminal use the list of files commands so lsof and mention rtc port that is 8080 in our case we'll get the pid here and then just kill that particular process using its pid that is 31667 in this case and now if i see there's no process running on port 8080 so now if you'll try running our application it will cool so everything is in place server has started let's test our routes for that i'll be using postman as the client and here instead of 3000 i'll use eight zero eight zero slash flight that is our route and we are getting the response let's try changing it or maybe let's first try the second route so it goes like eight zero eight zero slash details slash and let's not mention the id because we have handled the case where the id will not be passed and we'll be returning the bad request that id is missing and that is what we got here bed request 400 now let's pass some id and the ids we are getting in our first call that is one zero zero two one zero zero one maybe so let's go for this mr one one zero zero two and we got the data i'm using the same web services in the ios application that i showed earlier so i'll be pushing the code for that too on the github repo you can clone it from there it is pretty much straightforward stuff over there i have used swift ui for building the ui combine and url session url session for fetching the data and combined for the interaction between the view model and the views it's pretty much straightforward let's not buy time on it and now let's see that how can we render a webpage that i talked about so let's go for it in the router let's create one more route on which we will be returning the html routes dot add the method will be get and let's leave the uri as slash this means that we'll get the response for this particular route on localhost colon 8080 that's all and in the handler we will get request and ipad request and response and instead of adding another controller let's return the html data from the dummy data itself so in the dummy data i need the html data over here so that's the html code for our web page let's call this method here from our router and here instead of returning it as a json i'll be returning it as a string because that's what the client our browser is expecting so here on the response i'll be calling the method set body and i'll pass the string that is get index data let's call the completed let's run it again and test our web page here in the browser let's try localhost 8080 is our port and this is the webpage this is the html data that we have written from our server so this way we can render the webpages at the server side and by now you would have got an idea that what we can do using swift at server side almost everything is possible i haven't encountered anything yet that is not possible using swift at the server side perfect gives a lot of features and we can use them out of the box so here you can see that if i go for perfect assistant you will see that we can use it for deploying our application directly to the aws ec2 instance we can use it for testing our application on the fly we can extend it to the messaging queues and the more you will deep dive in more you will get to know so so that's pretty much for this video let's break the stereotype that swift can be only used for building ios applications or can be used only for apple platform swift.org is trying hard to make it platform independent and let's start using it for the other stuff like building the back end server side programming let's write better code together happy coding and stay safe
Info
Channel: iCode
Views: 2,514
Rating: undefined out of 5
Keywords: Swift, Server side programming, backend, backend development, server side, iOS, xcode, iCode, perfect, postman, server side rendering, web page, api, webservice, server, client, pallav
Id: -Cq-rnZd4X0
Channel Id: undefined
Length: 21min 26sec (1286 seconds)
Published: Sat Jul 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.