Compare commits
3 Commits
3a4f58621a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
0dac1a5505
|
|||
|
271eb107a2
|
|||
|
69c216a0d0
|
@@ -0,0 +1 @@
|
|||||||
|
node_modules/
|
||||||
+16
-9
@@ -1,15 +1,22 @@
|
|||||||
FROM node:23-slim
|
FROM node:24-slim AS builder
|
||||||
|
|
||||||
WORKDIR /usr/src/app
|
WORKDIR /usr/src/app
|
||||||
|
|
||||||
|
# Copy dependency files first
|
||||||
|
COPY package*.json ./
|
||||||
|
|
||||||
|
# Install production dependencies only
|
||||||
|
RUN npm ci --only=production && npm cache clean --force
|
||||||
|
|
||||||
|
FROM node:24-slim
|
||||||
|
|
||||||
|
WORKDIR /usr/src/app
|
||||||
|
|
||||||
|
COPY --from=builder /usr/src/app/node_modules ./node_modules
|
||||||
|
|
||||||
|
# Copy application code
|
||||||
|
COPY . .
|
||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|
||||||
COPY package.json /usr/src/app/package.json
|
|
||||||
|
|
||||||
RUN npm install
|
|
||||||
|
|
||||||
USER node
|
|
||||||
|
|
||||||
COPY . /usr/src/app/
|
|
||||||
|
|
||||||
CMD ["npm", "start"]
|
CMD ["npm", "start"]
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ const PORT = process.env.PORT || 3000;
|
|||||||
// Load reasons from JSON
|
// Load reasons from JSON
|
||||||
const reasons = JSON.parse(fs.readFileSync('./reasons.json', 'utf-8'));
|
const reasons = JSON.parse(fs.readFileSync('./reasons.json', 'utf-8'));
|
||||||
|
|
||||||
|
// Load OpenAPI schema
|
||||||
|
const openapiSchema = JSON.parse(fs.readFileSync('./openapi.json', 'utf-8'));
|
||||||
|
|
||||||
// Rate limiter: 120 requests per minute per IP
|
// Rate limiter: 120 requests per minute per IP
|
||||||
const limiter = rateLimit({
|
const limiter = rateLimit({
|
||||||
windowMs: 60 * 1000, // 1 minute
|
windowMs: 60 * 1000, // 1 minute
|
||||||
@@ -21,17 +24,35 @@ const limiter = rateLimit({
|
|||||||
|
|
||||||
app.use(limiter);
|
app.use(limiter);
|
||||||
|
|
||||||
|
// Serve OpenAPI schema
|
||||||
|
app.get('/openapi.json', (req, res) => {
|
||||||
|
const ip = req.headers['x-forwarded-for'] || req.ip;
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
console.log(`[${timestamp}] OpenAPI schema request from IP: ${ip}`);
|
||||||
|
res.json(openapiSchema);
|
||||||
|
});
|
||||||
|
|
||||||
// Random rejection reason endpoint
|
// Random rejection reason endpoint
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
|
const ip = req.headers['x-forwarded-for'] || req.ip;
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
console.log(`[${timestamp}] Request from IP: ${ip}`);
|
||||||
const reason = reasons[Math.floor(Math.random() * reasons.length)];
|
const reason = reasons[Math.floor(Math.random() * reasons.length)];
|
||||||
res.json({ reason });
|
res.json({ reason });
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/health', (req, res) => {
|
app.get('/health', (req, res) => {
|
||||||
|
const ip = req.headers['x-forwarded-for'] || req.ip;
|
||||||
|
const timestamp = new Date().toISOString();
|
||||||
|
console.log(`[${timestamp}] Request from IP: ${ip}`);
|
||||||
res.json({"status": "ok"});
|
res.json({"status": "ok"});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start server
|
// Start server only if this file is run directly
|
||||||
|
if (require.main === module) {
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`No-as-a-Service is running on port ${PORT}`);
|
console.log(`No-as-a-Service is running on port ${PORT}`);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = app;
|
||||||
|
|||||||
Generated
+5516
File diff suppressed because it is too large
Load Diff
+7
-2
@@ -1,15 +1,20 @@
|
|||||||
{
|
{
|
||||||
"name": "no-as-service",
|
"name": "no-as-service",
|
||||||
"version": "1.0.0",
|
"version": "1.0.1",
|
||||||
"description": "A lightweight API that returns random rejection or no reasons.",
|
"description": "A lightweight API that returns random rejection or no reasons.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node index.js"
|
"start": "node index.js",
|
||||||
|
"test": "jest"
|
||||||
},
|
},
|
||||||
"author": "hotheadhacker",
|
"author": "hotheadhacker",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"express-rate-limit": "^7.0.0"
|
"express-rate-limit": "^7.0.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"jest": "^30.1.2",
|
||||||
|
"supertest": "^7.1.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
const request = require('supertest');
|
||||||
|
|
||||||
|
describe('API Endpoints', () => {
|
||||||
|
let app;
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
app = require('../index.js');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should return a random reason at root endpoint', async () => {
|
||||||
|
const response = await request(app)
|
||||||
|
.get('/')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', /application\/json/);
|
||||||
|
|
||||||
|
expect(response.body).toHaveProperty('reason');
|
||||||
|
expect(typeof response.body.reason).toBe('string');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should return health status at /health endpoint', async () => {
|
||||||
|
const response = await request(app)
|
||||||
|
.get('/health')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', /application\/json/);
|
||||||
|
|
||||||
|
expect(response.body).toEqual({ status: 'ok' });
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
const request = require('supertest');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
describe('OpenAPI Schema Endpoint', () => {
|
||||||
|
let app;
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
// Import the app after modifications
|
||||||
|
app = require('../index.js');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should serve OpenAPI schema at /openapi.json', async () => {
|
||||||
|
// Read the expected schema from the file
|
||||||
|
const expectedSchema = JSON.parse(fs.readFileSync('./openapi.json', 'utf-8'));
|
||||||
|
|
||||||
|
const response = await request(app)
|
||||||
|
.get('/openapi.json')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', /application\/json/);
|
||||||
|
|
||||||
|
expect(response.body).toEqual(expectedSchema);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user