Learn Ktor | What is Ktor? | Setting Up | Getting Started

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey folks welcome back to my YouTube channel and in this video we are going to learn catar so what's cater it's okay to it as you can see it's uh it's basically to create a synchronous client and server application so you can build anything from microservices to any multi-platform HTTP Client app in a simple way and it's open source free and a lot of fun to do okay so the next question might be why someone will use keto right there are various ways of creating for example apis uh your server your client right so why cater so I think first thing which I would say is that it's built by chat brains because chat prints is very popular in the Tech Community because they have built a lot of Ides if you know like IntelliJ Android Studio you know and Rider um PHP storm all right Pi charm all of these and also they are the creator of kotlin language so I think that those are good enough reasons right to use a cater second it's lightweight and it's flexible so um that's another thing which you know everyone wants it's probably you can say it's very much similar to flask when it comes to use it as a like a simple application which you can extend using plugins and you can even extend using your own plugin and the third thing is that it's written using kotlin so it's cater is built using cartilene and Co routines so if you don't know what is co routines then you'll we will cover it later okay so um yeah you can use all the advantages of using kotlin if you are already familiar with kotlin then it will be fun to use right so without any further Ado let's start so what you have to do you have to install IntelliJ IDEA for this and this is one of the like requirement right now okay because it will help you basically it's not like you cannot do K2 development without IntelliJ technically you can do it but it would be better if you use IntelliJ IDEA right so well what we will do we will go to new project it will be as simple as that and then you see here we have a lot of options from here we have quarterly in multi-platform cater compose multi-platform and all of those things right even we have Dart flutter Android and all those things so we will select cater now you have to here let's say cator and we say tutorial okay the name of the project then location it's under this is my location you can change it you can check if you want to create a git repository or not I would suggest you should definitely check it and then you have to choose the build system either you can use Gradle kotlin you can use groovy or Maven I would prefer to go with the kotlin one and then you select the website or the domain which you want to determine your package name uh right now it's example.com which is fine or I can change this for example code.dev so this will be my artifact looks like Dev dot code dot create or tutorial the name of your application then you will give the keto version right now it's 2.2.1 by default engine you can use native which is by default you can use JT and maybe there will be more yeah there are there is Tomcat CEO JT and 80. we are going to use Neti itself and then there is configuration is how you want to configurate it maybe you can use it a yaml file hook on file or code so right now we are using Code but in the future we will definitely uh I'll show you how you can use hook on file and there's a checkbox which says do you want to add sample code so because of um it's it's a first start so just because we want to make keep it simple so let's add sample code so just click this check mark and then you will click this next button which is fair enough create now there are a lot of plugins which you can add so these plugins can range from authentication to serialization uh compression all of those things right routing all of these things are part of plugins when it comes to ktor so you can add these plugins for example as you can see I have authentication plugin over here and lot of authentication routing plugins you know HTTP plugins like compression caching headers condition content negotiation uh codes a lot of things are there right so you can use all of the serialization is also they are kotlin serialization Json Jackson web sockets all of these are options so for now let's do let's go with routing so I'll search routing and I'll find this one allowed to Define structure route which is important for us so I'll just say add and then you will create the project so we have just added one um routing plugin that's it um initially it will take some time to download all the dependencies so if you're doing it for the first time it can take some time so you have to be aware that you know that's gonna take some time so um yeah and just grab some coffee or do something so as you can see my build is successful for now and let's also go through the entire project structure so if I go here you can see we have a source folder a Gradle folder so if you have done Android app development then you might be familiar with Gradle and all the things um for now what you what you can say is that Gradle is being used to handle all the dependencies and all right so that's the purpose of Gradle and then we have a source folder and external libraries you can ignore these are all which will be downloaded when you will be creating a project for the first time and then okay so let's open this Source folder then you will find two folders one is main another is test test is for writing test cases main is where you will write your main code so again it has resources which has logback.xml and then we have kotlin right which is again having the package folder and then the plugins and then application.kt so the focus should be here application.kt because here is where you will have all your code and that's that's how the application starts from right so here are two important things if we go into details of it first is this function main also I would like to expand this import so as you can see we are importing server.application server.engine Neti and then plugins okay so there are two important calls here one is embedded server and another is configure routing so embedded server is basically nothing but this is how you will you know configure all your all your server parameters in code and that will run your application so if you remember we have chosed the engine as Neti so that's what is here over here right and then the port which is 8080 host is 0.0.0.0 and module is application module right we will go in details of all of these Stone way but right now the important part is that this is how you start your server uh run your application basically and then we have a DOT start where we say rate is equal to true you can also make it false if you don't want to wait but yeah let's go with True by default and then we have a function which has which is having the name module um and then what we are doing here we are just saying configure routing okay and this is another function which is there in the plugins part so um it's an extension function obviously and um it it has all the routing specific things so if we go here in the plugins we can see routing.kt and inside here you can find this function which is configure routing right so what's happening in configure routing that's like if we see here we have routing and then we have get function okay so um so this is a routing block and routing block contains this get and if you see we we get this kind of informations here like what is this and all all of those things so get is what get is doing so it's basically this is how you receive get requests so that's why you specified get if you don't know about get request post request and all of these things you can refer to my um like there is a cookbook which you can find on code.dev where you'll find all this information I'll probably paste the link in the description so you can go and read about networking and all so um and then so basically the path here is this slash which is like the default path I would say or the um the base level path I would say and then we are responding with a plain text so we say call Dot respond text and the text is hello world that's it so this is all and everything is being done really like you haven't written even a single line of code because we asked it it asked it to write this sample code right so that's what we're doing so now in the function main here we have this run button we will run it so run application KT it will take some time building the catalog kit or tutorial and you can see responding at 0 0 0 and 8080 so if we go here and if I'll show you how this works so I have the one second let me just bring that as well here okay one second so what I'll do I'll just keep I'll just get the hello world from here so you can see Hello World right and it is running on zero zero zero and colon 80. so that's good right so that means our app is running just fine and now uh what else we can do so the code is running fine what you can do is basically maybe if you want you can run the test case as well so um to run the test case what we can do um we can go to this test folder and we can find application test over here and here um you can see we have application test class then we have this annotation as test and we have a function test root which has this test application block inside that we just say configure routing as we are saying in our main application as well and then we say client dot get this dot apply and if HTTP status code is okay which is the status then it's fine and the body should also say hello world so this is the expected output if I run this by this then we will see if our test cases are passing yes so you can see test passed one which means everything is working just fine if I change it a little bit for example I remove this exclamation mark from here and if I try to run it now you will see that the test will fail and you can see test failed so that means this thing is working fine so rather than running it in the browser we can always run the test case and that's how you create your first application using ktor I know this is very simple um this is the basic of things like there are a lot of things which are happening which you might want to know but for that you will have to watch my next video right so if you enjoyed it give it a thumbs up subscribe to the channel if you're not a subscriber already and thanks for watching I'll see you in the next one bye bye take care
Info
Channel: Codepur
Views: 9,569
Rating: undefined out of 5
Keywords: codepur, ktor, learn ktor, ktor tutorial, ktor android, ktor kotlin, ktor course, kotlin, what is ktor, ktor server, ktor server tutorial, ktor server example
Id: lZWILBcHy3Q
Channel Id: undefined
Length: 11min 47sec (707 seconds)
Published: Wed Dec 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.