Skip to content

Commit e00af59

Browse files
committed
Add express-simple example
Seems to work except for sessions.
1 parent a25393f commit e00af59

File tree

9 files changed

+231
-4
lines changed

9 files changed

+231
-4
lines changed

Package.swift

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@ let package = Package(
1212

1313
products: [
1414
.executable(name: "httpd-helloworld", targets: [ "httpd-helloworld" ]),
15-
.executable(name: "connect-static", targets: [ "connect-static" ])
15+
.executable(name: "connect-static", targets: [ "connect-static" ]),
16+
.executable(name: "express-simple", targets: [ "express-simple" ])
1617
],
1718

1819
dependencies: [
1920
.package(url: "https://github.com/Macro-swift/Macro.git",
2021
from: "0.0.16"),
2122
.package(url: "https://github.com/Macro-swift/MacroExpress.git",
22-
from: "0.0.3")
23+
from: "0.0.3"),
24+
.package(url: "https://github.com/AlwaysRightInstitute/cows",
25+
from: "1.0.0")
2326
],
2427

2528
targets: [
2629
.target(name: "httpd-helloworld", dependencies: [ "Macro" ]),
27-
.target(name: "connect-static", dependencies: [ "MacroExpress" ])
30+
.target(name: "connect-static", dependencies: [ "MacroExpress" ]),
31+
.target(name: "express-simple", dependencies: [ "MacroExpress", "cows" ])
2832
]
2933
)

Sources/connect-static/main.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/swift sh
22

33
import Foundation
4-
import MacroExpress // @Macro-swift ~> 0.0.2
4+
import MacroExpress // @Macro-swift ~> 0.0.3
55

66
let dirname = __dirname()
77

