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.
- Github: IBM-Swift/Kitura
- Find this tutorial and more at www.kitura.io!
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:
$ mkdir kituraHelloworld
Create a swift project using the Swift Package Manger:
$ cd kituraHelloworld
$ swift 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:
import PackageDescription
let 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:
import Kitura
// Create a new router
let router = Router()
// Handle HTTP GET requests to /
router.get("/") {
request, response, next in
response.send("Hello, World!")
next()
}
// Add an HTTP server and connect it to the router
Kitura.addHTTPServer(onPort: 8090, with: router)
// Start the Kitura runloop (this call never returns)
Kitura.run()
Compile your application:
$ swift build
Now run your new web application:
$ .build/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:
import PackageDescription
let 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:
import Kitura
import HeliumLogger
// Initialize HeliumLogger
HeliumLogger.use()
// Create a new router
let router = Router()
// Handle HTTP GET requests to /
router.get("/") {
request, response, next in
response.send("Hello, World!")
next()
}
// Add an HTTP server and connect it to the router
Kitura.addHTTPServer(onPort: 8090, with: router)
// Start the Kitura runloop (this call never returns)
Kitura.run()
Compile your application:
$ swift build
Now run your new web application:
$ .build/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:
$ cd kituraHelloworld
Generate the Xcode project:
$ swift package generate-xcodeproj
Your kituraHelloworld directory should look like this:
├── kituraHelloWorld
├── Package.swift
├── Packages
├── Sources
│ └── main.swift
├── Tests
└──kituraHelloworld.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
:
$ export PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/:$PATH