Skip to content

Trevi-Swift/Trevi

Repository files navigation

Trevi

Swift 2.2 Mac OS X Ubuntu Apache 2

Overview

Fast, light web application server framework for Swift. Trevi uses an event-driven, non-blocking I/O model based on libuv (https://github.com/libuv/libuv).
Trevi refers to node.js core modules and makes Trevi core modules similar to support node.js features. Trevi also hopes that node.js developers easily use and develop Trevi.

Notice

If you want to build or test all projects at Xcode, please check out Trevi-Dev. Otherwise, you can build Trevi, lime and other packages by using Swift Package manager.
Here are an example and it now runs on Linux.

Features

  • Handles persistent streams
  • No dependencies
  • Effective data transfer
  • Parsing HTTP message

Swift version

Trevi works with the latest version of Swift 3.0 or latest Snapshot. You can download Swift binaries on here.

Installation (Ubuntu; APT-based linux)

  1. Update your local package index first.

    $ sudo apt-get update && sudo apt-get upgrade
  2. Install Swift dependencies on linux:

    $ sudo apt-get install clang libicu-dev
  3. Install Swift depending on your platform on the follow link (The latest version are recommended).

  4. After installation of Swift, check your PATH environment variable whether Swift binary path is set or not. If it is not set execute below.:

    $ export PATH=/path/to/swift/installed/usr/bin:"${PATH}"

    More details : 'Linux' on here

  5. Install git to clone libuv:

    $ sudo apt-get install git
  6. Install libuv dependencies on linux:

    $ sudo apt-get install autoconf automake make build-essential libtool gcc g++
  7. Clone libuv:

    $ git clone https://github.com/libuv/libuv.git
  8. Install libuv:

    $ cd libuv
    $ sh autogen.sh
    $ ./configure
    $ make
    $ make check
    $ sudo make install

    More details : Build Instructions on libuv

  9. Set LD_LIBRARY_PATH environment variable to run executable.

    $ export LD_LIBRARY_PATH=/usr/local/lib/:/usr/lib:$LD_LIBRARY_PATH

Installation (OS X)

  1. Install Swift depending on your platform on the follow link (The latest version are recommended).

  2. After installation of Swift, check your PATH environment variable whether Swift binary path is set or not. If it is not set execute below.:

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

    More details : 'Apple Platforms' on here

  3. Clone libuv:

    $ git clone https://github.com/libuv/libuv.git
  4. Install libuv:

    $ cd libuv
    $ sh autogen.sh
    $ ./configure
    $ make
    $ make check
    $ sudo make install

    or using Homebrew:

    $ brew install --HEAD libuv

    More details : Build Instructions on libuv

  5. Set LD_LIBRARY_PATH environment variable to run executable.

    $ export LD_LIBRARY_PATH=/usr/local/lib/:/usr/lib:$LD_LIBRARY_PATH

Usage

  1. Create a new project directory

    mkdir HelloTrevi
  2. Initialize this project as a new Swift package project

    $ cd HelloTrevi
    $ swift build --init

    Now your directory structure under HelloTrevi should look like this :

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

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

  3. Add a dependency of Trevi for your project (Package.swift) :

    import PackageDescription
    
    let package = Package(
        name: "HelloTrevi",
        dependencies: [
          .Package(url: "https://github.com/Trevi-Swift/Trevi.git", versions: Version(0,1,0)..<Version(0,2,0)),
        ]
    )
  4. Import the modules, create and start a HTTPServer in your code (Sources/main.swift) :

    import Trevi
    
    let server = Http ()
    
    server.createServer({ (req, res, next) in
        res.statusCode = 200
        res.write("Hello Trevi")
        res.end()
    }).listen(8081)
  5. Build your application :

    • Mac OS X:

      $ swift build -Xcc -fblocks -Xswiftc -I/usr/local/include -Xlinker -L/usr/local/lib
    • Ubuntu:

      $ swift build -Xcc -fblocks
  6. Now run your application:

    $ .build/debug/HelloTrevi

    Note: May need to set LD_LIBRARY_PATH environment variable to run:

    $ export LD_LIBRARY_PATH=/usr/local/lib/:/usr/lib:$LD_LIBRARY_PATH
  7. Open your browser at http://localhost:8080

  8. Enjoy Trevi!

Versioning

Trevi follows the semantic versioning scheme. The API change and backwards compatibility rules are those indicated by SemVer.

License

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

Copyright 2015 Trevi Community

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.