-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
84 lines (73 loc) · 1.98 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const { URL } = require('node:url');
const express = require('express');
const basicAuth = require('express-basic-auth');
const Queue = require('bull');
const { createBullBoard } = require('@bull-board/api');
const { BullAdapter } = require('@bull-board/api/bullAdapter');
const { ExpressAdapter } = require('@bull-board/express');
const Redis = require('ioredis');
const serverAdapter = new ExpressAdapter();
serverAdapter.setBasePath('/admin/queues');
const { addQueue, removeQueue, setQueues, replaceQueues } = createBullBoard({
serverAdapter,
queues: createQueueInstances(getQueueNames()).map(
(queue) => new BullAdapter(queue)
),
});
const app = express();
app.use(
'/admin/queues',
basicAuth({
users: getUsers(),
challenge: true,
}),
serverAdapter.getRouter()
);
app.use((req, res) => {
res.json({ message: 'Hello world!' });
});
// other configurations of your server
app.listen(3000, () => {
console.log('Running on 3000...');
});
function getQueueNames(envName = 'QUEUES') {
const queues = process.env[envName];
return queues ? queues.split(',') : [];
}
/**
* @param {string[]} names
*/
function createQueueInstances(names) {
const redisOptions = parseRedisURL(process.env.REDIS_URL_QUEUE);
const redisClient = new Redis(redisOptions);
const createClient = (type, config) => {
if (['bclient', 'subscriber'].includes(type)) {
return new Redis({
...redisOptions,
...config,
maxRetriesPerRequest: null,
});
} else {
return redisClient;
}
};
return names.map((name) => new Queue(name, { createClient }));
}
/**
* @param {string} url
*/
function parseRedisURL(url) {
const { password, hostname, port, pathname } = new URL(url);
return {
host: hostname,
port: parseInt(port),
password,
db: pathname ? parseInt(pathname.slice(1)) : undefined,
};
}
function getUsers() {
const [username, password] = process.env.BASIC_AUTH_CREDENTIAL.split(':');
return {
[username]: password,
};
}