Created
August 6, 2025 17:53
-
-
Save sibelius/fbf4fc4703d8dd390be16e314881f459 to your computer and use it in GitHub Desktop.
queue API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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