IBM/swift-kitura-helloworld

Name: swift-kitura-helloworld

Owner: International Business Machines

Description: Kitura hands-on demo - Simple tutorial for getting started developing server-side Swift applications.

Created: 2017-03-14 05:34:03.0

Updated: 2017-11-14 13:43:28.0

Pushed: 2017-04-26 17:59:40.0

Homepage:

Size: 366

Language: null

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

swift-kitura-helloworld

Learn how to create a simple Kitura web application, add logging, and generate an Xcode project. This tutorial will take approximately 5 minutes to complete. You should complete the prerequisites before starting this tutorial.

Kitura Logo

A high performance and simple to use web framework for building modern Swift applications.

Steps

  1. Prerequisites
  2. Getting Started
  3. Add Logging
  4. Generate Xcode Project

1. Prerequisites

Install swift on your system, see swift-install. This tutorial is currently compatible with Xcode 8.3.2 - Swift 3.1.1

2. Getting Started

Create a new directory for your project:

dir kituraHelloworld

Create a swift project using the Swift Package Manger:

 kituraHelloworld
ift package init --type executable

Your kituraHelloworld directory should look like this:

kituraHelloWorld
Package.swift
Sources
??? main.swift
Tests

In Package.swift, add Kitura as a dependency for your project:

rt PackageDescription

package = Package(
name: "kituraHelloworld",
dependencies: [
    .Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 1, minor: 7)
])

In Sources/main.swift, add the following code:

rt Kitura

reate a new router
router = Router()

andle HTTP GET requests to /
er.get("/") {
request, response, next in
response.send("Hello, World!")
next()


dd an HTTP server and connect it to the router
ra.addHTTPServer(onPort: 8090, with: router)

tart the Kitura runloop (this call never returns)
ra.run()

Compile your application:

ift build

Now run your new web application:

uild/debug/kituraHelloworld

If prompted, click Allow:

Network Connection Permission

Open your browser and visit http://localhost:8090.

Browser Helloworld

3. Add Logging

Add HeliumLogger as a dependency of your application in Package.swift:

rt PackageDescription

package = Package(
name: "kituraHelloworld",
dependencies: [
    .Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 1, minor: 7),
    .Package(url: "https://github.com/IBM-Swift/HeliumLogger.git", majorVersion: 1, minor: 7)
])

Enable HeliumLogger in Sources/main.swift:

rt Kitura
rt HeliumLogger

nitialize HeliumLogger
umLogger.use()

reate a new router
router = Router()

andle HTTP GET requests to /
er.get("/") {
request, response, next in
response.send("Hello, World!")
next()


dd an HTTP server and connect it to the router
ra.addHTTPServer(onPort: 8090, with: router)

tart the Kitura runloop (this call never returns)
ra.run()

Compile your application:

ift build

Now run your new web application:

uild/debug/kituraHelloworld

If prompted, click Allow:

Network Connection Permission

Open your browser and visit http://localhost:8090.

You will see logging output in your console:

Console Output

4. Generate Xcode Project

Note: MacOS Only

Navigate to your kituraHelloworld directory:

 kituraHelloworld

Generate the Xcode project:

ift package generate-xcodeproj

Your kituraHelloworld directory should look like this:

kituraHelloWorld
Package.swift
Packages
Sources
??? main.swift
Tests
ituraHelloworld.xcodeproj

Open the generated project in Xcode.

Change the build scheme to your executable:

Xcode Scheme Exec

Click Run.

Xcode Rum

Open your browser and visit http://localhost:8090.

Troubleshooting

Issues with swift build, make sure swift is in your PATH:

port PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/:$PATH

License

Apache 2.0


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.