Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
dileping committed Feb 11, 2016
2 parents 1b66e1a + f953dae commit 497fa7b
Show file tree
Hide file tree
Showing 18 changed files with 734 additions and 184 deletions.
36 changes: 36 additions & 0 deletions .travis.yml
@@ -0,0 +1,36 @@
language: objective-c
osx_image: xcode7.2
env:
global:
- FRAMEWORK_NAME=Express
before_install:
- brew update
- brew unlink carthage
- brew install carthage
- brew link carthage
- brew tap crossroadlabs/tap
- brew install libevhtp --without-oniguruma --with-shared
before_script:
# bootstrap the dependencies for the project
# you can remove if you don't have dependencies
- carthage bootstrap --platform osx
before_deploy:
- carthage build --no-skip-current
- carthage archive $FRAMEWORK_NAME
# - pod trunk push PathToRegex.podspec
script:
- xcodebuild build -project $FRAMEWORK_NAME.xcodeproj -scheme $FRAMEWORK_NAME
#- xcodebuild test -project Regex.xcodeproj -scheme Regex-iOS -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6'
#- xcodebuild test -project Regex.xcodeproj -scheme Regex-tvOS -sdk appletvsimulator -destination 'platform=tvOS Simulator,name=Apple TV 1080p'
#- xcodebuild build -project Regex.xcodeproj -scheme Regex-watchOS -sdk watchsimulator -destination 'platform=watchOS Simulator,name=Apple Watch - 42mm'
notifications:
email: false
deploy:
provider: releases
api_key:
secure: NxWvEe4nqcI99oKbtcFtmpz3AVkQiC+4kFu4OcCusAoKimMxx3WJ4+B6anDLG5dj/5rhsl4rQdKr/WIneHodyPrIFQPsRLUV2KFxgd+vbKrXLRRw+sZb//r0j89E7tCEUPdModIKWN4OCR7UWHvZ5syVQFfsP4MH1r+OJcBiewVVVPhgXzvndkxVlgXonyrJ++NAW/g+jO+CYNU9Y3KkyK7BaPMw6/oSC2Ly3A3zU1El++MFz+ew6YDGxiEiw7y20q7/qTT4pdl2HWem1P04UwPyJ2fUw/nD0C+fvX6tnFYX0s9p9VPSnrWXkhSHKT9IGlr8sveRPuQIaJcXcbkMHF8rnCmMpJJ63+YeXV88krW0q2ZyVpSodh/NaJ76PqbXij9WBSZ8AmNJsBlBsjhAgPCfZZkJ3vjrSSH2XP4k0ICLI8sxYmELXzR0MU1LaexE/4KKMDUcl4zuHWQBTJTMvtDyl+AJk+GdPBSGEpCQvHjXAwuW9OAz/sqZrLQy8eQJmji8DVhMJYlCK+krBQFZIekA6Wun5VTmATrtcwd6qwXyMOYeoblh3308zhzgyjBQwfJVpWxLLqB+dyQNLEOYhMcCcTwWNMvz7RYbEMcplkIu5azuUL400pPk2g2wp6U522vsAmH7bra3+VyA0qYG0ckLiwBaBKVCxD7EzkAy5CA=
file: $FRAMEWORK_NAME.framework.zip
skip_cleanup: true
on:
repo: crossroadlabs/$FRAMEWORK_NAME
tags: true
8 changes: 4 additions & 4 deletions Cartfile
@@ -1,5 +1,5 @@
#github "crossroadlabs/BrightFutures" "master" #due to bug 1100 in carthage 0.12
github "crossroadlabs/BrightFutures" "5127b928524887ce3ad33b7d741f49511902cb03"
github "crossroadlabs/BrightFutures" "master"
github "SwiftyJSON/SwiftyJSON"
#github "groue/GRMustache.swift" "Swift2.1" #due to bug 1100 in carthage 0.12
github "groue/GRMustache.swift" "bf7d6031d7e0dd862519eaba2b36b2e11a0d25a9"
github "groue/GRMustache.swift" "Swift2.1"
github "crossroadlabs/Regex" ~> 0.4
github "crossroadlabs/PathToRegex" ~> 0.1
96 changes: 84 additions & 12 deletions Demo/main.swift
Expand Up @@ -51,15 +51,87 @@ app.errorHandler.register { e in
return Action<AnyContent>.render("test", context: ["test": "error", "items": viewItems])
}

app.get("/*", action: StaticAction(path: "public"))
/// StaticAction is just a predefined configurable handler for serving static files.
/// It's important to pass exactly the same param name to it from the url pattern.
app.get("/:file+", action: StaticAction(path: "public", param:"file"))

app.get("/hello") { request in
return Action.ok(AnyContent(str: "<h1>Hello Express!!!</h1>", contentType: "text/html"))
}

