Git Product home page Git Product logo

swift-kitura-helloworld's Introduction

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:

$ 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:

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:

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:

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:

$ 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:

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:

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

License

Apache 2.0

swift-kitura-helloworld's People

Contributors

kostickm avatar

Watchers

James Cloos avatar Karlo Pagtakhan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.