Skip to content

Commit 401d2ed

Browse files
committed
Merge branch 'develop'
# Conflicts: # Sources/connect-static/main.swift
2 parents e298c40 + fa58f76 commit 401d2ed

File tree

16 files changed

+136
-91
lines changed

16 files changed

+136
-91
lines changed

.github/workflows/swift.yml

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
pull_request:
66
schedule:
7-
- cron: "38 9 * * 1"
7+
- cron: "38 9 * * 2"
88

99
jobs:
1010
linux:
@@ -13,14 +13,13 @@ jobs:
1313
fail-fast: false
1414
matrix:
1515
image:
16-
- swift:5.5.3-xenial
17-
- swift:5.6.1-bionic
18-
- swift:5.7.2-focal
19-
- swift:5.8-jammy
16+
- swift:5.9.2-focal
17+
- swift:5.10-jammy
18+
- swift:6.0-noble
2019
container: ${{ matrix.image }}
2120
steps:
2221
- name: Checkout Repository
23-
uses: actions/checkout@v3
22+
uses: actions/checkout@v4
2423
- name: Build Swift Debug Package
2524
run: swift build -c debug
2625
- name: Build Swift Release Package
@@ -31,9 +30,9 @@ jobs:
3130
- name: Select latest available Xcode
3231
uses: maxim-lobanov/[email protected]
3332
with:
34-
xcode-version: 13.2.1
33+
xcode-version: latest
3534
- name: Checkout Repository
36-
uses: actions/checkout@v3
35+
uses: actions/checkout@v4
3736
- name: Build Swift Debug Package
3837
run: swift build -c debug
3938
- name: Build Swift Release Package

.gitignore

+2
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

+2-2
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

+9-16
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

-3
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

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +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.
16+
let dirname = __dirname()
17+
18+
19+
// "connect" here provides an object which is used to attach middleware to the
20+
// Macro HTTP server.
521
let app = connect()
622

23+
// A builtin middleware that just logs incoming requests and the generated
24+
// responses. "dev" is a logging config (others are "default", "short", "tiny").
725
app.use(logger("dev"))
8-
app.use(serveStatic(__dirname() + "/public"))
926

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.
30+
app.use(serveStatic(dirname + "/public"))
31+
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`).
1039
app.use { req, res, next in
1140
guard req.url == "/" else { return next() }
1241
res.redirect("/index.html")
1342
}
1443

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

Sources/express-simple-lambda/README.md

+7-7
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

+21-11
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

+22-12
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

-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ http.createServer { req, res in
6363
### Who
6464

6565
**Macro** is brought to you by
66-
the
67-
[Always Right Institute](http://www.alwaysrightinstitute.com)
68-
and
6966
[ZeeZide](http://zeezide.de).
7067
We like
7168
[feedback](https://twitter.com/ar_institute),

0 commit comments

Comments
 (0)