//user as an url param
app.get("/hello/:user.html") { request in
//get user
let user = request.params["user"]
//if there is a user - create our context. If there is no user, context will remain nil
let context = user.map {["user": $0]}
//render our template named "hello"
return Action.render("hello", context: context)
}

app.post("/api/user") { request in
//check if JSON has arrived
guard let json = request.body?.asJSON() else {
return Action.ok("Invalid request")
}
//check if JSON object has username field
guard let username = json["username"].string else {
return Action.ok("Invalid request")
}
//compose the response as a simple dictionary
let response =
["status": "ok",
"description": "User with username '" + username + "' created succesfully"]

//render disctionary as json (remember the one we've registered above?)
return Action.render(JsonView.name, context: response)
}

app.get("/myecho") { request in
return Action.ok(request.query["message"]?.first)
}

//:param - this is how you define a part of URL you want to receive through request object
app.get("/myecho/:param") { request in
//here you get the param from request: request.params["param"]
return Action.ok(request.params["param"])
}

func factorial(n: Int) -> Int {
return n == 0 ? 1 : n * factorial(n - 1)
}

func calcFactorial(num:Int) -> Future<Int, AnyError> {
return future {
return factorial(num)
}
}

// (request -> Future<Action<AnyContent>, AnyError> in) - this is required to tell swift you want to return a Future
// hopefully inference in swift will get better eventually and just "request in" will be enough
app.get("/factorial/:num(\\d+)") { request -> Future<Action<AnyContent>, AnyError> in
// get the number from the url
let num = request.params["num"].flatMap{Int($0)}.getOrElse(0)

// get the factorial Future. Returns immediately - non-blocking
let factorial = calcFactorial(num)

//map the result of future to Express Action
let future = factorial.map { fac in
Action.ok(String(fac))
}

//return the future
return future
}

app.get("/test") { req in
return future {
return try test()
}
}

app.get("/test.html") { (request:Request<AnyContent>)->Action<AnyContent> in
app.get("/test.html") { request in
let newItems = request.query.map { (k, v) in
(k, v.first!)
}
Expand All @@ -73,26 +145,26 @@ app.get("/test.html") { (request:Request<AnyContent>)->Action<AnyContent> in
throw TestError.Test
}

return Action<AnyContent>.render("test", context: ["test": "ok", "items": viewItems])
return Action.render("test", context: ["test": "ok", "items": viewItems])
}

app.get("/echo") { request in
return Action<AnyContent>.chain()
return Action.chain()
}

app.get("/myecho") { request in
return Action<AnyContent>.ok(AnyContent(str: request.query["message"]?.first))
return Action.ok(AnyContent(str: request.query["message"]?.first))
}

app.get("/hello") { request in
return Action<AnyContent>.ok(AnyContent(str: "<h1>Hello Express!!!</h1>", contentType: "text/html"))
return Action.ok(AnyContent(str: "<h1>Hello Express!!!</h1>", contentType: "text/html"))
}

app.get("/") { (request:Request<AnyContent>)->Action<AnyContent> in
app.get("/") { request in
for me in request.body?.asJSON().map({$0["test"]}) {
print(me)
}
return Action<AnyContent>.ok(AnyContent(str:"{\"response\": \"hey hey\"}", contentType: "application/json"))
return Action.ok(AnyContent(str:"{\"response\": \"hey hey\"}", contentType: "application/json"))
}

func echoData(request:Request<AnyContent>) -> Dictionary<String, String> {
Expand All @@ -112,14 +184,14 @@ func echo(request:Request<AnyContent>) -> Action<AnyContent> {
func echoRender(request:Request<AnyContent>) -> Action<AnyContent> {
var data = echoData(request)
data["hey"] = "Hello from render"
return Action.render("json", context: data)
return Action.render(JsonView.name, context: data)
}

app.post("/echo/inline") { (request:Request<AnyContent>)->Action<AnyContent> in
app.post("/echo/inline") { request in
let call = request.body?.asJSON().map({$0["say"]})?.string
let response = call.getOrElse("I don't hear you!")

return Action<AnyContent>.ok(AnyContent(str:"{\"said\": \"" + response + "\"}", contentType: "application/json"))
return Action.ok(AnyContent(str:"{\"said\": \"" + response + "\"}", contentType: "application/json"))
}

app.get("/echo") { request in
Expand All @@ -143,7 +215,7 @@ app.post("/echo3") { request in
contentType: request.contentType))
}

app.handle(HttpMethod.Any.rawValue, path: "/async/echo") { request in
app.all("/async/echo") { request in
return future {
return echo(request)
}
Expand Down
5 changes: 5 additions & 0 deletions Demo/views/hello.mustache
@@ -0,0 +1,5 @@
<html>
<body>
<h1>Hello: {{user}}</h1>
</body>
</html>

0 comments on commit 497fa7b

Please sign in to comment.