Compare commits

...

3 Commits

Author SHA1 Message Date
herel 0dac1a5505 build: optimize Docker image with multi-stage build and security improvements
- Implement multi-stage build to reduce final image size
- Install only production dependencies with npm ci --only=production
- Clean npm cache to reduce image size
- Improve layer caching by copying package files before application code
- Update package version from 1.0.0 to 1.0.1
2025-10-09 10:11:38 +02:00
herel 271eb107a2 feat: add OpenAPI documentation and comprehensive test suite
- Add OpenAPI schema endpoint at /openapi.json with proper logging
- Make server exportable for testing by wrapping app.listen in module check
- Add Jest test suite covering all API endpoints
- Improve request logging with IP addresses and timestamps
- Add development dependencies for testing (jest, supertest)
2025-09-02 12:26:30 +02:00
herel 69c216a0d0 chore: add node_modules to .gitignore 2025-09-02 12:23:36 +02:00
7 changed files with 5616 additions and 15 deletions
+1
View File
@@ -0,0 +1 @@
node_modules/
+16 -9
View File
@@ -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"]
+22 -1
View File
@@ -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;
+5516
View File
File diff suppressed because it is too large Load Diff
+7 -2
View File
@@ -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"
} }
} }
+28
View File
@@ -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' });
});
});
+23
View File
@@ -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);
});
});