News Tips & Tricks

Server-side Swift in CLion

Hi everyone,

In this article, we will take a look at the CLion and AppCode features which are especially useful for server-side Swift development. You can either read through to the end of this article to learn about them all or watch the short demo below to see them in action:


Before you begin

  1. Download the latest CLion 2018.3 EAP.
  2. Start CLion and go to Preferences | Plugins.
  3. Install the following plugins:
  4. Restart CLion to activate the plugins.
  5. Checkout the Kitura-Sample project and open it in CLion.
  6. Wait for the cache/index phase to finish.
  7. Install PostgreSQL via brew install postgresql.

HTML/JavaScript

Get the same functionality for editing HTML and JavaScript files as WebStorm, including all the code generation and refactoring actions:
HTML code assistance

Debug your JavaScript code as easily as this:

Web Templates

Get Handlebars/Mustache, Markdown, and other template languages with support from our dedicated plugins:
Mustache

There is no plugin for Stencil templates, so it’s not possible to have all the code assistance features working both for Stencil and HTML syntax together. We can set up some code assistance for Stencil files and syntax using one of the following approaches:

  1. Open Preferences | Editor | File Types, find HTML file type and add *.stencil to the Registered patterns sections. After these steps CLion will recognize any Stencil template as an HTML file, providing code assistance for HTML and showing Stencil parts as plain text.
  2. Use the Stencil settings from this article. In this case, only the Stencil syntax will be highlighted and no HTML code assistance will be available.

Build and Run

Press ⌃R to build and run the project. Kitura server will start on localhost:8080. Let’s set up CLion to open this link automatically when we finish building the project:

  1. Click Edit Configurations… in the Run Configurations popup.
  2. Select Kitura-Sample Run Configuration.
  3. Expand the Before Launch section and click the + button:Run Configurations
  4. Choose Launch Web Browser.
  5. Select Safari as browser and set Url field to http://localhost:8080:Launch browser action
  6. Click OK.

Run the project again and have http://localhost:8080 opened in Safari automatically.

Databases

For this sample project to work, we need a school database. Open Terminal via ⌥F12 and execute createdb school. Connect to the database using the following steps:

  1. Open View | Tool Windows | Database.
  2. Press ⌘N and add a new PostgreSQL datasource:Add database
  3. Enter the database name and test the connection:Database properties
  4. Click OK to save the connection.

View the contents of the school database and see how it’s changed when adding a new entity:
Database contents

Deployment

Docker

The Docker integration plugin provides full code assistance for Docker configuration files:
Docker code assistance

The Dockerfile in our sample project contains everything we need to run our application in a Docker container. Select the Docker Run Configuration and run it via ⌃R.

Wait for the container to be started and open http://localhost:8080 in your favorite browser. Kitura sample should be up and running.

Remote Hosts

We can now sync our sources to the Docker container via SFTP:

  1. Open View | Tool Windows | Remote host.
  2. Select the Docker remote host configuration in the drop-down:Remote hosts
  3. Switch to Remote Host tool window and expand package/Views/docs directory.
  4. Open doc1.md via ⌘↓.
  5. Navigate to Rendering Markdown | First chapter section of the sample Kitura application in the browser.
  6. Change the contents of doc1.md, press ⇧⌘⌥X (Upload to…), and select the Docker server
  7. As soon as the file is uploaded to the Docker server, repeat step 5 to see changes instantly

Define any remote server in Preferences | Build, Execution, Deployment | Deployment and synchronize the whole project or specific files to test your changes:
Remote host preferences

Select Tools | Deployment | Sync with Deployed to... to see the difference between the local and remote versions before they are synchronized:
Sync with remote

Testing requests

Open the requests.http file and click the Run button on the left gutter:
Execute request

Have the formatted response displayed in the dedicated tool window:
Response

Take advantage of code assistance for .rest and .http files when creating your own requests:
Request code assistance

That’s it! We hope all these features, together with Swift Package Manager support in CLion 2018.3 EAP, will help you create better web apps using Swift!

Your AppCode Team
JetBrains
The Drive to Develop