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
Size: 366
Language: null
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
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.
A high performance and simple to use web framework for building modern Swift applications.
Install swift on your system, see swift-install. This tutorial is currently compatible with Xcode 8.3.2 - Swift 3.1.1
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:
Open your browser and visit http://localhost:8090.
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:
Open your browser and visit http://localhost:8090.
You will see logging output in your console:
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:
Click Run.
Open your browser and visit http://localhost:8090.
Issues with swift build
, make sure swift is in your PATH
:
port PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/:$PATH