github.com/ZJSwift/Kitura

Web framework and HTTP server for Swift



Documentation

Kitura

A Swift Web Framework and HTTP Server

Build Status Build Status Mac OS X Linux Swift 2 compatible Apache 2 Join the chat at https://gitter.im/IBM-Swift/Kitura

Summary

Kitura is a web framework and web server that is created for web services written in Swift.

Features:

  • URL routing (GET, POST, PUT, DELETE)
  • URL parameters
  • Static file serving
  • JSON parsing
  • Pluggable middleware

Installation (Docker development environment)

  1. Install Docker on your development system and start a Docker session/terminal.

  2. From the Docker session, pull down the kitura-ubuntu image from Docker Hub:

    docker pull ibmcom/kitura-ubuntu:latest

  3. Create a Docker container using the kitura-ubuntu image you just downloaded:

    docker run -i -t ibmcom/kitura-ubuntu:latest /bin/bash

  4. From within the Docker container, execute the ci.sh script to build Kitura and execute the test cases:

    source /root/ci.sh

    The last output line from executing the ci.sh script should be similar to >> Build and execution of test cases completed (see above for results).

  5. You can now run the KituraSample executable inside the Docker container:

    /root/Kitura/.build/debug/KituraSample

    You should see a message that says "Listening on port 8090".

Installation (Vagrant development environment)

  1. Download VirtualBox.

  2. Download Vagrant.

  3. From the root of the Kitura folder containing the vagrantfile, create and configure a guest machine:

    vagrant up

  4. SSH into the Vagrant machine:

    vagrant ssh

  5. From the Vagrant shell, run the sample program:

    Kitura/.build/debug/KituraSample. You should see a message that says "Listening on port 8090".

  6. As needed for development, edit the vagrantfile to setup Synced Folders to share files between your host and guest machine.

Installation (OS X)

  1. Clone this repository:

    git clone https://github.com/IBM-Swift/Kitura

  2. Install Homebrew:

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

  3. Install the necessary dependencies:

    brew install http-parser pcre2 curl hiredis

  4. Download and install the latest Swift compiler.

    Make sure the latest Swift compiler is installed https://swift.org/download/. After installing it, make sure you update your PATH environment variable as described in the installation instructions (e.g. export PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:$PATH)

  5. Build Kitura and Kitura Sample

    Run make to build the helper libraries, Kitura framework, and the sample program (invokes swift build).

  6. Run KituraSample:

    You can run the sample program which located in: <path-to-repo>/.build/debug. From the project root, execute the ./.build/debug/KituraSample command from a terminal window. You should see a message that says "Listening on port 8090".

Installation (Linux)

  1. Install the following system linux libraries:

    sudo apt-get install libhttp-parser-dev libcurl4-openssl-dev libhiredis-dev

  2. Install the latest Swift compiler for Linux.

    Follow the instructions provided on that page. After installing it (i.e. uncompressing the tar file), make sure you update your PATH environment variable so that it includes the extracted tools: export PATH=/<path to uncompress tar contents>/usr/bin:$PATH. To update the PATH env variable, you can update your .bashrc file (for further information on .bashrc and .bash_profile see http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html).

  3. Clone the patched libdispatch library:

    git clone -b opaque-pointer git://github.com/seabaylea/swift-corelibs-libdispatch

  4. Build and install the patched libdispatch library:

    Please note that the complete instructions for building and installing this library are found here. Though, all you need to do is just this: cd swift-corelibs-libdispatch && sh ./autogen.sh && ./configure && make && sudo make install

  5. Install modulemap on the system:

    Add a modulemap file for the libdispatch library to the following folder: /usr/local/include/dispatch. You can simply copy the contents of the following map module file in module.modulemap.

  6. Compile and install PCRE2:

    Download the pcre2 source code. Unpack the tar. Run ./configure && make && sudo make install. This will place the necessary headers and libraries into /usr/local/include and /user/local/libs.

  7. Build Kitura and Kitura Sample

    On the root folder of the Kitura repo, run make to build the helper libraries, Kitura framework, and the sample program (invokes swift build).

  8. Set the dynamic library loading path:

    In order to run the sample, first you need to point to the shared libraries that have been built by running export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

  9. Run the sample program:

    <path_to_kitura_repo>./.build/debug/KituraSample. You should see a message that says "Listening on port 8090".

Usage

Let's write our first Kitura-based Web Application written in Swift!

1) First we need to create a new project directory

mkdir myFirstProject

2) Next we need to go in an initialize this project as a new Swift package project

cd myFirstProject
swift build --init

Now your directory structure under myFirstProject should look like this:

myFirstProject
├── Package.swift
├── Sources
│   └── main.swift
└── Tests
    └── empty

Note: For more information on the Swift Package Manager, go here

3) Now we need to add Kitura as a dependency for your project (Package.swift):

import PackageDescription

let package = Package(
    name: "myFirstProject",

    dependencies: [
        .Package(url: "https://github.com/IBM-Swift/Kitura-router.git", majorVersion: 0),
    ]

)

4) Now we can issue a swift build command to to download the dependencies.

Because Swift Package Manager does not compile C code, expect this step to fail because of a linker error.

swift build

5) Copy the Makefile.client from KituraNet to your project as Makefile:

cp Packages/Kitura-net-0.2.0/Makefile-client Makefile

6) Import the modules in your code:

import KituraRouter
import KituraNet
import KituraSys

7) Add a router and a path:

let router = Router()

router.get("/") {
    request, response, next in

    response.status(HttpStatusCode.OK).send("Hello, World!")

    next()
}

8) Create and start a HTTPServer:

let server = HttpServer.listen(8090, delegate: router)
Server.run()

9) Sources/main.swift file should now look like this:

import KituraRouter
import KituraNet
import KituraSys

let router = Router()

router.get("/") {
request, response, next in

    response.status(HttpStatusCode.OK).send("Hello, World!")

    next()
}

let server = HttpServer.listen(8090, delegate: router)
Server.run()

10) Run make

make

11) Now run your new web application

.build/debug/myFirstProject

12) Open your browser at http://localhost:8090

License

This library is licensed under Apache 2.0. Full license text is available in LICENSE.