Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
helje5 committed Jun 3, 2020
2 parents e00af59 + 7b8e5b4 commit d1915e6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 40 deletions.
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ matrix:
include:
- os: Linux
dist: trusty
env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-5.0.2-release/ubuntu1404/swift-5.0.2-RELEASE/swift-5.0.2-RELEASE-ubuntu14.04.tar.gz"
env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-5.0.3-release/ubuntu1404/swift-5.0.3-RELEASE/swift-5.0.3-RELEASE-ubuntu14.04.tar.gz"
sudo: required
- os: Linux
dist: trusty
env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-5.1.3-release/ubuntu1404/swift-5.1.3-RELEASE/swift-5.1.3-RELEASE-ubuntu14.04.tar.gz"
env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-5.1.5-release/ubuntu1404/swift-5.1.5-RELEASE/swift-5.1.5-RELEASE-ubuntu14.04.tar.gz"
sudo: required
- os: Linux
dist: xenial
env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-5.2.4-release/ubuntu1604/swift-5.2.4-RELEASE/swift-5.2.4-RELEASE-ubuntu16.04.tar.gz"
sudo: required
- os: osx
osx_image: xcode11
- os: osx
osx_image: xcode11.4

before_install:
- ./.travis.d/before-install.sh
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ This repository contains examples for

### Running Examples as Scripts

Single file examples can be run as scripts using [swift-sh](https://github.com/mxcl/swift-sh),
which can be installed using a simple `brew install mxcl/made/swift-sh`.
Examples can be run as scripts using [swift-sh](https://github.com/mxcl/swift-sh)
(can be installed using a simple `brew install mxcl/made/swift-sh`).

Example:
```
$ Sources/httpd-helloworld/main.swift
2020-01-07T17:14:35+0100 notice: Server listening on http://localhost:1337/
$ Sources/express-simple/main.swift
2020-06-03T14:58:59+0200 notice: Server listening on http://localhost:1337
```

Looks like this:
![MacroExpress Simple Screenshot](https://zeezide.de/img/macro/MacroExpressSimple.png)


### Who

**Macro** is brought to you by
Expand Down
6 changes: 3 additions & 3 deletions Sources/connect-static/main.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/swift sh

import Foundation
import MacroExpress // @Macro-swift ~> 0.0.3
import MacroExpress // @Macro-swift ~> 0.0.4

let dirname = __dirname()

Expand All @@ -11,8 +11,8 @@ app.use(logger("dev"))
app.use(serveStatic(__dirname() + "/public"))

app.use { req, res, next in
guard req.url == "/" else { return next() }
res.redirect("/index.html")
guard req.url == "/" else { return next() }
res.redirect("/index.html")
}

app.listen(1337) {
Expand Down
62 changes: 31 additions & 31 deletions Sources/express-simple/main.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/swift sh

import MacroExpress // @Macro-swift ~> 0.0.3
import MacroExpress // @Macro-swift ~> 0.0.4
import cows // @AlwaysRightInstitute ~> 1.0.0

let app = express()
Expand All @@ -20,77 +20,77 @@ app.set("views", __dirname() + "/views")
// MARK: - Session View Counter

app.use { req, _, next in
req.session["viewCount"] = req.session[int: "viewCount"] + 1
next()
req.session["viewCount"] = req.session[int: "viewCount"] + 1
next()
}


// MARK: - Routes

let taglines = [
"Less than Perfect.",
"Das Haus das Verrückte macht.",
"Rechargeables included",
"Sensible Server Side Swift aS a Successful Software Service Solution"
"Less than Perfect.",
"Das Haus das Verrückte macht.",
"Rechargeables included",
"Sensible Server Side Swift aS a Successful Software Service Solution"
]


// MARK: - Form Handling

app.get("/form") { _, res, _ in
res.render("form")
res.render("form")
}
app.post("/form") { req, res, _ in
let user = req.body[string: "u"]
console.log("USER IS: \(user)")
let user = req.body[string: "u"]
console.log("USER IS: \(user)")

let options : [ String : Any ] = [
"user" : user,
"nouser" : user.isEmpty,
"viewCount" : req.session["viewCount"] ?? 0
]
res.render("form", options)
let options : [ String : Any ] = [
"user" : user,
"nouser" : user.isEmpty,
"viewCount" : req.session["viewCount"] ?? 0
]
res.render("form", options)
}


// MARK: - JSON & Cookies

app.get("/json") { _, res, _ in
res.json([
[ "firstname": "Donald", "lastname": "Duck" ],
[ "firstname": "Dagobert", "lastname": "Duck" ]
])
res.json([
[ "firstname": "Donald", "lastname": "Duck" ],
[ "firstname": "Dagobert", "lastname": "Duck" ]
])
}

app.get("/cookies") { req, res, _ in
// returns all cookies as JSON
res.json(req.cookies)
// returns all cookies as JSON
res.json(req.cookies)
}


// MARK: - Cows

app.get("/cows") { _, res, _ in
res.send("<html><body><pre>\(cows.vaca())</pre></body></html>")
res.send("<html><body><pre>\(cows.vaca())</pre></body></html>")
}


// MARK: - Main page

app.get("/") { req, res, _ in
let tagline = taglines.randomElement()!
let tagline = taglines.randomElement()!

let values : [ String : Any ] = [
"tagline" : tagline,
"viewCount" : req.session["viewCount"] ?? 0,
"cowOfTheDay" : cows.vaca()
]
res.render("index", values)
let values : [ String : Any ] = [
"tagline" : tagline,
"viewCount" : req.session["viewCount"] ?? 0,
"cowOfTheDay" : cows.vaca()
]
res.render("index", values)
}


// MARK: - Start Server

app.listen(1337) {
console.log("Server listening: \($0)")
console.log("Server listening on http://localhost:1337")
}
8 changes: 6 additions & 2 deletions Sources/express-simple/views/index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{{> header}}

<p>
Reload page for a new cow and tagline!
</p>

<pre style="border: 1px solid #DEDEDE; margin: 1em; padding: 0.5em;"
>{{cowOfTheDay}}</pre>

<div class="menu-centered">
<ul class="menu">
<li><a href="/form">Form</a></li>
<li><a href="https://github.com/Macro-swift">Macro</a></li>
<li><a href="/form">Form Demo</a></li>
<li><a href="https://github.com/Macro-swift">Macro.swift</a></li>
</ul>
</div>

Expand Down

0 comments on commit d1915e6

Please sign in to comment.