From 7f5cdf2f4c8c0bd95176e7557038d0bf417ca75a Mon Sep 17 00:00:00 2001 From: Rick van der Staaij Date: Wed, 24 Jul 2019 12:19:54 +0200 Subject: [PATCH 1/3] Add ability to trigger events directly from the API --- back-end/domain/module/AbstractModule.js | 1 + back-end/routes/event.js | 29 ++++++++++++++++++++++++ back-end/routes/index.js | 1 + webpack.mix.js | 3 +-- 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 back-end/routes/event.js diff --git a/back-end/domain/module/AbstractModule.js b/back-end/domain/module/AbstractModule.js index 461abc78..73c1eba9 100644 --- a/back-end/domain/module/AbstractModule.js +++ b/back-end/domain/module/AbstractModule.js @@ -12,6 +12,7 @@ class AbstractModule { } /** + * @param config * @param {Status} status */ fireEvent(config, status) { diff --git a/back-end/routes/event.js b/back-end/routes/event.js new file mode 100644 index 00000000..c0329b21 --- /dev/null +++ b/back-end/routes/event.js @@ -0,0 +1,29 @@ +const app = (module.exports = require('express')()); +const EventTrigger = require('../domain/event/EventTrigger.js'); +const Config = require('../config/ConfigLoaderFactory') + .getLoader() + .getConfig(); + +app.post('/', (request, response) => { + console.log('/event [POST]'); + + const eventName = request.body.event; + + if (!eventName) { + return response.status(422).json({ + message: `Missing required field "event".`, + }); + } + + if (Config.getEventByName(eventName)) { + EventTrigger.fireModulesForEvent(eventName, null); + + return response.json({ + message: 'Event has been triggered!', + }); + } + + return response.status(404).json({ + message: `The given event name "${eventName}" is not found.`, + }); +}); diff --git a/back-end/routes/index.js b/back-end/routes/index.js index f369fa6f..72fb2816 100644 --- a/back-end/routes/index.js +++ b/back-end/routes/index.js @@ -6,3 +6,4 @@ app.use('/webhook', require('./webhook')); app.use('/debug', require('./debug')); app.use('/contributors', require('./contributors')); app.use('/version', require('./version')); +app.use('/event', require('./event.js')); diff --git a/webpack.mix.js b/webpack.mix.js index e15a9500..cdf9b5ff 100644 --- a/webpack.mix.js +++ b/webpack.mix.js @@ -24,9 +24,8 @@ mix.copy('front-end/static/', 'dashboard/'); if (!mix.inProduction()) { mix.webpackConfig({ devtool: `inline-source-map` }); - const Config = require('./back-end/config/Config'); mix.browserSync({ - proxy: `localhost:${Config.getServerPort()}`, + proxy: `localhost:9999`, injectChanges: false, files: [`dashboard/**/*`], }); From d0b1a1cf647eee5e6dff467a9c50f4795a125780 Mon Sep 17 00:00:00 2001 From: Rick van der Staaij Date: Wed, 24 Jul 2019 12:31:28 +0200 Subject: [PATCH 2/3] Add documentation for directly triggering an event --- docs/{status-API.md => API.md} | 29 ++++++++++++++++++++++++++++- mkdocs.yml | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) rename docs/{status-API.md => API.md} (69%) diff --git a/docs/status-API.md b/docs/API.md similarity index 69% rename from docs/status-API.md rename to docs/API.md index 5b867f1b..bec0f6d8 100644 --- a/docs/status-API.md +++ b/docs/API.md @@ -1,4 +1,11 @@ -# Status API +# API + +- [Statuses](#statuses) +- [Events](#events) + +## Statuses + +### POST /status @todo: Explain that you can manually push statuses to the `POST /status`. The API will let you know what's required or not. @@ -22,6 +29,26 @@ | `image` | no | An URL to an image representing the status | | `userImage` | no | An URL to an image showing the user who triggered the status | +### DELETE /status/:status-key + @todo: Explain that you can remove statues using `DELETE /status/:status-key` +### GET /status/clear-all + @todo: Explain that yo can remove all statuses using `GET /status/clear-all` + +## Events + +### POST /event + +You can directly trigger an event (configured in the configuration) via `POST /event`. + +```json +{ + "event": "celebrate-success" +} +``` + +| key | required? | description | +| ----------- | --------- | ------------------------------------------------------------------------ | +| `event` | yes | They configuration key that used for the event that should be triggered. | diff --git a/mkdocs.yml b/mkdocs.yml index 1ded2502..cc988b73 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -12,7 +12,7 @@ pages: - Run as a service: setup/run-as-service.md - Run with Docker: setup/run-with-docker.md - Setup Raspberry: setup/Raspberry.md - - Status API: status-API.md + - CIMonitor API: API.md - Link a service: - GitLab: services/GitLab.md - Travis CI: services/Travis-CI.md From 168f097f6b34395e2c2f8f45bf7131acb4ccc533 Mon Sep 17 00:00:00 2001 From: Rick van der Staaij <6495166+RickvdStaaij@users.noreply.github.com> Date: Thu, 25 Jul 2019 08:42:04 +0200 Subject: [PATCH 3/3] Fix English --- docs/API.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/API.md b/docs/API.md index bec0f6d8..96105420 100644 --- a/docs/API.md +++ b/docs/API.md @@ -49,6 +49,6 @@ You can directly trigger an event (configured in the configuration) via `POST /e } ``` -| key | required? | description | -| ----------- | --------- | ------------------------------------------------------------------------ | -| `event` | yes | They configuration key that used for the event that should be triggered. | +| key | required? | description | +| ----------- | --------- | -------------------------------------------------------------------------- | +| `event` | yes | The configuration key that is used for the event that should be triggered. |