Sources/express-simple/main.swift

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/swift sh
2+
3+
import MacroExpress // @Macro-swift ~> 0.0.3
4+
import cows // @AlwaysRightInstitute ~> 1.0.0
5+
6+
let app = express()
7+
8+
app.use(logger("dev"))
9+
app.use(bodyParser.urlencoded())
10+
app.use(cookieParser())
11+
app.use(session())
12+
app.use(serveStatic(__dirname() + "/public"))
13+
14+
// MARK: - Express Settings
15+
16+
app.set("view engine", "html") // really mustache, but we want to use .html
17+
app.set("views", __dirname() + "/views")
18+
19+
20+
// MARK: - Session View Counter
21+
22+
app.use { req, _, next in
23+
req.session["viewCount"] = req.session[int: "viewCount"] + 1
24+
next()
25+
}
26+
27+
28+
// MARK: - Routes
29+
30+
let taglines = [
31+
"Less than Perfect.",
32+
"Das Haus das Verrückte macht.",
33+
"Rechargeables included",
34+
"Sensible Server Side Swift aS a Successful Software Service Solution"
35+
]
36+
37+
38+
// MARK: - Form Handling
39+
40+
app.get("/form") { _, res, _ in
41+
res.render("form")
42+
}
43+
app.post("/form") { req, res, _ in
44+
let user = req.body[string: "u"]
45+
console.log("USER IS: \(user)")
46+
47+
let options : [ String : Any ] = [
48+
"user" : user,
49+
"nouser" : user.isEmpty,
50+
"viewCount" : req.session["viewCount"] ?? 0
51+
]
52+
res.render("form", options)
53+
}
54+
55+
56+
// MARK: - JSON & Cookies
57+
58+
app.get("/json") { _, res, _ in
59+
res.json([
60+
[ "firstname": "Donald", "lastname": "Duck" ],
61+
[ "firstname": "Dagobert", "lastname": "Duck" ]
62+
])
63+
}
64+
65+
app.get("/cookies") { req, res, _ in
66+
// returns all cookies as JSON
67+
res.json(req.cookies)
68+
}
69+
70+
71+
// MARK: - Cows
72+
73+
app.get("/cows") { _, res, _ in
74+
res.send("<html><body><pre>\(cows.vaca())</pre></body></html>")
75+
}
76+
77+
78+
// MARK: - Main page
79+
80+
app.get("/") { req, res, _ in
81+
let tagline = taglines.randomElement()!
82+
83+
let values : [ String : Any ] = [
84+
"tagline" : tagline,
85+
"viewCount" : req.session["viewCount"] ?? 0,
86+
"cowOfTheDay" : cows.vaca()
87+
]
88+
res.render("index", values)
89+
}
90+
91+
92+
// MARK: - Start Server
93+
94+
app.listen(1337) {
95+
console.log("Server listening: \($0)")
96+
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<html>
2+
<head>
3+
<title>Macro</title>
4+
<style>
5+
html { background: #FFFFFF; }
6+
body {
7+
color: #383838;
8+
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
9+
line-height: 1.5rem;
10+
margin: 0 auto;
11+
}
12+
p {
13+
margin-left: 1rem;
14+
margin-right: 1rem;
15+
}
16+
a { text-decoration: none; }
17+
</style>
18+
</head>
19+
<body>
20+
<p>
21+
<center>
22+
<img src="images/logo256x256.png" width="256" height="256" /><br />
23+
<a href="https://github.com/Macro-swift" target="gh">Macro</a>
24+
is brought to you by
25+
<a href="https://zeezide.de/" target="zz">ZeeZide</a>.
26+
</center>
27+
</p>
28+
29+
<p>Bacon ipsum dolor amet pancetta shankle jerky pork loin venison tri-tip, cupim pastrami capicola beef. Kielbasa swine biltong, jerky corned beef pig pork filet mignon fatback chicken hamburger ball tip. Swine pork belly beef ribs spare ribs kevin pancetta shank beef jerky chuck shoulder capicola tongue pork. Alcatra swine ribeye, kielbasa landjaeger corned beef beef ribs venison fatback bresaola andouille capicola pig meatloaf. Pork loin andouille venison ball tip, kielbasa tenderloin beef ribs biltong prosciutto rump. Short ribs flank boudin strip steak tenderloin brisket andouille kielbasa pork loin rump salami fatback. Sausage pork pork belly ball tip chicken biltong tri-tip hamburger cupim tenderloin pancetta.</p>
30+
31+
<p>Corned beef andouille short loin meatloaf. Brisket pork turkey picanha short loin, shank bacon andouille drumstick ham pancetta. Ball tip pork chop chicken, tenderloin chuck jerky tongue short ribs strip steak pig turkey landjaeger frankfurter ribeye pancetta. Kielbasa shank pancetta tongue frankfurter bresaola. Strip steak drumstick sausage landjaeger pork belly tongue venison cupim prosciutto pork beef ham corned beef. Chuck beef landjaeger venison, meatloaf sirloin biltong fatback chicken strip steak pork chop. Ham hock chuck bacon, salami cow pork loin pig beef picanha corned beef brisket doner frankfurter.</p>
32+
</body>
33+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{{#viewCount}}
2+
Views: {{viewCount}}
3+
{{/viewCount}}
4+
<hr />
5+
<div class="row">
6+
<div class="large-12 small-centered columns" style="text-align: center;">
7+
<em>Code from the <a href="http://zeezide.de/">ZeeZide</a>.</em>
8+
</div>
9+
</div>
10+
11+
<script src="http://cdn.jsdelivr.net/jquery/2.2.4/jquery.min.js"></script>
12+
<script src="http://cdn.jsdelivr.net/what-input/2.1.0/what-input.min.js"></script>
13+
<script src="http://cdn.jsdelivr.net/foundation/6.2.3/foundation.min.js"></script>
14+
<script>
15+
$(document).foundation();
16+
</script>
17+
</body>
18+
</html>
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{{> header}}
2+
3+
<form action="/form" method="POST">
4+
<div class="row">
5+
<div class="medium-6 medium-centered columns">
6+
<label>
7+
User
8+
<input name="u" type="text" placeholder="username"
9+
value="{{user}}" />
10+
</label>
11+
<p class="help-text" id="userHelpText"
12+
>Your username should be cool.</p>
13+
</div>
14+
</div>
15+
<div class="row">
16+
<div class="medium-6 medium-centered columns">
17+
<label>
18+
Password <input name="p" type="password" placeholder="password" />
19+
</label>
20+
<p class="help-text" id="passwordHelpText"
21+
>Your password must have at least 1337 Emoji.</p>
22+
</div>
23+
</div>
24+
<div class="row">
25+
<div class="medium-6 medium-centered columns">
26+
<input type="submit" class="button" value="Submit" />
27+
</div>
28+
</div>
29+
</form>
30+
31+
<pre>{{debug}}</pre>
32+
{{> footer}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!doctype html>
2+
<html class="no-js" lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="x-ua-compatible" content="ie=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Macro - {{tagline}}</title>
8+
<link rel="stylesheet"
9+
href="http://cdn.jsdelivr.net/foundation/6.2.3/foundation.min.css" />
10+
<style>
11+
body { padding: 1.5em; }
12+
</style>
13+
</head>
14+
<body>
15+
{{#nouser}}
16+
<div class="callout alert">
17+
<h5>Missing User</h5>
18+
<p>Pleaze provide a username!</p>
19+
</div>
20+
{{/nouser}}
21+
22+
<div class="row">
23+
<div class="large-12 columns">
24+
<div class="panel">
25+
<a href="/"><img align="right" src="images/logo256x256.png"
26+
width="128" height="128" /></a>
27+
<h2>Macro</h2>
28+
<blockquote>{{tagline}}</blockquote>
29+
</div>
30+
</div>
31+
</div>
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{> header}}
2+
3+
<pre style="border: 1px solid #DEDEDE; margin: 1em; padding: 0.5em;"
4+
>{{cowOfTheDay}}</pre>
5+
6+
<div class="menu-centered">
7+
<ul class="menu">
8+
<li><a href="/form">Form</a></li>
9+
<li><a href="https://github.com/Macro-swift">Macro</a></li>
10+
</ul>
11+
</div>
12+
13+
{{> footer}}

0 commit comments

Comments
 (0)