Skip to content

Commit

Permalink
feat: add hono adapter (#670)
Browse files Browse the repository at this point in the history
* feat: add hono adapter

* Update README.md
  • Loading branch information
nihalgonsalves authored Jan 7, 2024
1 parent 92b6edc commit 56a271c
Show file tree
Hide file tree
Showing 11 changed files with 773 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ With this library you get a beautiful UI for visualizing what's happening with e
| [@bull-board/koa](https://www.npmjs.com/package/@bull-board/koa) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/koa) |
| [@bull-board/hapi](https://www.npmjs.com/package/@bull-board/hapi) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/hapi) |
| [@bull-board/nestjs](https://www.npmjs.com/package/@bull-board/nestjs) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/nestjs) |
| [@bull-board/hono](https://www.npmjs.com/package/@bull-board/hono) | ![npm (scoped)](https://img.shields.io/npm/v/@bull-board/hono) |

## Notes

Expand All @@ -51,6 +52,10 @@ yarn add @bull-board/fastify
yarn add @bull-board/hapi
# or
yarn add @bull-board/koa
# or
yarn add @bull-board/nestjs
# or
yarn add @bull-board/hono
```

### NestJS specific setup
Expand Down Expand Up @@ -105,6 +110,7 @@ For more advanced usages check the `examples` folder, currently it contains:
5. [With Koa.js server](https://github.com/felixmosh/bull-board/tree/master/examples/with-koa)
6. [With Nest.js server using the built-in module](https://github.com/felixmosh/bull-board/tree/master/examples/with-nestjs-module) (Thanx to @dennissnijder)
7. [With Nest.js server using the express adapter](https://github.com/felixmosh/bull-board/tree/master/examples/with-nestjs) (Thanx to @lodi-g)
8. [With Hono server](https://github.com/felixmosh/bull-board/tree/master/examples/with-hono) (Thanks to @nihalgonsalves)

### Board options
1. `uiConfig.boardTitle` (default: `Bull Dashboard`)
Expand Down
3 changes: 3 additions & 0 deletions examples/with-hono/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hono example

This example shows how to use [Hono](https://hono.dev) as a server for bull-board.
79 changes: 79 additions & 0 deletions examples/with-hono/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const { createBullBoard } = require('@bull-board/api');
const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter');
const { HonoAdapter } = require('@bull-board/hono');
const { Queue: QueueMQ, Worker } = require('bullmq');
const { Hono } = require('hono');
const { showRoutes } = require('hono/dev');
const { serve } = require('@hono/node-server');
const { serveStatic } = require('@hono/node-server/serve-static');

const sleep = (t) => new Promise((resolve) => setTimeout(resolve, t * 1000));

const redisOptions = {
port: 6379,
host: 'localhost',
password: '',
tls: false,
};

const createQueueMQ = (name) => new QueueMQ(name, { connection: redisOptions });

async function setupBullMQProcessor(queueName) {
new Worker(
queueName,
async (job) => {
for (let i = 0; i <= 100; i++) {
await sleep(Math.random());
await job.updateProgress(i);
await job.log(`Processing job at interval ${i}`);

if (Math.random() * 200 < 1) throw new Error(`Random error ${i}`);
}

return { jobId: `This is the return value of job (${job.id})` };
},
{ connection: redisOptions }
);
}

const run = async () => {
const exampleBullMq = createQueueMQ('BullMQ');

await setupBullMQProcessor(exampleBullMq.name);

const app = new Hono();

const serverAdapter = new HonoAdapter(serveStatic);

createBullBoard({
queues: [new BullMQAdapter(exampleBullMq)],
serverAdapter,
});

const basePath = '/ui'
serverAdapter.setBasePath(basePath);
app.route(basePath, serverAdapter.registerPlugin());

app.get('/add', async (c) => {
await exampleBullMq.add('Add', { title: c.req.query('title') });

return c.json({ ok: true })
});

showRoutes(app);

serve({ fetch: app.fetch, port: 3000 }, ({ address, port }) => {
/* eslint-disable no-console */
console.log(`Running on ${address}:${port}...`);
console.log(`For the UI of instance1, open http://localhost:${port}/ui`);
console.log('Make sure Redis is running on port 6379 by default');
console.log('To populate the queue, run:');
console.log(` curl http://localhost:${port}/add?title=Example`);
/* eslint-enable */
})
};

run().catch((e) => {
console.error(e);
process.exit(1);
});
18 changes: 18 additions & 0 deletions examples/with-hono/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "bull-board-with-hono",
"version": "1.0.0",
"description": "Example of how to use Hono server with bull-board",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "felixmosh",
"license": "ISC",
"dependencies": {
"@bull-board/hono": "../../packages/hono",
"@hono/node-server": "^1.4.0",
"bullmq": "^4.6.0",
"hono": "^3.12.0"
}
}
Loading

0 comments on commit 56a271c

Please sign in to comment.