Skip to content

Commit 5877d99

Browse files
committed
docs: split discovery example from receiver example
Signed-off-by: Remi Cattiau <[email protected]>
1 parent 8213c7d commit 5877d99

File tree

2 files changed

+62
-40
lines changed

2 files changed

+62
-40
lines changed

examples/express-ex/discovery.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* eslint-disable */
2+
3+
const express = require("express");
4+
const { DiscoveryService } = require("cloudevents");
5+
const app = express();
6+
const bodyParser = require("body-parser");
7+
app.use(bodyParser.json());
8+
9+
DiscoveryService.registerService({
10+
url: "https://example.com/services/widgetService",
11+
name: "com.example.services.widgetService",
12+
specversions: ["1.0"],
13+
subscriptionurl: "https://events.example.com",
14+
protocols: ["HTTP"],
15+
types: [
16+
{
17+
type: "com.example.widget.create",
18+
},
19+
{
20+
type: "com.example.widget.delete",
21+
},
22+
],
23+
});
24+
DiscoveryService.registerService({
25+
url: "https://example.com/services/catService",
26+
name: "com.example.services.catService",
27+
specversions: ["1.0"],
28+
subscriptionurl: "https://cats.example.com",
29+
protocols: ["HTTP"],
30+
types: [
31+
{
32+
type: "com.example.cats.buy",
33+
},
34+
{
35+
type: "com.example.cats.adopt",
36+
},
37+
{
38+
type: "com.example.cats.feed",
39+
},
40+
{
41+
type: "com.example.cats.give",
42+
},
43+
{
44+
// Just to play with discovery service
45+
type: "com.example.widget.create",
46+
},
47+
],
48+
});
49+
50+
// Serve the discovery service for our services
51+
DiscoveryService.express(app);
52+
// Create a anonymous mapping of cloud events for example
53+
DiscoveryService.express(app, "/anonymous", (name, type, req) => {
54+
// A true life example would have only one call to express and check req for permission
55+
// Only give access to the catService and widgetCreate if anonymous
56+
return name === "com.example.widget.create" || name === "com.example.services.catService";
57+
});
58+
59+
app.listen(3000, () => {
60+
console.log("Example app listening on port 3000!");
61+
});

examples/express-ex/index.js

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,11 @@
11
/* eslint-disable */
22

33
const express = require("express");
4-
const { Receiver, DiscoveryService } = require("cloudevents");
4+
const { Receiver } = require("cloudevents");
55
const app = express();
66
const bodyParser = require("body-parser");
77
app.use(bodyParser.json());
88

9-
DiscoveryService.registerService({
10-
url: "https://example.com/services/widgetService",
11-
name: "com.example.services.widgetService",
12-
specversions: ["1.0"],
13-
subscriptionurl: "https://events.example.com",
14-
protocols: ["HTTP"],
15-
types: [
16-
{
17-
type: "com.example.widget.create",
18-
type: "com.example.widget.delete",
19-
},
20-
],
21-
});
22-
DiscoveryService.registerService({
23-
url: "https://example.com/services/catService",
24-
name: "com.example.services.catService",
25-
specversions: ["1.0"],
26-
subscriptionurl: "https://cats.example.com",
27-
protocols: ["HTTP"],
28-
types: [
29-
{
30-
type: "com.example.cats.buy",
31-
type: "com.example.cats.adopt",
32-
type: "com.example.cats.feed",
33-
type: "com.example.cats.give",
34-
// Just to play with discovery service
35-
type: "com.example.widget.create",
36-
},
37-
],
38-
});
39-
40-
DiscoveryService.express(app);
41-
// Create a anonymous mapping of cloud events for example
42-
DiscoveryService.express(app, "/anonymous", (name, type, req) => {
43-
// A true life example would have only one call to express and check req for permission
44-
// Only give access to the catService and widgetCreate if anonymous
45-
return name === "com.example.widget.create" || name === "com.example.services.catService";
46-
});
47-
489
app.post("/", (req, res) => {
4910
console.log("HEADERS", req.headers);
5011
console.log("BODY", req.body);

0 commit comments

Comments
 (0)