Skip to content

Commit c56dc3e

Browse files
committed
Merge branch 'feature/todomvc-1' into develop
2 parents f32fc1b + 0033bdd commit c56dc3e

File tree

14 files changed

+358
-23
lines changed

14 files changed

+358
-23
lines changed

Package.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ let package = Package(
1111
],
1212

1313
products: [
14-
.executable(name: "httpd-helloworld", targets: [ "httpd-helloworld" ]),
15-
.executable(name: "connect-static", targets: [ "connect-static" ]),
16-
.executable(name: "express-simple", targets: [ "express-simple" ]),
17-
.executable(name : "express-simple-lambda",
14+
.executable(name: "httpd-helloworld", targets: [ "httpd-helloworld" ]),
15+
.executable(name: "connect-static", targets: [ "connect-static" ]),
16+
.executable(name: "express-simple", targets: [ "express-simple" ]),
17+
.executable(name: "todomvc", targets: [ "todomvc" ]),
18+
.executable(name : "express-simple-lambda",
1819
targets : [ "express-simple-lambda" ])
1920
],
2021

2122
dependencies: [
2223
// A lot of packages for demonstration purposes, only add what you
2324
// actually need in your own project.
2425
.package(url: "https://github.com/Macro-swift/Macro.git",
25-
from: "0.6.2"),
26+
from: "0.8.7"),
2627
.package(url: "https://github.com/Macro-swift/MacroExpress.git",
27-
from: "0.6.1"),
28+
from: "0.8.4"),
2829
.package(url: "https://github.com/Macro-swift/MacroLambda.git",
2930
from: "0.2.1"),
3031
.package(url: "https://github.com/AlwaysRightInstitute/cows",
@@ -35,7 +36,8 @@ let package = Package(
3536
.target(name: "httpd-helloworld", dependencies: [ "Macro" ]),
3637
.target(name: "connect-static", dependencies: [ "MacroExpress" ]),
3738
.target(name: "express-simple", dependencies: [ "MacroExpress", "cows" ]),
38-
39+
.target(name: "todomvc", dependencies: [ "MacroExpress" ]),
40+
3941
.target(name: "express-simple-lambda",
4042
dependencies: [ "MacroLambda", "cows" ])
4143
]

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ This repository contains examples for
1313

1414
### Running Examples as Scripts
1515

16-
Examples can be run as scripts using [swift-sh](https://github.com/mxcl/swift-sh)
17-
(can be installed using a simple `brew install mxcl/made/swift-sh`).
16+
Single file examples can be either run as scripts using
17+
[swift-sh](https://github.com/mxcl/swift-sh)
18+
(install using a simple `brew install mxcl/made/swift-sh`),
19+
from within Xcode or via `swift run`.
1820

1921
This also contains an example for deploying to AWS Lambda:
2022
[express-simple-lambda](Sources/express-simple-lambda/).
2123

24+
2225
## Examples
2326

24-
### httpd-helloworld
27+
### [httpd-helloworld](Sources/httpd-helloworld)
2528

2629
Raw HTTP server w/o Express extras (middleware, templates).
2730

@@ -34,7 +37,7 @@ Single source file:
3437
```swift
3538
#!/usr/bin/swift sh
3639

37-
import Macro // @Macro-swift ~> 0.0.12
40+
import Macro // @Macro-swift
3841

3942
http.createServer { req, res in
4043
// log request
@@ -60,7 +63,7 @@ http.createServer { req, res in
6063
}
6164
```
6265

63-
### express-simple
66+
### [express-simple](Sources/express-simple)
6467

6568
```bash
6669
$ Sources/express-simple/main.swift
@@ -75,8 +78,8 @@ Forms, cookies, JSON, sessions, templates and cows - you get it all!
7578
```swift
7679
#!/usr/bin/swift sh
7780

78-
import MacroExpress // @Macro-swift ~> 0.0.4
79-
import cows // @AlwaysRightInstitute ~> 1.0.0
81+
import MacroExpress // @Macro-swift
82+
import cows // @AlwaysRightInstitute
8083

8184
let app = express()
8285

@@ -148,7 +151,7 @@ Looks like this:
148151
There is an AWS Lambda variant of this (with minor adjustments):
149152
[express-simple-lambda](Sources/express-simple-lambda/).
150153

151-
### connect-simple
154+
### [connect-simple](Sources/connect-simple)
152155

153156
Use the simpler `connect` module, instead of `express`.
154157
Probably no need to do this in the real world, just use MacroExpress.
@@ -160,6 +163,15 @@ $ Sources/express-simple/main.swift
160163
2020-06-03T14:58:59+0200 notice: Server listening on http://localhost:1337
161164
```
162165

166+
### [todomvc](Sources/todomvc/)
167+
168+
A MacroExpress implementation of a [Todo-Backend](http://todobackend.com/),
169+
a simple JSON API to access and modify a list of todos.
170+
171+
```bash
172+
$ swift run todomvc
173+
2020-06-03T14:58:59+0200 notice: Server listening on http://localhost:1337
174+
```
163175

164176

165177
## Who

Sources/connect-static/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h2>Macro.swift connect-static
2-
<img src="http://zeezide.com/img/MicroExpressIcon1024.png"
3-
align="right" width="100" height="100" />
2+
<img src="http://zeezide.com/img/macro/MacroExpressIcon128.png"
3+
align="right" width="100" height="100" />
44
</h2>
55

66
Demonstrates the use of the `connect.serveStatic` middleware.

Sources/connect-static/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/swift sh
22

3-
import MacroExpress // @Macro-swift ~> 0.5.4
3+
import MacroExpress // @Macro-swift
44

55
let dirname = __dirname()
66

Sources/express-simple-lambda/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/swift sh
22

3-
import MacroLambda // @Macro-swift ~> 0.1.1
4-
import cows // @AlwaysRightInstitute ~> 1.0.0
3+
import MacroLambda // @Macro-swift
4+
import cows // @AlwaysRightInstitute
55

66
let app = express()
77

Sources/express-simple/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/swift sh
22

3-
import MacroExpress // @Macro-swift ~> 0.5.4
4-
import cows // @AlwaysRightInstitute ~> 1.0.0
3+
import MacroExpress // @Macro-swift
4+
import cows // @AlwaysRightInstitute
55

66
let app = express()
77

Sources/httpd-helloworld/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<h2>Macro.swift httpd-helloworld
2-
<img src="http://zeezide.com/img/MicroExpressIcon1024.png"
2+
<img src="http://zeezide.com/img/macro/MacroExpressIcon128.png"
33
align="right" width="100" height="100" />
44
</h2>
55

Sources/todomvc/Model.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Model.swift
3+
// Noze.io / mod_swift / MacroExpress
4+
//
5+
// Created by Helge Heß on 6/3/16.
6+
// Copyright © 2016-2021 ZeeZide GmbH. All rights reserved.
7+
//
8+
9+
struct Todo: Encodable {
10+
// Note: Decodable doesn't work for partial objects and has little to no
11+
// value for the Todo-Backend API.
12+
13+
var id : Int
14+
var title : String
15+
var completed : Bool
16+
var order : Int
17+
18+
var url : String { return "\(ourAPI)todos/\(id)" }
19+
20+
21+
// Codable is a train wreck. We need all this boilerplate just to emit the
22+
// extra `url`.
23+
24+
enum CodingKeys: String, CodingKey {
25+
case id, title, completed, order, url
26+
}
27+
func encode(to encoder: Encoder) throws {
28+
var values = encoder.container(keyedBy: CodingKeys.self)
29+
try values.encode(url, forKey: .url)
30+
try values.encode(id, forKey: .id)
31+
try values.encode(title, forKey: .title)
32+
try values.encode(completed, forKey: .completed)
33+
try values.encode(order, forKey: .order)
34+
}
35+
}

Sources/todomvc/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<h2>MacroExpress TodoMVC
2+
<img src="http://zeezide.com/img/macro/MacroExpressIcon128.png"
3+
align="right" width="100" height="100" />
4+
</h2>
5+
6+
An implementation of a [Todo-Backend](http://todobackend.com/),
7+
a simple JSON API to access and modify a list of todos.
8+
9+
10+
## Running the Sample
11+
12+
Use `swift run` or run the sample from within Xcode:
13+
```
14+
$ swift run todomvc
15+
2021-01-30T15:51:41+0100 notice μ.express.app : Listening on 8042
16+
```
17+
18+
### Who
19+
20+
**Macro** is brought to you by
21+
the
22+
[Always Right Institute](http://www.alwaysrightinstitute.com)
23+
and
24+
[ZeeZide](http://zeezide.de).
25+
We like
26+
[feedback](https://twitter.com/ar_institute),
27+
GitHub stars,
28+
cool [contract work](http://zeezide.com/en/services/services.html),
29+
presumably any form of praise you can think of.
30+
31+
There is a `#microexpress` channel on the
32+
[Noze.io Slack](http://slack.noze.io/). Feel free to join!
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// Storage.swift
3+
// Noze.io / mod_swift / MacroExpress
4+
//
5+
// Created by Helge Hess on 03/06/16.
6+
// Copyright © 2016-2021 ZeeZide GmbH. All rights reserved.
7+
//
8+
9+
/// A collection store which just stores everything in memory. Data added will
10+
/// be gone when the process is stopped.
11+
///
12+
/// Note: The operations in here are all synchronous for simplicity. This is not
13+
/// how a regular store would usually work.
14+
/// Check out todo-mvc-redis for a store with async operations.
15+
///
16+
class VolatileStoreCollection<T> {
17+
18+
var sequence = 1337
19+
var changeCounter = 0
20+
21+
var objects = [ Int : T ]()
22+
23+
init() {}
24+
25+
func nextKey() -> Int {
26+
sequence += 1
27+
return sequence
28+
}
29+
30+
func getAll() -> [ T ] {
31+
return Array(objects.values)
32+
}
33+
34+
func get(ids keys: [ Int ]) -> [ T ] {
35+
var matches = [T]()
36+
for key in keys {
37+
if let object = objects[key] {
38+
matches.append(object)
39+
}
40+
}
41+
return matches
42+
}
43+
44+
func get(id key: Int) -> T? {
45+
return objects[key]
46+
}
47+
48+
func delete(id key: Int) {
49+
changeCounter += 1
50+
objects.removeValue(forKey: key)
51+
}
52+
53+
func update(id key: Int, value v: T) {
54+
changeCounter += 1
55+
objects[key] = v // value type!
56+
}
57+
58+
func deleteAll() {
59+
changeCounter += 1
60+
objects.removeAll()
61+
}
62+
}

0 commit comments

Comments
 (0)