Skip to content

Commit c4ccf8a

Browse files
committed
Use the new next-less API
Also update the comments and READMEs a little.
1 parent 1097f88 commit c4ccf8a

File tree

15 files changed

+126
-82
lines changed

15 files changed

+126
-82
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,6 @@ xcuserdata
9595
.docker.build
9696
.swiftpm
9797
.vscode
98+
.DS_Store
99+
*.swp
98100

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ let package = Package(
2525
// A lot of packages for demonstration purposes, only add what you
2626
// actually need in your own project.
2727
.package(url: "https://github.com/Macro-swift/Macro.git",
28-
from: "1.0.0"),
28+
from: "1.0.1"),
2929
.package(url: "https://github.com/Macro-swift/MacroExpress.git",
30-
from: "1.0.0"),
30+
from: "1.0.2"),
3131
.package(url: "https://github.com/Macro-swift/MacroLambda.git",
3232
from: "0.5.0"),
3333
.package(url: "https://github.com/AlwaysRightInstitute/cows",

README.md

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,14 @@ Single source file:
4040
import Macro // @Macro-swift
4141

4242
http.createServer { req, res in
43-
// log request
4443
console.log("\(req.method) \(req.url)")
45-
46-
// set content type to HTML
4744
res.writeHead(200, [ "Content-Type": "text/html" ])
48-
49-
// write some HTML
5045
res.write("<h1>Hello Client: \(req.url)</h1>")
51-
5246
res.write("<table><tbody>")
5347
for ( key, value ) in req.headers {
5448
res.write("<tr><td><nobr>\(key)</nobr></td><td>\(value)</td></tr>")
5549
}
5650
res.write("</tbody></table>")
57-
58-
// finish up
5951
res.end()
6052
}
6153
.listen(1337) { server in
@@ -82,12 +74,13 @@ import MacroExpress // @Macro-swift
8274
import cows // @AlwaysRightInstitute
8375

8476
let app = express()
85-
86-
app.use(logger("dev"))
87-
app.use(bodyParser.urlencoded())
88-
app.use(cookieParser())
89-
app.use(session())
90-
app.use(serveStatic(__dirname() + "/public"))
77+
app.use(
78+
logger("dev"),
79+
bodyParser.urlencoded(),
80+
cookieParser(),
81+
session(),
82+
serveStatic(__dirname() + "/public")
83+
)
9184

9285
// MARK: - Express Settings
9386

@@ -105,13 +98,13 @@ app.use { req, _, next in
10598

10699
// MARK: - Cows
107100

108-
app.get("/cows") { _, res, _ in
101+
app.get("/cows") { req, res in
109102
res.send("<html><body><pre>\(cows.vaca())</pre></body></html>")
110103
}
111104

112105
// MARK: - Main page
113106

114-
app.get("/") { req, res, _ in
107+
app.get("/") { req, res in
115108
let tagline = taglines.randomElement()!
116109

117110
let values : [ String : Any ] = [

Sources/connect-static/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ $ swift run connect-static
2525
### Who
2626

2727
**Macro** is brought to you by
28-
the
29-
[Always Right Institute](http://www.alwaysrightinstitute.com)
30-
and
3128
[ZeeZide](http://zeezide.de).
3229
We like
3330
[feedback](https://twitter.com/ar_institute),

Sources/connect-static/main.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,48 @@
11
#!/usr/bin/swift sh
2-
32
import MacroExpress // @Macro-swift
43

4+
// As with all "Macro*", this tries to replicate the Node.js APIs for Swift.
5+
//
6+
// "Connect" is a middleground between just "Macro" (which provides a basic
7+
// HTTP server) and "Express", which adds a lot on top of Connect.
8+
// It provides a Middleware router, but not the mountable Route objects Express
9+
// has.
10+
11+
12+
// `__dirname` gives the directory location of the current Swift file, or
13+
// fallbacks for binary setups.
14+
// Here it is used to lookup static resources that live in the "public"
15+
// directory alongside the Swift sources.
516
let dirname = __dirname()
617

18+
19+
// "connect" here provides an object which is used to attach middleware to the
20+
// Macro HTTP server.
721
let app = connect()
822

23+
// A builtin middleware that just logs incoming requests and the generated
24+
// responses. "dev" is a logging config (others are "default", "short", "tiny").
925
app.use(logger("dev"))
26+
27+
// The builtin `serveStatic` middleware directly serves static resources to the
28+
// browser. If it requests `/public/hello.gif`, this would serve the `hello.gif`
29+
// file sitting in the `public` directory alongside the Swift sources.
1030
app.use(serveStatic(__dirname() + "/public"))
1131

32+
// The only user middleware provided by this example - a simple redirect. If
33+
// the browser hits `/` on the server, this will redirect to `/index.html`.
34+
// In all other cases, it will continue processing (by calling the `next`
35+
// function, which essentially says "I don't handle this, continue with the
36+
// next middleware attached" - while will be the default 404).
37+
// Note that this middleware runs after the `serveStatic` above, which already
38+
// handles all static files in the `public` directory (including `index.html`).
1239
app.use { req, res, next in
1340
guard req.url == "/" else { return next() }
1441
res.redirect("/index.html")
1542
}
1643

44+
// This starts the webserver on port 1337. It will process the middleware
45+
// attached using the functions above.
1746
app.listen(1337) {
1847
console.log("Server listening on http://localhost:1337")
1948
}

Sources/express-simple-lambda/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ A few adjustments had to be made:
6969
```swift
7070
#!/usr/bin/swift sh
7171

72-
import MacroLambda // @Macro-swift ~> 0.1.3
72+
import MacroLambda // @Macro-swift ~> 0.5.0
7373
import cows // @AlwaysRightInstitute ~> 1.0.0
7474

7575
let app = express()
@@ -93,10 +93,10 @@ let taglines = [
9393

9494
// MARK: - Form Handling
9595

96-
app.get("/form") { _, res, _ in
96+
app.get("/form") { _, res in
9797
res.render("form")
9898
}
99-
app.post("/form") { req, res, _ in
99+
app.post("/form") { req, res in
100100
let user = req.body[string: "u"]
101101
console.log("USER IS: \(user)")
102102

@@ -111,28 +111,28 @@ app.post("/form") { req, res, _ in
111111

112112
// MARK: - JSON & Cookies
113113

114-
app.get("/json") { _, res, _ in
114+
app.get("/json") { _, res in
115115
res.json([
116116
[ "firstname": "Donald", "lastname": "Duck" ],
117117
[ "firstname": "Dagobert", "lastname": "Duck" ]
118118
])
119119
}
120120

121-
app.get("/cookies") { req, res, _ in
121+
app.get("/cookies") { req, res in
122122
// returns all cookies as JSON
123123
res.json(req.cookies)
124124
}
125125

126126

127127
// MARK: - Cows
128128

129-
app.get("/cows") { _, res, _ in
129+
app.get("/cows") { _, res in
130130
res.send("<html><body><pre>\(cows.vaca())</pre></body></html>")
131131
}
132132

133133
// MARK: - Main page
134134

135-
app.get("/") { req, res, _ in
135+
app.get("/") { req, res in
136136
let tagline = taglines.randomElement()!
137137

138138
let values : [ String : Any ] = [

Sources/express-simple-lambda/main.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
#!/usr/bin/swift sh
2-
32
import MacroLambda // @Macro-swift
43
import cows // @AlwaysRightInstitute
54

5+
// As with all "Macro*", this tries to replicate the Node.js APIs for Swift.
6+
//
7+
// This is a little more complex Express.js like example specifically for
8+
// MacroLambda, aka running Macro in AWS Lambda. Featuring
9+
// - cooking parsing,
10+
// - static resource delivery,
11+
// - Mustache template rendering,
12+
// - Express middleware.
13+
614
let app = express()
715

8-
app.use(logger("dev"))
9-
app.use(bodyParser.urlencoded())
10-
app.use(cookieParser())
11-
app.use(serveStatic(__dirname() + "/public"))
16+
app.use(
17+
logger("dev"),
18+
bodyParser.urlencoded(),
19+
cookieParser(),
20+
serveStatic(__dirname() + "/public")
21+
)
1222

1323
// MARK: - Express Settings
1424

@@ -28,10 +38,10 @@ let taglines = [
2838

2939
// MARK: - Form Handling
3040

31-
app.get("/form") { _, res, _ in
41+
app.get("/form") { _, res in
3242
res.render("form")
3343
}
34-
app.post("/form") { req, res, _ in
44+
app.post("/form") { req, res in
3545
let user = req.body[string: "u"]
3646
console.log("USER IS: \(user)")
3747

@@ -46,29 +56,29 @@ app.post("/form") { req, res, _ in
4656

4757
// MARK: - JSON & Cookies
4858

49-
app.get("/json") { _, res, _ in
59+
app.get("/json") { _, res in
5060
res.json([
5161
[ "firstname": "Donald", "lastname": "Duck" ],
5262
[ "firstname": "Dagobert", "lastname": "Duck" ]
5363
])
5464
}
5565

56-
app.get("/cookies") { req, res, _ in
66+
app.get("/cookies") { req, res in
5767
// returns all cookies as JSON
5868
res.json(req.cookies)
5969
}
6070

6171

6272
// MARK: - Cows
6373

64-
app.get("/cows") { _, res, _ in
74+
app.get("/cows") { _, res in
6575
res.send("<html><body><pre>\(cows.vaca())</pre></body></html>")
6676
}
6777

6878

6979
// MARK: - Main page
7080

71-
app.get("/") { req, res, _ in
81+
app.get("/") { req, res in
7282
let tagline = taglines.randomElement()!
7383

7484
let values : [ String : Any ] = [

Sources/express-simple/main.swift

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
#!/usr/bin/swift sh
2-
32
import MacroExpress // @Macro-swift
43
import cows // @AlwaysRightInstitute
54

5+
// As with all "Macro*", this tries to replicate the Node.js APIs for Swift.
6+
//
7+
// This is a little more complex Express.js like example, featuring
8+
// - cooking parsing,
9+
// - session handling,
10+
// - static resource delivery,
11+
// - Mustache template rendering,
12+
// - Express middleware.
13+
614
let app = express()
715

8-
app.use(logger("dev"))
9-
app.use(bodyParser.urlencoded())
10-
app.use(cookieParser())
11-
app.use(session())
12-
app.use(serveStatic(__dirname() + "/public"))
16+
app.use(
17+
logger("dev"),
18+
bodyParser.urlencoded(),
19+
cookieParser(),
20+
session(),
21+
serveStatic(__dirname() + "/public")
22+
)
1323

1424
// MARK: - Express Settings
1525

@@ -37,10 +47,10 @@ let taglines = [
3747

3848
// MARK: - Form Handling
3949

40-
app.get("/form") { _, res, _ in
50+
app.get("/form") { _, res in
4151
res.render("form")
4252
}
43-
app.post("/form") { req, res, _ in
53+
app.post("/form") { req, res in
4454
let user = req.body[string: "u"]
4555
console.log("USER IS: \(user)")
4656

@@ -55,29 +65,29 @@ app.post("/form") { req, res, _ in
5565

5666
// MARK: - JSON & Cookies
5767

58-
app.get("/json") { _, res, _ in
68+
app.get("/json") { _, res in
5969
res.json([
6070
[ "firstname": "Donald", "lastname": "Duck" ],
6171
[ "firstname": "Dagobert", "lastname": "Duck" ]
6272
])
6373
}
6474

65-
app.get("/cookies") { req, res, _ in
75+
app.get("/cookies") { req, res in
6676
// returns all cookies as JSON
6777
res.json(req.cookies)
6878
}
6979

7080

7181
// MARK: - Cows
7282

73-
app.get("/cows") { _, res, _ in
83+
app.get("/cows") { _, res in
7484
res.send("<html><body><pre>\(cows.vaca())</pre></body></html>")
7585
}
7686

7787

7888
// MARK: - Main page
7989

80-
app.get("/") { req, res, _ in
90+
app.get("/") { req, res in
8191
let tagline = taglines.randomElement()!
8292

8393
let values : [ String : Any ] = [

Sources/httpd-helloworld/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ $ swift run httpd-helloworld
3333
### Who
3434

3535
**Macro** is brought to you by
36-
the
37-
[Always Right Institute](http://www.alwaysrightinstitute.com)
38-
and
3936
[ZeeZide](http://zeezide.de).
4037
We like
4138
[feedback](https://twitter.com/ar_institute),

Sources/httpd-helloworld/main.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
#!/usr/bin/swift sh
2-
32
import Macro // @Macro-swift
43

4+
// As with all "Macro*", this tries to replicate the Node.js APIs for Swift.
5+
//
6+
// This is the lowest level HTTP server interface in Macro and is suitable for
7+
// very basic HTTP servers that have no real routing needs.
8+
// Most apps will rather use MacroExpress (or Connect as a middleground).
9+
510
http.createServer { req, res in
6-
// log request
11+
// Log request
712
req.log.log("\(req.method) \(req.url)")
813

9-
// set content type to HTML
14+
// Set the response status to 200-OK and the "Content-Type" header to HTML
1015
res.writeHead(200, [ "Content-Type": "text/html" ])
1116

12-
// write some HTML
17+
// Write some HTML
1318
res.write("<h1>Hello Client: \(req.url)</h1>")
1419

1520
res.write("<table><tbody>")
@@ -18,7 +23,8 @@ http.createServer { req, res in
1823
}
1924
res.write("</tbody></table>")
2025

21-
// finish up
26+
// Mark the response as done, it is important to always call `end` at some
27+
// point to finish processing.
2228
res.end()
2329
}
2430
.listen(1337) { server in

0 commit comments

Comments
 (0)