{"id":9175,"date":"2022-02-21T23:29:53","date_gmt":"2022-02-21T23:29:53","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9175"},"modified":"2022-02-21T23:29:53","modified_gmt":"2022-02-21T23:29:53","slug":"getting-started-with-vapor","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/getting-started-with-vapor\/","title":{"rendered":"Getting started with Vapor"},"content":{"rendered":"<p><a href=\"https:\/\/vapor.codes\/\" rel=\"noopener\" target=\"_blank\">Vapor<\/a> is a web application framework written in Swift.<\/p>\n<p><strong>Installing on Linux<\/strong><\/p>\n<p>As I&#8217;m more likely to be using a Linux server for my web application, that&#8217;s what we&#8217;ll use for this post. So following the <a href=\"https:\/\/docs.vapor.codes\/4.0\/install\/linux\/\" rel=\"noopener\" target=\"_blank\">Install on Linux<\/a> instructione, I went the route of installing the toolbox for the toolbox repo (instructions recreated below)<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ngit clone https:\/\/github.com\/vapor\/toolbox.git\r\ncd toolbox\r\ngit checkout &lt;desired version&gt;\r\nmake install\r\n<\/pre>\n<p>This will build and install <em>vapor<\/em>. <\/p>\n<p><strong>Generating an application<\/strong><\/p>\n<p>Vapor will generate an application for us using <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nvapor new hello-vapor -n\r\n<\/pre>\n<p>(obviously replace hello-vapor with your application name).<\/p>\n<p>As we&#8217;re using Linux, we&#8217;ll <em>cd<\/em> into the <em>hello-vapor<\/em> application folder and then run<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nswift run\r\n<\/pre>\n<p>This will run, by default on localhost:8080 which is not great if you want to access remotely, so let&#8217;s instead start the application using<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nswift run Run --hostname 0.0.0.0 --port 8080\r\n<\/pre>\n<p>Now if you connect to the server&#8217;s ip address, port 8080 you should see the default return &#8220;It works!&#8221; if you use http:\/\/localhost:8080\/hello you&#8217;ll see &#8220;Hello, world!&#8221; returned.<\/p>\n<p>If you take a look at the source generated by <em>Vapor<\/em> and check the folder Sources\/App\/routes.swift you&#8217;ll see code like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nimport Vapor\r\n\r\nfunc routes(_ app: Application) throws {\r\n    app.get { req in\r\n        return &quot;It works!&quot;\r\n    }\r\n\r\n    app.get(&quot;hello&quot;) { req -&gt; String in\r\n        return &quot;Hello, world!&quot;\r\n    }\r\n}\r\n<\/pre>\n<p>As you can see this is routing the \/hello GET method to return &#8220;Hello, world!&#8221;.<\/p>\n<p><strong>Using HTTP methods<\/strong><\/p>\n<p>As you can see from the previous code, <em>app.get<\/em> denotes the method is a GET method, so let&#8217;s simply write an example of each of the HTTP methods, so change your routes.swift file to add the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\napp.get(&quot;method&quot;) { req -&gt; String in\r\n   return &quot;\\(req)&quot;\r\n}\r\n\r\napp.post(&quot;method&quot;) { req -&gt; String in\r\n   return &quot;\\(req)&quot;\r\n}\r\n\r\napp.put(&quot;method&quot;) { req -&gt; String in\r\n   return &quot;\\(req)&quot;\r\n}\r\n\r\napp.patch(&quot;method&quot;) { req -&gt; String in\r\n   return &quot;\\(req)&quot;\r\n}\r\n\r\napp.delete(&quot;method&quot;) { req -&gt; String in\r\n   return &quot;\\(req)&quot;\r\n}\r\n<\/pre>\n<p>This doesn&#8217;t cover all HTTP methods, but we can use the alternate syntax to the above, which looks like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\napp.on(.GET, &quot;method&quot;) { req -&gt; String in\r\n   return &quot;\\(req)&quot;\r\n}\r\n<\/pre>\n<p>and simply replace .GET with each of the HTTP methods, such as .CONNECT, .OPTIONS, etc.<\/p>\n<p>Now let&#8217;s tests these &#8211; if we simply use CURL to execute each method, i.e.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ncurl -X GET localhost:8080\/method\r\ncurl -X POST localhost:8080\/method\r\ncurl -X PUT localhost:8080\/method\r\ncurl -X PATCH localhost:8080\/method\r\ncurl -X DELETE localhost:8080\/method\r\n<\/pre>\n<p><strong>Parameters and Query Parameters<\/strong><\/p>\n<p>We&#8217;re also likely to need to handle parameters, so for example \/method\/aparam<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\napp.get(&quot;method&quot;, &quot;:param&quot;) { req -&gt; String in\r\n   let param = req.parameters.get(&quot;param&quot;)!        \r\n   return &quot;\\(param) --&gt; \\(req)&quot;\r\n}\r\n<\/pre>\n<p>The : prefixing the param token is indicates the parameter is dynamic and hence we need to use <em>req.parameters.get(&#8220;param&#8221;)!<\/em> to get the value from the <em>param<\/em>.<\/p>\n<p>Finally for this post, what about if we want to pass query parameters, for example http:\/\/192.168.0.88:8080\/method?firstName=Scooby&#038;lastName=Doo<\/p>\n<p>We can declare a struct to handle our expected query parameters like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nstruct Person: Content {\r\n    var firstName: String?\r\n    var lastName: String?\r\n}\r\n<\/pre>\n<p>and now change our GET method to decode the query parameters into this struct, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\napp.get(&quot;method&quot;) { req -&gt; String in\r\n   let person = try req.query.decode(Person.self)\r\n   return &quot;Firstname: \\(person.firstName!), Lastname \\(person.lastName!)&quot;\r\n}\r\n<\/pre>\n<p>There&#8217;s a whole lot more functionality, as you&#8217;d expect, including streaming, validation etc. take a look at the <a href=\"https:\/\/docs.vapor.codes\/4.0\/\" rel=\"noopener\" target=\"_blank\">Vapor Docs<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vapor is a web application framework written in Swift. Installing on Linux As I&#8217;m more likely to be using a Linux server for my web application, that&#8217;s what we&#8217;ll use for this post. So following the Install on Linux instructione, I went the route of installing the toolbox for the toolbox repo (instructions recreated below) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286,324],"tags":[],"class_list":["post-9175","post","type-post","status-publish","format-standard","hentry","category-swift","category-vapor"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9175"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9175\/revisions"}],"predecessor-version":[{"id":9181,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9175\/revisions\/9181"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}