All checks were successful
Build and Push to ACR / Build and Push (push) Successful in 45s
Change-Id: Iccb9a69a29ce96d618762c5c852c8ec43d9b78df
79 lines
2.5 KiB
JavaScript
79 lines
2.5 KiB
JavaScript
/* eslint-env node, es2020 */
|
|
'use strict';
|
|
|
|
const express = require('express');
|
|
const client = require('prom-client');
|
|
|
|
const app = express();
|
|
app.use(express.json());
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Prometheus metrics
|
|
// ---------------------------------------------------------------------------
|
|
const register = new client.Registry();
|
|
client.collectDefaultMetrics({ register });
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// In-memory store
|
|
// ---------------------------------------------------------------------------
|
|
let nextId = 1;
|
|
const store = new Map();
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Health
|
|
// ---------------------------------------------------------------------------
|
|
app.get('/health', (_req, res) => res.json({ status: 'UP' }));
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Metrics
|
|
// ---------------------------------------------------------------------------
|
|
app.get('/metrics', async (_req, res) => {
|
|
res.set('Content-Type', register.contentType);
|
|
res.end(await register.metrics());
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Items CRUD
|
|
// ---------------------------------------------------------------------------
|
|
app.get('/api/items', (_req, res) => {
|
|
res.json([...store.values()]);
|
|
});
|
|
|
|
app.post('/api/items', (req, res) => {
|
|
const { name = 'unnamed', description = '' } = req.body ?? {};
|
|
const item = { id: nextId++, name, description };
|
|
store.set(item.id, item);
|
|
res.status(201).json(item);
|
|
});
|
|
|
|
app.get('/api/items/:id', (req, res) => {
|
|
const item = store.get(Number(req.params.id));
|
|
if (!item) return res.status(404).json({ error: 'Not found' });
|
|
res.json(item);
|
|
});
|
|
|
|
app.put('/api/items/:id', (req, res) => {
|
|
const id = Number(req.params.id);
|
|
const existing = store.get(id);
|
|
if (!existing) return res.status(404).json({ error: 'Not found' });
|
|
const updated = { ...existing, ...req.body, id };
|
|
store.set(id, updated);
|
|
res.json(updated);
|
|
});
|
|
|
|
app.delete('/api/items/:id', (req, res) => {
|
|
const id = Number(req.params.id);
|
|
store.delete(id);
|
|
res.json({ deleted: id });
|
|
});
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Start
|
|
// ---------------------------------------------------------------------------
|
|
const PORT = Number(process.env.PORT ?? 3000);
|
|
app.listen(PORT, () => {
|
|
console.log('test-micro-alex-stable-1 listening on :' + PORT);
|
|
});
|
|
|
|
module.exports = app; // for testing
|