initial commit

This commit is contained in:
Roman Siverov 2026-06-04 13:36:52 +03:00
commit 46911314f2
16 changed files with 1733 additions and 0 deletions

85
src/http/motd.ts Normal file
View file

@ -0,0 +1,85 @@
import { Express } from 'express';
import { redisClient } from '../index.js';
import { pgPool } from '../db.js';
export async function httpMotd(app: Express) {
app.get('/motd', async (_req, res) => {
try {
const cached = await redisClient.get('motd:current');
if (cached) {
return res.json({
source: 'redis',
message: cached,
});
}
const result = await pgPool.query(
`
SELECT message
FROM motd
ORDER BY created_at DESC, id DESC
LIMIT 1
`
);
if (result.rowCount === 0) {
return res.status(404).json({
error: 'No MOTD set',
});
}
const message = result.rows[0].message as string;
await redisClient.set('motd:current', message, {
EX: 60,
});
return res.json({
source: 'postgres',
message,
});
} catch (error) {
console.error(error);
return res.status(500).json({
error: 'Internal server error',
});
}
});
app.post('/motd', async (req, res) => {
try {
const message = req.body?.message;
if (typeof message !== 'string' || message.trim() === '') {
return res.status(400).json({
error: 'message must be a non-empty string',
});
}
const trimmedMessage = message.trim();
await pgPool.query(
`
INSERT INTO motd (message)
VALUES ($1)
`,
[trimmedMessage]
);
await redisClient.set('motd:current', trimmedMessage, {
EX: 60,
});
return res.status(201).json({
message: 'MOTD updated',
motd: trimmedMessage,
});
} catch (error) {
console.error(error);
return res.status(500).json({
error: 'Internal server error',
});
}
});
}