feat: add files

This commit is contained in:
obvTiger 2025-04-09 10:09:13 +02:00
parent b44931ff90
commit d5baecaaca
3 changed files with 137 additions and 3 deletions

8
Dockerfile Normal file
View file

@ -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" ]

View file

@ -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.
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.

81
server.js Normal file
View file

@ -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);
});
});
});