1
1
#!/usr/bin/swift sh
2
-
3
2
import MacroLambda // @Macro-swift
4
3
import cows // @AlwaysRightInstitute
5
4
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
+
6
14
let app = express ( )
7
15
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
+ )
12
22
13
23
// MARK: - Express Settings
14
24
@@ -28,10 +38,10 @@ let taglines = [
28
38
29
39
// MARK: - Form Handling
30
40
31
- app. get ( " /form " ) { _, res, _ in
41
+ app. get ( " /form " ) { _, res in
32
42
res. render ( " form " )
33
43
}
34
- app. post ( " /form " ) { req, res, _ in
44
+ app. post ( " /form " ) { req, res in
35
45
let user = req. body [ string: " u " ]
36
46
console. log ( " USER IS: \( user) " )
37
47
@@ -46,29 +56,29 @@ app.post("/form") { req, res, _ in
46
56
47
57
// MARK: - JSON & Cookies
48
58
49
- app. get ( " /json " ) { _, res, _ in
59
+ app. get ( " /json " ) { _, res in
50
60
res. json ( [
51
61
[ " firstname " : " Donald " , " lastname " : " Duck " ] ,
52
62
[ " firstname " : " Dagobert " , " lastname " : " Duck " ]
53
63
] )
54
64
}
55
65
56
- app. get ( " /cookies " ) { req, res, _ in
66
+ app. get ( " /cookies " ) { req, res in
57
67
// returns all cookies as JSON
58
68
res. json ( req. cookies)
59
69
}
60
70
61
71
62
72
// MARK: - Cows
63
73
64
- app. get ( " /cows " ) { _, res, _ in
74
+ app. get ( " /cows " ) { _, res in
65
75
res. send ( " <html><body><pre> \( cows. vaca ( ) ) </pre></body></html> " )
66
76
}
67
77
68
78
69
79
// MARK: - Main page
70
80
71
- app. get ( " / " ) { req, res, _ in
81
+ app. get ( " / " ) { req, res in
72
82
let tagline = taglines. randomElement ( ) !
73
83
74
84
let values : [ String : Any ] = [
0 commit comments