Awesome
Trevi-Lime
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)
-
Update your local package index first.
$ sudo apt-get update && sudo apt-get upgrade
-
Install Swift dependencies on linux:
$ sudo apt-get install clang libicu-dev
-
Install Swift depending on your platform on the follow link (The latest version are recommended).
-
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
-
Install git to clone libuv:
$ sudo apt-get install git
-
Install libuv dependencies on linux:
$ sudo apt-get install autoconf automake make build-essential libtool gcc g++
-
Clone libuv:
$ git clone https://github.com/libuv/libuv.git
-
Install libuv:
$ cd libuv $ sh autogen.sh $ ./configure $ make $ make check $ sudo make install
More details : Build Instructions on libuv
-
Set LD_LIBRARY_PATH environment variable to run executable.
$ export LD_LIBRARY_PATH=/usr/local/lib/:/usr/lib:$LD_LIBRARY_PATH
Installation (OS X)
-
Install Swift depending on your platform on the follow link (The latest version are recommended).
-
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
-
Clone libuv:
$ git clone https://github.com/libuv/libuv.git
-
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
-
Set LD_LIBRARY_PATH environment variable to run executable.
$ export LD_LIBRARY_PATH=/usr/local/lib/:/usr/lib:$LD_LIBRARY_PATH
Usage
-
Create a new project directory
mkdir HelloLime
-
Initialize this project as a new Swift package project
cd HelloLime swift build --init
Now your directory structure under HelloLime should look like this :
<pre> HelloLime ├── Package.swift ├── Sources │ └── main.swift └── Tests └── <i>empty</i> </pre>Note: For more information on the Swift Package Manager, go here
-
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)), ] )
-
Import the modules in your code (ex: Sources/main.swift) :
import Trevi import Lime
-
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 } }
-
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") }
-
Create and start a server :
let server = Http () server.createServer(lime).listen(8080)
-
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)
-
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
-
-
Now run your application:
.build/debug/HelloLime
-
Open your browser at http://localhost:8080
-
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.