Skip to content

Commit d7e4950

Browse files
committed
docs: split discovery example from receiver example
Signed-off-by: Remi Cattiau <remi@cattiau.com>
1 parent 8213c7d commit d7e4950

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed

examples/express-ex/discovery.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
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+
// Serve the discovery service for our services
41+
DiscoveryService.express(app);
42+
// Create a anonymous mapping of cloud events for example
43+
DiscoveryService.express(app, "/anonymous", (name, type, req) => {
44+
// A true life example would have only one call to express and check req for permission
45+
// Only give access to the catService and widgetCreate if anonymous
46+
return name === "com.example.widget.create" || name === "com.example.services.catService";
47+
});
48+
49+
app.listen(3000, () => {
50+
console.log("Example app listening on port 3000!");
51+
});

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)