Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add H3 adapter #669

Merged
merged 30 commits into from
Jan 11, 2024
Merged

Add H3 adapter #669

merged 30 commits into from
Jan 11, 2024

Conversation

genu
Copy link
Contributor

@genu genu commented Jan 5, 2024

Resolves #623

Usage:

import { createApp, createRouter } from 'h3';
import { createBullBoard } from '@bull-board/api';
import { H3Adapter } from '@bull-board/h3';

const serverAdapter = new H3Adapter();
serverAdapter.setBasePath('/ui');

createBullBoard({
  queues: [...],
  serverAdapter,
});

export const app = createApp();

// your app router
const router = createRouter();
router.use(...)

app.use(router);

// Add the bullboard handlers
app.use(serverAdapter.registerHandlers());

@genu genu mentioned this pull request Jan 5, 2024
@genu genu marked this pull request as ready for review January 5, 2024 16:14
@genu
Copy link
Contributor Author

genu commented Jan 9, 2024

@felixmosh I would consider this complete, let me know if there are any other changes needed.

@AidanHibbard
Copy link

AidanHibbard commented Jan 9, 2024

@genu Are you able to spin-up an example repository with this? I'm trying to test this by copying the libs over to my repo here

But I'm running into errors when I try to set up the route following your example...

There's a good chance I'm missing something.

@genu
Copy link
Contributor Author

genu commented Jan 10, 2024

@genu Are you able to spin-up an example repository with this? I'm trying to test this by copying the libs over to my repo here -> https://github.com/AidanHibbard/bull-board-h3-test/

But I'm running into errors when I try to set up the route following your example...

There's a good chance I'm missing something.

The example that I provided in this PR is for h3. In your repository, you're trying to applying it in a Nuxt context.

Because Nuxt hijacks the routing, you can't really use the example I provided as is.

For nuxt, you have to

  1. Define your route path as:
server
--- routes
------ bullboard-ui.ts
------ bullboard-ui
--------- [...].ts
  1. In your [...]ts route and your bullboard-ui.ts route you have to forward the event to the server adapter. This is a design choice, but you can put the event handler in a new file and re-use it in both places:
import { redirectToBullboard } from "~/server/bullmq/h3-adapter/nitro-handler";

export default defineEventHandler(redirectToBullboard);
  1. Nitro handler:
import { createBullBoard } from "@bull-board/api";
import { BullMQAdapter } from "@bull-board/api/bullMQAdapter";
import { Queue } from "bullmq";
import { H3Adapter } from "~/server/bullmq/h3-adapter";
import { H3Event } from "h3";

const {
  redis: { host, password, port },
} = useRuntimeConfig();

const createQueue = (name: string) =>
  new Queue(name, {
    connection: {
      host,
      port,
      password,
    },
  });

const serverHandler = new H3Adapter();
serverHandler.setBasePath("/bullboard-ui");

createBullBoard({
  queues: [...],
  serverAdapter: serverHandler,
});

const uiHandler = serverHandler.registerHandlers();

export const redirectToBullboard = async (event: H3Event) => {
  return await uiHandler.handler(event);
};

The redirectToBullboard is key here, as we're forwarding the same event from nitro to the bullboard uiHandler. This allows you to integrate well with nitro's other features.

NOTE: You have to ensure that your route (in this case /bullboard-ui matches the basePath in the serverAdapter

@genu
Copy link
Contributor Author

genu commented Jan 10, 2024

If you still have trouble with it in Nuxt, I can have a look at your test repo tomorrow and suggest some changes.

My plan is, after this is merged, to build a nuxt module on top of this to simplify the nuxt integration further.

@AidanHibbard
Copy link

@genu That worked swimmingly, thanks

@genu
Copy link
Contributor Author

genu commented Jan 10, 2024

Excellent! 😎

packages/h3/package.json Outdated Show resolved Hide resolved
packages/h3/src/H3Adapter.ts Show resolved Hide resolved
packages/h3/src/H3Adapter.ts Show resolved Hide resolved
examples/with-h3/index.ts Outdated Show resolved Hide resolved
packages/h3/src/H3Adapter.ts Show resolved Hide resolved
packages/h3/src/H3Adapter.ts Outdated Show resolved Hide resolved
packages/h3/src/H3Adapter.ts Show resolved Hide resolved
@felixmosh felixmosh merged commit eedbcc8 into felixmosh:master Jan 11, 2024
@felixmosh
Copy link
Owner

Thank you for you contribution 🙏🏽

@felixmosh
Copy link
Owner

Released in v5.13.0 🎊

@genu
Copy link
Contributor Author

genu commented Jan 17, 2024

@AidanHibbard If you are using nuxt, I've created a module that abstracts a lot of what this adapter is doing.

Have a look at: https://github.com/genu/nuxt-concierge

@AidanHibbard
Copy link

@genu This looks great! Looks pretty similar to the module @productdevbook wrote for Pergel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Usage with Nuxt 3
3 participants