<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Server Side Swift - Medium]]></title>
        <description><![CDATA[Server Side Swift frameworks, tools and best practices - Medium]]></description>
        <link>https://medium.com/server-side-swift?source=rss----6c5cbc488e43---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Server Side Swift - Medium</title>
            <link>https://medium.com/server-side-swift?source=rss----6c5cbc488e43---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 09:41:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/server-side-swift" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[An opinionated guide to configuring and launching an API Server — Perfect]]></title>
            <link>https://medium.com/server-side-swift/an-opinionated-guide-to-configuring-and-launching-an-api-server-perfect-ae92e051f73d?source=rss----6c5cbc488e43---4</link>
            <guid isPermaLink="false">https://medium.com/p/ae92e051f73d</guid>
            <category><![CDATA[perfect]]></category>
            <category><![CDATA[cors]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[server-side-swift]]></category>
            <category><![CDATA[csrf]]></category>
            <dc:creator><![CDATA[Fatih Nayebi]]></dc:creator>
            <pubDate>Sun, 08 Apr 2018 13:32:45 GMT</pubDate>
            <atom:updated>2018-04-09T00:37:56.676Z</atom:updated>
            <content:encoded><![CDATA[<h3>An opinionated guide to configuring and launching an API Server — Perfect</h3><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*B4x_bB89XZ3keVD1M1525g.png" /></figure><p>There are many ways to configure and launch a <em>Perfect</em> Server. This guide presents opinionated best practices to configure a Perfect server with <em>sessions</em>, <em>routes</em>, <em>filters</em>, and <em>logging</em>.</p><h4>Session</h4><p>If we plan to have sessions on our <em>Server Side Swift</em> we need to configure it before we launch our server. Here is my example configuration that can be customized to your needs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c4422f9eeaf6e366d1792353b5f2f24/href">https://medium.com/media/8c4422f9eeaf6e366d1792353b5f2f24/href</a></iframe><p>Also we need to call let sessionDriver = SessionMySQLDriver() to configure the session filters and using <em>purgeInterval</em> to purge stalled sessions.</p><h4>CORS</h4><p>To make your APIs work with Single Page Apps (react, vue.js or Angular) we need to configure <em>Cross-Origin Reference Sharing (CORS)</em>. You can read about <em>CORS</em> here:</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS">Cross-Origin Resource Sharing (CORS) - HTTP | MDN</a></p><p>The following is a working example of <em>CORS</em> configuration. We can call CORS.configure() in the main before launching the server.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04ab2efca2a70bda8a54028ea260b12a/href">https://medium.com/media/04ab2efca2a70bda8a54028ea260b12a/href</a></iframe><blockquote>Make sure that you define all the custom http headers and acceptable host names otherwise web clients will not be accepted to consume your apis! 👈</blockquote><h4>CSRF</h4><p><em>Cross-Site Request Forgery (CSRF)</em> is an attack that forces an end user to execute unwanted actions on a web application in which they’re currently authenticated. <em>CSRF</em> attacks specifically target state-changing requests, not theft of data, since the attacker has no way to see the response to the forged request.</p><p>You can read about CSRF here:</p><p><a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)">Cross-Site Request Forgery (CSRF)</a></p><p>To avoid <em>CSRF</em> attacks we need to configure our server. The following is an example that can be customized as you may need. We can call CSRF.configure() in the main before launching the server.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af486eb6ca5678c482ea5a718a1ba264/href">https://medium.com/media/af486eb6ca5678c482ea5a718a1ba264/href</a></iframe><h4>Schema initialization</h4><p>As we need to have sessions and database connections we need to configure them before we launch our server. We can place all configurations for database, SMTP, session,… in a JSON and use it to configure our server. We can have configurations for different environments. The following example shows how we can configure our servers for macOS and Linux differently:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ce0f4cc01bd472b78060a6be3a234f4/href">https://medium.com/media/5ce0f4cc01bd472b78060a6be3a234f4/href</a></iframe><p>A typical configuration file looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ffd8aab4e0e630c7d98f78e93ee559d/href">https://medium.com/media/5ffd8aab4e0e630c7d98f78e93ee559d/href</a></iframe><p>Then we can set <em>DB</em>, <em>session</em> and <em>SMPT</em> configurations like the following:</p><p>let opts = initSchema(fname)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1cf540f7f512eeeb3224b18ee53143c4/href">https://medium.com/media/1cf540f7f512eeeb3224b18ee53143c4/href</a></iframe><p>This also return baseURL, httPort,… that we will be using when we launch our server.</p><h4>Logging</h4><p>We typically need two different types of logs:</p><ul><li><strong>Request logs</strong> that are brief and provide info about all the requests coming to our server. This can be done using a <em>filter</em> that we will be talking about in filters section.</li><li><strong>Logging</strong> for debug, error, and info cases.</li></ul><p>To enable both types of logging we need the following:</p><pre>RequestLogFile.location = opts[&quot;requestLoggingFileLocation&quot;] as? String ?? &quot;&quot;<br>LogFile.location = opts[&quot;logFileLocation&quot;] as? String ?? &quot;&quot;</pre><h4>Routes</h4><p>There are many ways to define routes that are covered here:</p><p><a href="https://medium.com/server-side-swift/https-medium-com-thefatih-typed-routes-with-codables-in-server-side-swift-perfect-ad6708eb6650">Typed routes with Codables in Server Side Swift — Perfect</a></p><h4>Filters</h4><p>There are two types of filters:</p><ul><li><strong>Request</strong> — These can be used to check for session, authorization, or to use the scratchPad to carry over data between request and response as well as initiating a specific task or creating a pipeline.</li><li><strong>Response</strong> — These types of filters can be used to filter or modify the responses (e.g. modifying headers, content compression,…) as well as initiating a specific task (e.g. logging responses of APIs)</li></ul><h4>Request filters</h4><p>The following presents an example of request filters:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ab5640dac8544add845fff06a8eac4e/href">https://medium.com/media/4ab5640dac8544add845fff06a8eac4e/href</a></iframe><h4>Response filters</h4><p>The following presents an example of response filters:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/173665b90fdbee0f5fbe9c7b734af0b2/href">https://medium.com/media/173665b90fdbee0f5fbe9c7b734af0b2/href</a></iframe><h4>Launching server</h4><p>Finally, we can launch our server as the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e32bb01d63381363c743a96cedbe0481/href">https://medium.com/media/e32bb01d63381363c743a96cedbe0481/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae92e051f73d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/server-side-swift/an-opinionated-guide-to-configuring-and-launching-an-api-server-perfect-ae92e051f73d">An opinionated guide to configuring and launching an API Server — Perfect</a> was originally published in <a href="https://medium.com/server-side-swift">Server Side Swift</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Typed routes with Codables in Server Side Swift — Perfect]]></title>
            <link>https://medium.com/server-side-swift/https-medium-com-thefatih-typed-routes-with-codables-in-server-side-swift-perfect-ad6708eb6650?source=rss----6c5cbc488e43---4</link>
            <guid isPermaLink="false">https://medium.com/p/ad6708eb6650</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[perfect]]></category>
            <category><![CDATA[server-side-swift]]></category>
            <dc:creator><![CDATA[Fatih Nayebi]]></dc:creator>
            <pubDate>Sun, 08 Apr 2018 12:57:56 GMT</pubDate>
            <atom:updated>2018-06-29T15:15:17.193Z</atom:updated>
            <content:encoded><![CDATA[<h3>Typed routes with Codables in Server Side Swift — Perfect</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FDAl9zTvSl9Cx6_l1zNkpA.jpeg" /></figure><p>Introduction of Codables in Swift 4 made our life way easier 😊, We no longer need to use parsing and mapping libraries or write a verbose code to extract / generate data structures such as JSONs.</p><p>Perfect framework adapted the Codables so we can create typed routes to easily decode and encode requests and responses in Server Side Swift. Let’s start with creating a function that returns main routes of our server. We will set our server’s routes by simply calling this function when we launch it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f2e570db286227f3d152437ee7ab84a/href">https://medium.com/media/0f2e570db286227f3d152437ee7ab84a/href</a></iframe><p>As we may know and we can see from above, there are many ways to define routes, here I am going to explain four different ways:</p><ul><li>Async routes (Not typed!)</li><li>Sync typed routes</li><li>Sync typed routes with custom HTTPResponse content</li><li>Async typed routes with custom HTTPResponse content and promises</li></ul><p>There are more ways to define routes but examining these categories will give you a pretty good idea on how to mix and match different approaches.</p><blockquote>options route is added to support SPA preflight calls</blockquote><h4>Asynchronous routes (Not typed!)</h4><p>Although these types of routes are flexible and async, they are not type safe! 😬</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb120051a80d91b1ca1ae527ba5d2d33/href">https://medium.com/media/eb120051a80d91b1ca1ae527ba5d2d33/href</a></iframe><p>Route for this handler is defined as:</p><p>routes.add(method: .get, uri: &quot;/api/v1/user/logout&quot;, handler: logout)</p><p>Suppose we needed to extract the data out of the request, we need to extract the <em>postBodyString</em> first then decode it to our mode like the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5c527683726db50129547fca019c0f5/href">https://medium.com/media/e5c527683726db50129547fca019c0f5/href</a></iframe><p>Also, in this case we can return anything as JSON in the body of response and it’s not clear what this API will return as the handler function doesn’t return anything. Using Codables we could make it less verbose and type safe!</p><h4>Synchronous typed route</h4><p>These types of routes are useful when we do not need to call anything asynchronous (e.g. calling other APIs with <em>PerfectCURL</em>) but we are looking for type safety! 😊</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54746649c8a959bea5af3a4703c60736/href">https://medium.com/media/54746649c8a959bea5af3a4703c60736/href</a></iframe><p>Route for this handler is defined as:</p><p>routes.add(TRoute(method: .get, uri: &quot;/healthcheck&quot;, handler: healthCheck))</p><p>Looking at method’s signature we can easily find out the return type that is already Codable and we do not need to generate a JSON out of it!</p><h4>Synchronous typed routes with custom HTTPResponse Content</h4><p>These types of routes are like previous category but they are more flexible in a sense that we can manipulate the response content (e.g. headers, http status,…)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1552558e30b506d1a124cbf36b58e99a/href">https://medium.com/media/1552558e30b506d1a124cbf36b58e99a/href</a></iframe><p>Route for this handler is defined as:</p><p>routes.add(TRoute(method: .get, uri: &quot;/api/v1/user/session&quot;, handler: session))</p><h4>Asynchronous typed routes with custom HTTPResponse Content and Promises</h4><p>Most of the time, APIs that we create are more complicated than just returning a JSON. For instance when we implement a login API we need to check user and password against database or we may call some other APIs asynchronously.</p><p>Meet <em>asynchronous typed routes</em> with <em>HTTPResponse Content and Promises</em>. 🎉These routes are the most flexible, type safe and performant routes 💪.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36bb7307ae5c0a4038ecef94dd1827d4/href">https://medium.com/media/36bb7307ae5c0a4038ecef94dd1827d4/href</a></iframe><p>Route for this handler is defined as:</p><p>routes.add(TRoute&lt;HTTPRequest&gt;(method: .post, uri: &quot;/api/v1/user/login&quot;, handler: login))</p><p>Extracting the model out of request is as simple as using the decode method on <em>req</em> (request). Also, it is async and returns a <em>Promise</em> that wraps our model so we do n’t need to generate a JSON manually! 😁</p><p>In this example, I created some helper methods to simplify the handlers:</p><ul><li>json that actually sets the promise and returns the result to the client</li><li>error that returns the error to the client in cases of errors</li><li>incompleteRequest that will return 400 when we are not able to decode the request to our request object</li></ul><blockquote>IMHO, <em>Async typed routes with HTTPResponse Contents and Promises</em> are the best option as they are flexible, type safe and async so I encourage you to use them!</blockquote><p>Finally, we can set our server’s routes like the following in the main.swift 😊</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e32bb01d63381363c743a96cedbe0481/href">https://medium.com/media/e32bb01d63381363c743a96cedbe0481/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad6708eb6650" width="1" height="1" alt=""><hr><p><a href="https://medium.com/server-side-swift/https-medium-com-thefatih-typed-routes-with-codables-in-server-side-swift-perfect-ad6708eb6650">Typed routes with Codables in Server Side Swift — Perfect</a> was originally published in <a href="https://medium.com/server-side-swift">Server Side Swift</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Server Side Swift Deployment — One Approach]]></title>
            <link>https://medium.com/server-side-swift/server-side-swift-deployment-one-approach-74dd06be5039?source=rss----6c5cbc488e43---4</link>
            <guid isPermaLink="false">https://medium.com/p/74dd06be5039</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[continuous-delivery]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <category><![CDATA[server-side-swift]]></category>
            <dc:creator><![CDATA[Fatih Nayebi]]></dc:creator>
            <pubDate>Tue, 27 Mar 2018 00:11:47 GMT</pubDate>
            <atom:updated>2018-06-29T15:10:37.448Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Server Side Swift Deployment — One Approach</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OpeFwqWFkn2cd-lnXaThzg.png" /></figure><p>There are many ways to deploy <em>Server Side Swift</em> into production. Let’s explore the topic and go through an approach that is simple and easy to maintain 😊.</p><h3>Platform as a Service (PaaS)</h3><p>Cloud solutions provide <em>platform as a service</em> so we can use them to deploy our applications without dealing with infrastructures.</p><p>Examples of these platforms are</p><h4>Google Cloud Platform (GCP) App Engine</h4><p>GCP App Engine custom flexible environment is suitable to deploy <em>Server Side Swift</em> into the cloud.</p><p><a href="https://cloud.google.com/appengine/">App Engine Application Platform | Google Cloud</a></p><h4>Heroku</h4><p>Heroku build packs can be used to deploy Serve Side Swift into the cloud easily.</p><h4>Perfect Heroku Buildpack</h4><p><a href="https://github.com/PerfectlySoft/Perfect-Heroku-Buildpack">PerfectlySoft/Perfect-Heroku-Buildpack</a></p><h4>Vapor Heroku Buildpack</h4><p><a href="https://github.com/vapor-community/heroku-buildpack">vapor-community/heroku-buildpack</a></p><h4>Amazon Web Services (AWS) Elastic Beanstalk</h4><p><a href="https://aws.amazon.com/elasticbeanstalk/">Web App Deployment - AWS Elastic Beanstalk - AWS</a></p><p>My preference would be the GCP App Engine because of its simplicity and pricing model.</p><h3>Infrastructure as a Service (IaaS)</h3><p>If you need more control over your environment and if you need to manage your own <em>Virtual Machine (VM)</em> then <em>IaaS</em> platforms are the solution.</p><p>Examples of these platforms are:</p><h4>AWS Elastic Compute Cloud (EC2)</h4><p><a href="https://aws.amazon.com/ec2/">Amazon EC2 - Cloud Compute Capacity - AWS</a></p><h4>GCP Compute Engine</h4><p><a href="https://cloud.google.com/compute/">Compute Engine: Virtual Machines (VMs) | Google Cloud</a></p><h3>☁️ Private Cloud</h3><p>If you find the cloud platforms too expensive or you have other legislative requirements that stop you from using cloud platforms then you would need to either host your own server or use a hosting solution and manage your own VMs.</p><blockquote>What if you could not decide which approach you should follow or you know that you may need to change the cloud platform.</blockquote><h3>Portability of Server Side Swift ❓💭</h3><p>Is there any solution to deploy Server Side Swift that can minimize the migration and porting costs from one platform to another?</p><h3>📌 Solution</h3><p>The answer as you already guessed is the <strong><em>Docker + nginx</em></strong> 💪.</p><p>Running the <em>Server Side Swift</em> in docker containers and using <em>nginx</em> as the reverse proxy is something that would work in most of platforms. Let’s see how we can achieve that.</p><h4>Create docker images</h4><p>The easiest way to do it, to use Perfect Assistant (PA). PA will create 2 docker files:</p><ul><li>PADockerfile_build to build a docker image, containing all dependencies</li><li>PADockerfile_deploy to use the previously created built docker image and deploy the project.</li></ul><p>So I use <strong>PA</strong> to build and deploy my project for Linux and create my own <em>Dockerfiles</em> from the ones created by <em>PA</em>. Unfortunately, <em>PA</em> doesn’t provide a <em>CLI</em> at this time so I need to do it manually. The rest of the process is automated using a <em>bash</em> script I share below:</p><pre>#!/bin/bash<br>echo &quot;Update project to version &quot;$1<br>docker build -f Dockerfile-build -t project-build .<br>docker build -f Dockerfile-deploy -t project-deploy .<br>docker tag project-deploy:latest your-docker-registry/project:$1<br>docker push your-docker-registry/project:$1<br>echo &quot;Done updating project to version &quot;$1</pre><p>As you ca see in the script above, I use Dockerfile-build and Dockerfile-Deploy to create the image and push it to a <em>Private Docker Registry</em> or <em>Cloud Container Registry</em> (e.g. <em>GCP Container Registry</em>) so I can pull and run it anytime I want. You will find these files in upcoming subsections.</p><p>To run it, I need to execute the following command in a terminal:</p><p>sh project.sh 1.0.0</p><h4>🚧 Dockerfile for building</h4><p>Let’s check our Dockerfile-build:</p><pre>FROM perfectlysoft/perfectassistant:4.1<br># Perfect-mysqlclient-Linux.git-1864881310269612043/PADockerfile<br>RUN apt-get -y update &amp;&amp; apt-get install -y libmysqlclient-dev<br>RUN sed -i -e &#39;s/-fabi-version=2 -fno-omit-frame-pointer//g&#39; /usr/lib/x86_64-linux-gnu/pkgconfig/mysqlclient.pc<br># Perfect-COpenSSL-Linux.git-3018179716261764243/PADockerfile<br>RUN apt-get -y update &amp;&amp; apt-get install -y libssl-dev<br># Perfect-libcurl.git--6009391049797615682/PADockerfile<br>RUN apt-get -y update &amp;&amp; apt-get install -y libcurl4-openssl-dev<br># Perfect-LinuxBridge.git-3155094712639753971/PADockerfile<br>RUN apt-get -y update &amp;&amp; apt-get install -y uuid-dev<br>RUN rm -rf /var/lib/apt/lists/*<br><br>RUN apt-get -y update &amp;&amp; apt-get install -y vim</pre><p>This is just an example and you will have different commands in it with different dependencies.</p><h4>🚢 Dockerfile for deployment</h4><p>In this step, previously built image will be used to deploy our project so here is our Dockerfile-Deploy</p><pre>FROM project-build:latest<br>COPY .build_lin/release/Project /perfect-deployed/project/<br>COPY webroot /perfect-deployed/project/webroot<br>COPY config /perfect-deployed/project/config<br>COPY resources /perfect-deployed/project/resources<br>RUN rm -rf /var/lib/apt/lists/*<br>CMD cd /perfect-deployed/project/ &amp;&amp; ./Project</pre><p>Definitely, you need to replace the project with your project name.</p><h4>Deploy and run the container</h4><p>As the image already pushed to a docker registry, you can just pull and run it in your Server. Also, you can easily automate the pulling/running process using a CI tool such as <em>Jenkins</em> or using cloud deployment management tools such as <em>AWS CloudFormation</em> and <em>GCP Deployment Manager</em>.</p><p>You can follow this guide for serving it with <em>NGINX</em>:</p><p><a href="https://medium.com/server-side-swift/server-side-swift-and-nginx-8ca9bb575cc1">Server Side Swift and NGINX</a></p><h3>Conclusion</h3><p>There are many ways to deploy your <em>Server Side Swift</em> either with <em>Docker</em> or without it. This post explained one approach that may not be suitable for everyone and also there are a lot of things that can be improved but as it is, it’s portable and easy to maintain 😐.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74dd06be5039" width="1" height="1" alt=""><hr><p><a href="https://medium.com/server-side-swift/server-side-swift-deployment-one-approach-74dd06be5039">Server Side Swift Deployment — One Approach</a> was originally published in <a href="https://medium.com/server-side-swift">Server Side Swift</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Server Side Swift and NGINX]]></title>
            <link>https://medium.com/server-side-swift/server-side-swift-and-nginx-8ca9bb575cc1?source=rss----6c5cbc488e43---4</link>
            <guid isPermaLink="false">https://medium.com/p/8ca9bb575cc1</guid>
            <category><![CDATA[server-side-swift]]></category>
            <category><![CDATA[vaporswift]]></category>
            <category><![CDATA[kitura]]></category>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[perfect]]></category>
            <dc:creator><![CDATA[Fatih Nayebi]]></dc:creator>
            <pubDate>Mon, 26 Mar 2018 17:00:07 GMT</pubDate>
            <atom:updated>2018-06-29T15:13:34.252Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bjD8YM8uZdE0k6vGtd4oAw.png" /></figure><h3>Why NGINX?</h3><p>Server Side Swift frameworks such as Vapor, Perfect and Kitura provide an embedded HTTP Server so there is no need to run them on a Apache or NGINX server.</p><p>Sometimes you would like to do that because of some of the following possible reasons:</p><ul><li>All the rest of your servers are running on a specific web server so you would like to run your Server Side Swift over there too</li><li>Your security team prefers a specific Web Server</li><li>You need a reverse proxy so you could run multiple servers easily on a virtual machine</li><li>You plan to deploy it to a PaaS such as Google App Engine or Amazon Elastic Beanstalk</li></ul><p>My preferred Web Server to run Server Side Swift is NGINX because of many reasons indicated above. 😊</p><p>Let’s see how we can use it to our benefits.</p><h3>How?</h3><p><strong>NGINX</strong> is a Load Balancer, Web Server and Reverse Proxy.</p><p>The easiest way to run our <em>Server Side Swift</em> project with <em>NGINX</em> is to use it as a Reverse Proxy. Simply we can run our project in <em>Docker</em> or as a service in <em>Linux</em> and use a configuration similar to the following:</p><pre>server {<br>    listen 443 http2;<br>    server_name your server name;<br><br>    # Log files<br>    error_log /yourserver/nginx.yourserver.error.log;<br>    access_log /yourserver/nginx.yourserver.access.log;<br><br>    # ssl<br>    ssl on;<br>    include /etc/nginx/snippets/ssl-params.conf;<br>    ssl_certificate /etc/nginx/ssl/yourserver.crt;<br>    ssl_certificate_key /etc/nginx/ssl/yourserver.key;<br><br>    location / {<br><br>        proxy_set_header        Host $host:$server_port;<br>        proxy_set_header        X-Real-IP $remote_addr;<br>        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;<br>        proxy_set_header        X-Forwarded-Proto $scheme;<br><br>        proxy_pass          http://127.0.0.1:8888;<br>        proxy_read_timeout  90;<br><br>        proxy_redirect      http://127.0.0.1:8888 http://yourserver.com;<br><br>        # Required for new HTTP-based CLI<br>        proxy_http_version 1.1;<br>        proxy_request_buffering off;<br>    }<br>}</pre><p>In this example we run our server on 8888 port and <em>NGINX</em> will route the request coming to our domain to our Swift Server.</p><p>So we do not need to deal with ssl certificates in <em>Server Side Swift</em> and we can easily install them with <em>NGINX</em> as you see above. 😃</p><p>Is n’t it great?!</p><p>I typically run my <em>Server Side Swift</em> project inside a <em>Docker</em> <em>container</em> as <em>HTTP</em> and use <em>NGINX</em> to configure my <em>SSL</em> certificate. This way I can deploy my server anywhere I like easily. I use <em>Google Cloud Platform (GCP) App Engine</em> to run some of my servers and I do not need to get and configure certificates and it already has the NGINX configured. I just need to run my Server Side Swift on 8888! 😊</p><h3>Benefits</h3><p>Using NGINX as a reverse proxy with Server Side Swift simplifies the deployment. It also works better with other systems. For instance, if you use another reverse proxy such as CloudFlare to prevent DDoS attacks, it is better to hook it with NGINX rather than a Web Server that doesn’t have much experience with. 😃</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ca9bb575cc1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/server-side-swift/server-side-swift-and-nginx-8ca9bb575cc1">Server Side Swift and NGINX</a> was originally published in <a href="https://medium.com/server-side-swift">Server Side Swift</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A modern Web Application with React & Perfect Swift Framework]]></title>
            <link>https://medium.com/server-side-swift/this-is-a-very-brief-introduction-to-what-are-the-things-required-to-develop-a-modern-front-end-49d921a071fc?source=rss----6c5cbc488e43---4</link>
            <guid isPermaLink="false">https://medium.com/p/49d921a071fc</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[perfect]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[server-side-swift]]></category>
            <dc:creator><![CDATA[Fatih Nayebi]]></dc:creator>
            <pubDate>Mon, 26 Mar 2018 00:10:06 GMT</pubDate>
            <atom:updated>2018-03-27T18:37:06.047Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*2N9Au5ddkFXPAL4boDvRZw.png" /></figure><p>This is a very brief introduction to what are the things required to develop a modern front-end application or SPA (Single Page Application) with <strong>React</strong> and a backend server with <strong>Swift</strong>, more specifically <strong>Perfect Framework</strong>.</p><p>As we separate the front-end and back-end and because front-end uses just the backend APIs then you can use Vapor or Kitura as you like. I use Perfect as an example here!</p><h3>Front-end</h3><p>A <em>React &amp; Redux</em> app that uses <em>sagas</em> to consume a set of apis provided by a <em>Perfect Server</em>.</p><p>There are many <em>options/boilerplates</em> to start with if you do not like to start from scratch, Facebook’s <strong>create-react-app</strong> is the most popular one:</p><p><a href="https://github.com/facebook/create-react-app">GitHub - facebook/create-react-app: Set up a modern web app by running one command.</a></p><p>If you do not like Facebook 😃 or if you are looking for something a little more opinionated then <strong>react-boilerplate</strong> is your friend:</p><p><a href="https://github.com/react-boilerplate/react-boilerplate">GitHub - react-boilerplate/react-boilerplate: :fire: A highly scalable, offline-first foundation with the best developer experience and a focus on performance and best practices.</a></p><p><em>Did you know that </em><strong><em>Reac</em></strong><em>t is developed by </em><strong><em>Facebook</em></strong><em>? So you need to somehow like Facebook or maybe not because React is an open-source MIT licensed project</em> 😛</p><h4>Front-end artifacts</h4><p>You may think that <strong>JavaScript</strong> ecosystem is so overwhelming and there are too many <em>frameworks/libraries/dependencies</em> that a front-end developer needs to deal with. That’s right, do not try to learn or use everything at the same time. I have a list for you that you can start with 😊:</p><h4>React</h4><p>React is a UI library to develop modern web applications. It is a very light-weight and performant library that provides a <strong>Virtual DOM</strong>. Do not worry about the <em>Virtual DOM</em>, just keep in mind that React changes/renders/syncs only the part that needs to be changed instead of reloading the whole <em>document object model</em>, this process is called <strong>reconciliation</strong>. <em>React</em> is fast because it’s smart! You can read more about the <em>Virtual DOM</em> here:</p><p><a href="https://reactjs.org/docs/faq-internals.html">Virtual DOM and Internals - React</a></p><p>Also, you may have heard that <em>React</em> is not a framework and it is only a front-end library! Your <em>Angular</em> developer friend said that to you, doesn’t she?! OK, that’s right, if we do not consider <strong><em>React Friends</em></strong>!. Actually only 2 imports are sufficient to create a <em>React</em> app 😮</p><ul><li>React and</li><li>ReactDOM</li></ul><p>These are only UI related and most of the time we need more than just UI so <em>React</em> has its friends to help us! Let’s start with the React’s essentials at the moment then we will check friends:</p><h4>Components (Presentational)</h4><p>React.Component is a pure function!</p><h4>Containers</h4><p>mostly <em>stateful</em> components. You can check the differences between components and containers here:</p><p><a href="https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0">Presentational and Container Components</a></p><h4>Props</h4><p>read-only properties of a <em>component</em></p><h4>Component state and lifecycle methods</h4><p>Similar to<em> iOS</em> and <em>Android</em> <em>ViewController</em>/<em>Activity</em> lifecycle methods. For instance,componentDidMount and componentWillUnmount.</p><h4>JSX</h4><p>It looks a little odd at the beginning but you get used to it by the time. It is like writing <strong>html</strong> as <strong>JavaScript</strong> inside the <strong><em>render</em></strong> method of a <em>component</em>.</p><p>That’s pretty much it 😏. Check out this <em>component</em> example from <em>React</em> web site:</p><p><a href="https://reactjs.org/docs/state-and-lifecycle.html">State and Lifecycle - React</a></p><pre>class Clock extends React.Component {<br>  constructor(props) {<br>    super(props);<br>    this.state = {date: new Date()};<br>  }<br><br>  componentDidMount() {<br>    this.timerID = setInterval(<br>      () =&gt; this.tick(),<br>      1000<br>    );<br>  }<br><br>  componentWillUnmount() {<br>    clearInterval(this.timerID);<br>  }<br><br>  tick() {<br>    this.setState({<br>      date: new Date()<br>    });<br>  }<br><br>  render() {<br>    return (<br>      &lt;div&gt;<br>        &lt;h1&gt;Hello, world!&lt;/h1&gt;<br>        &lt;h2&gt;It is {this.state.date.toLocaleTimeString()}.&lt;/h2&gt;<br>      &lt;/div&gt;<br>    );<br>  }<br>}<br><br>ReactDOM.render(<br>  &lt;Clock /&gt;,<br>  document.getElementById(&#39;root&#39;)<br>);</pre><p>You can also try it on <em>CodePen</em> here:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fgaearon%2Fembed%2Fpreview%2FamqdNA%3Fheight%3D600%26slug-hash%3DamqdNA%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fgaearon%2Fpen%2FamqdNA%3Feditors%3D0010&amp;image=https%3A%2F%2Fassets.codepen.io%2F215309%2Finternal%2Fscreenshots%2Fpens%2FamqdNA.default.png%3Ffit%3Dcover%26amp%3Bformat%3Dauto%26amp%3Bha%3Dtrue%26amp%3Bheight%3D360%26amp%3Bquality%3D75%26amp%3Bv%3D2%26amp%3Bversion%3D1506454469%26amp%3Bwidth%3D640&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/37affe919b5b2ba51aa903246fd8ceab/href">https://medium.com/media/37affe919b5b2ba51aa903246fd8ceab/href</a></iframe><h4>React Friends</h4><p>Friends, we need these friends to be able to create a fully functional web application that can connect to a back-end. 😎</p><h4>Redux</h4><p>Unidirectional flow — A great architecture that simplifies the sate management and many other things!</p><p><a href="https://redux.js.org/">Redux - A predictable state container for JavaScript apps. | Redux</a></p><h4>React-Router</h4><p>Routing mechanism</p><p><a href="https://reacttraining.com/react-router">React Router: Declarative Routing for React</a></p><h4>Redux Saga</h4><p>To call our back-end apis</p><p><a href="https://redux-saga.js.org/">Read Me</a></p><h4>Styled Components</h4><p>React/JavaScript based styling that replaces CSS</p><p><a href="https://www.styled-components.com/">styled-components</a></p><h3>Back-end</h3><p>A Server Side Swift framework that provides the following:</p><h4>Local Authentication (with your selected DB flavour)</h4><ul><li>Session</li><li>Login</li><li>Registration</li><li>Reset Pass…</li></ul><p>For instance:</p><p><a href="https://github.com/PerfectlySoft/Perfect-LocalAuthentication-MySQL.git">PerfectlySoft/Perfect-LocalAuthentication-MySQL</a></p><h4>JSON APIs or web sockets according to your needs</h4><p>For instance, <em>PerfectHTTPServer</em> to develop REST APIs</p><p><a href="https://github.com/PerfectlySoft/Perfect-HTTPServer">PerfectlySoft/Perfect-HTTPServer</a></p><p>or <em>PerfectWebSockets</em> to develop web sockets</p><p><a href="https://github.com/PerfectlySoft/Perfect-WebSockets">PerfectlySoft/Perfect-WebSockets</a></p><h4>DBMS</h4><p>Any DBMS or RDBMS of your choice</p><h4>ORM</h4><p>An object relational mapping for Server Side Swift. For instance, <em>PerfectCRUD</em>:</p><p><a href="https://github.com/PerfectlySoft/Perfect-CRUD">PerfectlySoft/Perfect-CRUD</a></p><p>Or StORM:</p><p><a href="https://github.com/SwiftORM">StORM - Swift ORM</a></p><h3>Benefits</h3><h4>Separation of concerns</h4><p>Separating the concerns between the front-end and back-end. Separate teams can work on front-end and back-end projects .</p><h4>Reusability</h4><p>Needless to say that anything we develop in the back-end can be used in any mobile APP or Web APP.</p><p>Also, any component that we develop in React can be easily used in a different web app</p><h4>Security</h4><p>Front-end doesn’t deal with lots of security intensive issues because back-end takes care of them.</p><h4>Code security</h4><ul><li>Compiled backend code to be deployed so the code of the back-end will not be present on the server</li><li>Built Front-end so the code consists of just chunks that are minified and uglified</li></ul><h4>Deployment</h4><ul><li>Front-end is just a static web content with only one <em>html</em> and a set of <em>chunks</em> so we can easily deploy it anywhere we want</li><li>Back-end can be easily deployed with Perfect Assistant (PA) that provides docker containers with binary and config files only!</li></ul><h3>Bottomline</h3><p>Marriage between React and Perfect may produce lots of high quality/reusable/scalable web applications! 🎉🎉🎉</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49d921a071fc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/server-side-swift/this-is-a-very-brief-introduction-to-what-are-the-things-required-to-develop-a-modern-front-end-49d921a071fc">A modern Web Application with React &amp; Perfect Swift Framework</a> was originally published in <a href="https://medium.com/server-side-swift">Server Side Swift</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An introduction to Server Side Swift (Tools & Frameworks)]]></title>
            <link>https://medium.com/server-side-swift/an-introduction-to-server-side-swift-tools-and-framework-b4a3e8268fbb?source=rss----6c5cbc488e43---4</link>
            <guid isPermaLink="false">https://medium.com/p/b4a3e8268fbb</guid>
            <category><![CDATA[server-side-swift]]></category>
            <category><![CDATA[perfect]]></category>
            <category><![CDATA[vaporswift]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[kitura]]></category>
            <dc:creator><![CDATA[Fatih Nayebi]]></dc:creator>
            <pubDate>Mon, 26 Mar 2018 00:09:07 GMT</pubDate>
            <atom:updated>2018-03-27T01:07:32.864Z</atom:updated>
            <content:encoded><![CDATA[<h3>An introduction to Server Side Swift (Tools &amp; Framewoeks)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ryuq5YWZK3_W1jow3TBa6A.png" /></figure><p>This post aims to provide an introduction to <strong><em>Server Side Swift </em></strong>development and covers tools and frameworks.</p><h3>Tools</h3><h4>Swift Package Manager (SPM)</h4><p>Swift Package Manager is the essential build and Dependency Management tool that runs on Mac and Linux.</p><p><a href="https://github.com/apple/swift-package-manager/">apple/swift-package-manager</a></p><p>Most useful SPM commands include:</p><ul><li>swift build to build a swift package</li><li>swift build -c release to build a swift code for release</li><li>swift package update to update dependencies</li><li>swift package generate-xcodeproj to create or update a Xcode project from Swift code</li><li>swift package resolve to fetch all the dependencies</li></ul><p>I will be posting a more complete guide to SPM in the future.</p><h4>Xcode</h4><p>IDE provided by Apple to write, compile and debug Swift code.</p><p>Although there are other options to develop <em>Server Side Swift</em>, <em>Xcode</em> is the most conventional one.</p><p><a href="https://developer.apple.com/xcode/">Xcode - Apple Developer</a></p><p>Other options include</p><ul><li>Text Editors such as vim, Sublime Text, Atom and VS Code + SPM</li><li>AppCode by Jetbrains</li></ul><p><a href="https://www.jetbrains.com/objc/special/appcode/appcode.html">AppCode: Smart Swift &amp; Obj-C IDE by JetBrains</a></p><h4>Perfect Assistant (PA)</h4><p>Developed by <em>PerfectlySoft</em> is the GUI to manage dependencies and build <em>Server Side Swift</em> for <em>macOS</em> and <em>Linux</em>.</p><p>It uses <strong><em>docker</em></strong> to build the <em>Server Side Swift</em> in <em>macOS</em>, creates docker image and spins up containers to run the server in different environments.</p><p>It also facilities deployments to cloud platforms such as <strong>Amazon Web Services (AWS)</strong> and <strong>Google Cloud Platform (GCP)</strong>.</p><p><a href="https://perfect.org/en/assistant/">Perfect Assistant Documentation | Perfect.org</a></p><h4>Vapor Toolbox</h4><p>Vapor’s CLI provides shortcuts and assistance for common tasks</p><p><a href="https://docs.vapor.codes/3.0/getting-started/toolbox/">Toolbox - Vapor Docs</a></p><h4>Kitura CLI</h4><p><a href="https://github.com/IBM-Swift/kitura-cli">IBM-Swift/kitura-cli</a></p><h4>Docker</h4><p>Not specific to <em>Server Side Swift</em>, <em>Docker</em> is used to containerize the code and to run it in different environments. Suppose you are developing a Server on <em>macOS</em> that needs to run on <em>Linux</em> as well, then <em>Docker</em> is your friend because you can use the <em>Xcode</em> and <em>SPM</em> to develop and build in <em>macOS</em> and ensure that your code works in <em>Linux</em>.</p><h3>Server Side Swift Frameworks</h3><p>There are many <em>Server Side Swift</em> Frameworks but there are 3 frameworks that are more popular against others:</p><h4>Vapor</h4><p>Most popular <em>Server Side Swift</em> framework at the moment.</p><p><a href="https://vapor.codes/">Vapor</a></p><h4>Perfect</h4><p>First and most stable <em>Server Side Swift</em> framework</p><p><a href="https://www.perfect.org/">Server-Side Swift is here: it&#39;s Perfect</a></p><h4>Kitura</h4><p>IBM’s take on <em>Server Side Swift</em></p><p><a href="https://github.com/IBM-Swift/Kitura">IBM-Swift/Kitura</a></p><h4>Compariosn</h4><p>Here is my high level comparison:</p><ul><li>If you are looking for a platform easier to understand and a bigger community then <strong>Vapor</strong> is your friend.</li><li>If you are looking for an enterprise grade, and stable framework then <strong>Perfect</strong> is your friend.</li><li>If you are invested in IBM technologies and run your environments on IBM cloud then <strong>Kitura</strong> is your best friend.</li></ul><p>My choice would be <strong><em>Perfect</em></strong> because I aim to develop a production and enterprise level server that communicates with a Big Data Stack.</p><p>Needless to say that there will be no wrong doing on selecting any of these frameworks.</p><h4>Detailed comparisons</h4><p><a href="https://medium.com/u/a623350b656a">Ryan Collins</a>’s comprehensive post on Swift frameworks vs node.js</p><p><a href="https://medium.com/@rymcol/updated-benchmarks-for-the-top-server-side-swift-frameworks-vs-node-js-9da4a0491eca">Updated Benchmarks for the Top Server-Side Swift Frameworks vs. Node.js</a></p><p>Vapor vs. other guys</p><p><a href="https://medium.com/@codevapor/server-side-swift-vs-the-other-guys-1-input-ec48d7be37b7">Server Side Swift vs. The Other Guys — 1: Input</a></p><p>and</p><p><a href="https://medium.com/@codevapor/server-side-swift-vs-the-other-guys-2-speed-ca65b2f79505">Server Side Swift vs. The Other Guys — 2: Speed</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4a3e8268fbb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/server-side-swift/an-introduction-to-server-side-swift-tools-and-framework-b4a3e8268fbb">An introduction to Server Side Swift (Tools &amp; Frameworks)</a> was originally published in <a href="https://medium.com/server-side-swift">Server Side Swift</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>