Giter VIP home page Giter VIP logo

kitura's Introduction

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 (OS X)

  1. Clone this repository:

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

  1. Install Homebrew:

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

  1. Install the necessary dependencies:

brew install http-parser, brew install pcre2, brew install curl, and brew install hiredis

  1. 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)

  1. Grab the Swift package dependencies using Swift Package Manager

In the root directory of this project run swift build to copy the dependencies. Note the build process itself will fail!

  1. Build Kitura and Kitura Sample

Then run make to build the helper libraries, Kitura framework, and the sample program.

  1. 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

  1. 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).

  1. Clone the patched libdispatch library:

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

  1. 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

  1. 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.

  1. 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.

  1. Download the Kitura dependencies with Swift Package Manager:

In the root directory of this project run swift build to copy the dependencies. Note the build process itself will fail!

  1. Build the helper modules:

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

  1. 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

  1. 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
  1. 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

  1. 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),
	]

)
  1. 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
  1. Copy the Makefile.client from KituraNet to your project as Makefile:
cp Packages/Kitura-net-0.2.0/Makefile-client Makefile
  1. Import the modules in your code:
import KituraRouter
import KituraNet
import KituraSys
  1. Add a router and a path:
let router = Router()

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

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

	next()
}
  1. Create and start a HTTPServer:
let server = HttpServer.listen(8090, delegate: router)
Server.run()
  1. 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()
  1. Run make
make
  1. Now run your new web application
.build/debug/myFirstProject
  1. Open your browser at http://localhost:8090

License

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

kitura's People

Contributors

rfdickerson avatar rolivieri avatar dfirsht avatar pbohrer avatar scottra avatar shmuelk avatar vadimeisenbergibm avatar rsmoz avatar

Stargazers

 avatar  avatar

Watchers

James Cloos 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.