/Trevi-lime

Improved Swift web framework and http server for Trevi

Primary LanguageSwiftApache License 2.0Apache-2.0

Trevi-Lime

Swift 2.2 Mac OS X Ubuntu Apache 2

Overview

Lime is improved web framework for Trevi, and Lime refers to express.js.

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

  • powerful routing
  • user-friendly usage
  • HTTP sub modules (redirection, favicon, etc)
  • View render supporting Swift Server Page template engines
  • Can parsing all type of body data
  • Make high performance.

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 HelloLime
  2. Initialize this project as a new Swift package project

    cd HelloLime
    swift build --init

    Now your directory structure under HelloLime should look like this :

     HelloLime
     ├── 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: "HelloLime",
        dependencies: [
          .Package(url: "https://github.com/Trevi-Swift/Trevi-lime.git", versions: Version(0,1,0)..<Version(0,2,0)),
        ]
    )
  4. Import the modules in your code (ex: Sources/main.swift) :

    import Trevi
    import Lime
  5. Implement one or more routers :

    public class Root {	    
        private let lime = Lime()
        private var router: Router!
        
        public init() {        
            router = lime.router
            
            router.get("/") { req , res , next in
                res.send("Hello Lime!")
            }
    
            router.get("/param/:param") { req , res , next in
                if let param = req.params["param"] {
                    res.send(param)
                }
                next!()
            }
        }
    }
    
    extension Root: Require {
        public func export() -> Router {
            return self.router
        }
    }
  6. Get instance of Lime and put router :

    let lime = Lime()
    lime.use("/", Root())
    lime.use { (req, res, next) in
        res.statusCode = 404
        res.send("404 error")
    }
  7. Create and start a server :

    let server = Http ()
    server.createServer(lime).listen(8080)
  8. Then your code should look like this :

    import Trevi
    import Lime
    
    public class Root {	    
        private let lime = Lime()
        private var router: Router!
        
        public init() {        
            router = lime.router
            
            router.get("/") { req , res , next in
                res.send("Hello Lime!")
            }
    
            router.get("/param/:param") { req , res , next in
                if let param = req.params["param"] {
                    res.send(param)
                }
                next!()
            }
        }
    }
    
    extension Root: Require {
        public func export() -> Router {
            return self.router
        }
    }
    
    let lime = Lime()
    lime.use("/", Root())
    lime.use { (req, res, next) in
        res.statusCode = 404
        res.send("404 error")
    }
    
    let server = Http()
    server.createServer(lime).listen(8080)
  9. 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
  10. Now run your application:

    .build/debug/HelloLime
  11. Open your browser at http://localhost:8080

  12. Enjoy Lime!

Versioning

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