Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created August 6, 2025 17:53
Show Gist options
  • Save sibelius/fbf4fc4703d8dd390be16e314881f459 to your computer and use it in GitHub Desktop.
Save sibelius/fbf4fc4703d8dd390be16e314881f459 to your computer and use it in GitHub Desktop.
queue API
// src/server.ts
import Koa from 'koa';
import Router from '@koa/router';
import bodyParser from 'koa-bodyparser';
import {
pushToQueue,
pullFromQueue,
getQueueLength
} from './redisQueue';
const app = new Koa();
const router = new Router();
router.post('/queue', async (ctx) => {
const { message } = ctx.request.body as { message?: string };
if (!message) {
ctx.status = 400;
ctx.body = { error: 'Message is required' };
return;
}
await pushToQueue(message);
ctx.body = { success: true };
});
router.get('/queue', async (ctx) => {
const message = await pullFromQueue();
ctx.body = { message };
});
router.get('/queue/length', async (ctx) => {
const length = await getQueueLength();
ctx.body = { length };
});
app
.use(bodyParser())
.use(router.routes())
.use(router.allowedMethods());
const port = 3000;
app.listen(port, () => {
console.log(`Queue service running at http://localhost:${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment