11#!/usr/bin/swift sh
2-
32import MacroLambda // @Macro-swift
43import 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+
614let 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 ] = [
0 commit comments