Remove server stuff
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
46dd70563d
commit
3dad0d200c
50
README.md
50
README.md
|
@ -174,35 +174,8 @@ See how to listen to events using
|
||||||
[express](https://github.com/expressjs/express).
|
[express](https://github.com/expressjs/express).
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
var Unmarshaller02 = require("cloudevents-sdk/http/unmarshaller/v02");
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Receiving Using our Simple HTTP Server
|
|
||||||
|
|
||||||
See how to listen to events using out simple http server.
|
|
||||||
|
|
||||||
```js
|
|
||||||
var Cloudevent = require("cloudevents-sdk");
|
|
||||||
|
|
||||||
// The binding configuration
|
|
||||||
var config = {
|
|
||||||
path : "/events",
|
|
||||||
port : 10300,
|
|
||||||
method : "POST"
|
|
||||||
};
|
|
||||||
|
|
||||||
// The binding instance
|
|
||||||
var binding = new Cloudevent.bindings["http-structured0.2"](config);
|
|
||||||
|
|
||||||
binding.listen()
|
|
||||||
.then(cloudevent => {
|
|
||||||
// do something with event
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
// deal with errors
|
|
||||||
console.error(err);
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Repository Structure
|
## Repository Structure
|
||||||
|
@ -357,27 +330,6 @@ Receiver.check(Object, Map)
|
||||||
Cloudevent Receiver.parse(Object, Map)
|
Cloudevent Receiver.parse(Object, Map)
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Receiver Binding Server
|
|
||||||
|
|
||||||
Use this API to start a HTTP server and listen to events.
|
|
||||||
|
|
||||||
```js
|
|
||||||
/*
|
|
||||||
* The constructor must receives the map of configurations.
|
|
||||||
*/
|
|
||||||
Receiver(config)
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Listen to events and returns a Promise.
|
|
||||||
*/
|
|
||||||
Promise Receiver.listen()
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Stops the listen to events
|
|
||||||
*/
|
|
||||||
Receiver.stop()
|
|
||||||
```
|
|
||||||
|
|
||||||
### `Unmarshaller` classes
|
### `Unmarshaller` classes
|
||||||
|
|
||||||
The Unmarshaller classes uses the receiver API, abstracting the formats:
|
The Unmarshaller classes uses the receiver API, abstracting the formats:
|
||||||
|
|
|
@ -1,102 +0,0 @@
|
||||||
var http = require("http");
|
|
||||||
var Spec02 = require("../../../specs/spec_0_2.js")
|
|
||||||
var spec02 = new Spec02();
|
|
||||||
|
|
||||||
const allowedContentTypes = [];
|
|
||||||
allowedContentTypes.push("application/cloudevents+json; charset=utf-8");
|
|
||||||
|
|
||||||
function is_valid_http_request(req, res, config) {
|
|
||||||
var valid = true;
|
|
||||||
|
|
||||||
if(req.url === config.path
|
|
||||||
&& req.method.toLowerCase()
|
|
||||||
=== config.method.toLowerCase()) {
|
|
||||||
|
|
||||||
if(!req.headers["content-type"]
|
|
||||||
|| !allowedContentTypes.includes(
|
|
||||||
req.headers["content-type"].toLowerCase())){
|
|
||||||
res.statusCode = 400;
|
|
||||||
res.end("Bad Request");
|
|
||||||
|
|
||||||
valid = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if(req.url !== config.path) {
|
|
||||||
res.statusCode = 404;
|
|
||||||
res.end("Not Found");
|
|
||||||
|
|
||||||
valid = false;
|
|
||||||
} else {
|
|
||||||
res.statusCode = 405;
|
|
||||||
res.end("Method Not Allowed");
|
|
||||||
|
|
||||||
valid = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return valid;
|
|
||||||
}
|
|
||||||
|
|
||||||
function HTTPStructured(configuration){
|
|
||||||
this.config = configuration;
|
|
||||||
|
|
||||||
if(!this.config["path"]){
|
|
||||||
this.config["path"] = "/";
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!this.config["method"]){
|
|
||||||
this.config["method"] = "POST";
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!this.config["interface"]){
|
|
||||||
this.config["interface"] = "0.0.0.0";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
HTTPStructured.prototype.listen = function(){
|
|
||||||
this.server;
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
self.server =
|
|
||||||
http.createServer((request, res) => {
|
|
||||||
if(is_valid_http_request(request, res, this.config)){
|
|
||||||
var body = [];
|
|
||||||
request.on("error", err => {
|
|
||||||
console.error(err);
|
|
||||||
})
|
|
||||||
.on("data", chunk => {
|
|
||||||
// accumulate the chunks
|
|
||||||
body.push(chunk);
|
|
||||||
})
|
|
||||||
.on("end", () => {
|
|
||||||
body = Buffer.concat(body).toString();
|
|
||||||
var jsonBody = JSON.parse(body);
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Process/validate the body
|
|
||||||
spec02.check(jsonBody);
|
|
||||||
|
|
||||||
res.statusCode = 201;
|
|
||||||
res.end("Event Accepted");
|
|
||||||
}catch(e) {
|
|
||||||
res.statusCode = 400;
|
|
||||||
res.end(JSON.stringify(e));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
self.server.listen(this.config.port, this.config.interface, (err) => {
|
|
||||||
if(err){
|
|
||||||
console.error(err);
|
|
||||||
reject(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
HTTPStructured.prototype.stop = function() {
|
|
||||||
this.server.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = HTTPStructured;
|
|
|
@ -1,8 +1,6 @@
|
||||||
var expect = require("chai").expect;
|
var expect = require("chai").expect;
|
||||||
var Cloudevent = require("../index.js");
|
var Cloudevent = require("../index.js");
|
||||||
var nock = require("nock");
|
var nock = require("nock");
|
||||||
var ReceiverStructured01 =
|
|
||||||
require("../lib/bindings/http/server/structured_0_2.js");
|
|
||||||
var http = require("http");
|
var http = require("http");
|
||||||
var request = require("request");
|
var request = require("request");
|
||||||
var Spec02 = require("../lib/specs/spec_0_2.js");
|
var Spec02 = require("../lib/specs/spec_0_2.js");
|
||||||
|
@ -81,99 +79,6 @@ describe("HTTP Transport Binding - Version 0.2", () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Receiver", () => {
|
|
||||||
var receiver;
|
|
||||||
before(() => {
|
|
||||||
// setup
|
|
||||||
receiver = new ReceiverStructured01(receiverConfig);
|
|
||||||
receiver.listen()
|
|
||||||
.then(response => {
|
|
||||||
console.log(response);
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
console.error(err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
after(() => {
|
|
||||||
receiver.stop();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Should return 404 when path is wrong", () => {
|
|
||||||
// act
|
|
||||||
request.post("http://localhost:" + receiverConfig.port + "/foobar",
|
|
||||||
(err, res, body) => {
|
|
||||||
// assert
|
|
||||||
expect(res.statusCode).to.equal(404);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Should return 405 when method is wrong", () => {
|
|
||||||
// act
|
|
||||||
request.get("http://localhost:" + receiverConfig.port
|
|
||||||
+ receiverConfig.path,
|
|
||||||
(err, res, body) => {
|
|
||||||
// assert
|
|
||||||
expect(res.statusCode).to.equal(405);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Should return 400 when Content-Type is wrong", () => {
|
|
||||||
// act
|
|
||||||
request.post("http://localhost:" + receiverConfig.port
|
|
||||||
+ receiverConfig.path,
|
|
||||||
(err, res, body) => {
|
|
||||||
// assert
|
|
||||||
expect(res.statusCode).to.equal(400);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Should return 400 when is not a cloudevent", () => {
|
|
||||||
// setup
|
|
||||||
var requestOptions = {
|
|
||||||
url : "http://localhost:" + receiverConfig.port
|
|
||||||
+ receiverConfig.path,
|
|
||||||
method : "POST",
|
|
||||||
headers : {
|
|
||||||
"Content-Type":"application/cloudevents+json; charset=utf-8"
|
|
||||||
},
|
|
||||||
body : JSON.stringify({"foo": "bar"})
|
|
||||||
};
|
|
||||||
|
|
||||||
// act
|
|
||||||
request(requestOptions,
|
|
||||||
(err, res, body) => {
|
|
||||||
// assert
|
|
||||||
expect(res.statusCode).to.equal(400);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("Should return 201 when accepts the event", () => {
|
|
||||||
// setup
|
|
||||||
var ce_spec02 = new Spec02();
|
|
||||||
ce_spec02
|
|
||||||
.type(type)
|
|
||||||
.source(source);
|
|
||||||
|
|
||||||
var requestOptions = {
|
|
||||||
url : "http://localhost:" + receiverConfig.port
|
|
||||||
+ receiverConfig.path,
|
|
||||||
method : "POST",
|
|
||||||
headers : {
|
|
||||||
"Content-Type":"application/cloudevents+json; charset=utf-8"
|
|
||||||
},
|
|
||||||
body : JSON.stringify(ce_spec02.payload)
|
|
||||||
};
|
|
||||||
|
|
||||||
// act
|
|
||||||
request(requestOptions,
|
|
||||||
(err, res, body) => {
|
|
||||||
// assert
|
|
||||||
expect(res.statusCode).to.equal(201);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Binary", () => {
|
describe("Binary", () => {
|
||||||
|
|
Loading…
Reference in New Issue