From d5baecaaca530cc5b8ef29d331707244d15cd07c Mon Sep 17 00:00:00 2001 From: obvTiger Date: Wed, 9 Apr 2025 10:09:13 +0200 Subject: [PATCH] feat: add files --- Dockerfile | 8 ++++++ README.md | 51 ++++++++++++++++++++++++++++++++-- server.js | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 Dockerfile create mode 100644 server.js diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ec42746 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM git.eplg.services/archive/node:latest + +WORKDIR /usr/src/app +COPY server.js . + +EXPOSE 3000 + +CMD [ "node", "server.js","-h","0.0.0.0" ] \ No newline at end of file diff --git a/README.md b/README.md index d352b5d..fb50bd9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,49 @@ -# simple-webhook-server +# Simple Webhook Server -A simple server for receiving for testing with webhook calls. -The server responds with every request method and logs the full data sent to the server to the console. \ No newline at end of file +A simple server for testing webhook calls. +The server responds with every request method and logs the full data sent to the server to the console. + +Example request: + +```bash +curl -X POST -H "Content-Type: application/json" -d '{"foo": "bar"}' http://localhost:3000 +``` + +Server Console: + +``` +2025-04-09T08:07:07.151Z - POST / +Headers: { + "host": "localhost:3000", + "user-agent": "curl/8.5.0", + "accept": "*/*", + "content-type": "application/json", + "content-length": "14" +} +Body: { + "foo": "bar" +} +``` + +## Usage + +This currently supports running with node and docker + + +### Running with node +```bash +node server.js +``` + +The server allows the following options: + +- `--port`: The port to listen on. Default: 3000 +- `--host`: The host to bind the server to. Default: localhost + +### Running with docker + +```bash +docker run -p 3000:3000 git.eplg.services/obvtiger/simple-webhook-server:latest +``` + +You can change the port by modifying the first 3000 in the `-p` option. \ No newline at end of file diff --git a/server.js b/server.js new file mode 100644 index 0000000..3654981 --- /dev/null +++ b/server.js @@ -0,0 +1,81 @@ +const http = require('http'); + +function parseArgs() { + const args = process.argv.slice(2); + const options = { + port: 3000, + host: 'localhost' + }; + + for (let i = 0; i < args.length; i++) { + if (args[i] === '--port' || args[i] === '-p') { + options.port = parseInt(args[i + 1]) || 3000; + i++; + } else if (args[i] === '--host' || args[i] === '-h') { + options.host = args[i + 1] || 'localhost'; + i++; + } else if (args[i] === '--help') { + console.log(` +Usage: node server.js [options] + +Options: + --port, -p Port to run the server on (default: 3000) + --host, -h Host to bind the server to (default: localhost) + --help Show this help message + `); + process.exit(0); + } + } + return options; +} + +const options = parseArgs(); + +const server = http.createServer((req, res) => { + let body = ''; + + console.log(`\n${new Date().toISOString()} - ${req.method} ${req.url}`); + console.log('Headers:', JSON.stringify(req.headers, null, 2)); + + req.on('data', chunk => { + body += chunk.toString(); + }); + + req.on('end', () => { + if (body) { + try { + const jsonBody = JSON.parse(body); + console.log('Body:', JSON.stringify(jsonBody, null, 2)); + } catch { + console.log('Body:', body); + } + } + + res.writeHead(200, { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS' + }); + + const response = { + message: 'Request received', + method: req.method, + url: req.url, + timestamp: new Date().toISOString() + }; + + res.end(JSON.stringify(response)); + }); +}); + +server.listen(options.port, options.host, () => { + console.log(`Webhook listener running on http://${options.host}:${options.port}`); + + process.on('SIGINT', () => { + console.log('\nReceived CTRL+C - Shutting down server...'); + server.close(() => { + console.log('Server closed'); + process.exit(0); + }); + }); +});