Skip to content

Commit 4c0c584

Browse files
authored
Merge pull request #6 from Diluka/master
Add PROXY_PATH
2 parents 93f1581 + 41a1835 commit 4c0c584

File tree

7 files changed

+77
-18
lines changed

7 files changed

+77
-18
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ node_modules/
1010
jspm_packages/
1111

1212
.idea/
13+
.env

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ see "Example with docker-compose" section for example with env parameters
3030
### Environment variables
3131
* `REDIS_HOST` - host to connect to redis (localhost by default)
3232
* `REDIS_PORT` - redis port (6379 by default)
33+
* `REDIS_DB` - redis db ('0' by default)
3334
* `REDIS_USE_TLS` - enable TLS true or false (false by default)
3435
* `REDIS_PASSWORD` - password to connect to redis (no password by default)
3536
* `BULL_PREFIX` - prefix to your bull queue name (bull by default)
3637
* `BULL_VERSION` - version of bull lib to use 'BULLMQ' or 'BULL' ('BULLMQ' by default)
3738
* `BASE_PATH` - basePath for bull board, e.g. '/bull-board' ('/' by default)
39+
* `PROXY_PATH` - proxyPath for bull board, e.g. '/bull-board' ('' by default)
3840
* `USER_LOGIN` - login to restrict access to bull-board interface (disabled by default)
3941
* `USER_PASSWORD` - password to restrict access to bull-board interface (disabled by default)
4042

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
"version": "2.0.3",
44
"main": "src/index.js",
55
"license": "MIT",
6+
"scripts": {
7+
"start": "node ."
8+
},
69
"dependencies": {
710
"body-parser": "^1.19.0",
811
"bull": "^3.13.0",
912
"bull-board": "^2.0.3",
1013
"bullmq": "^1.8.4",
1114
"connect-ensure-login": "^0.1.1",
15+
"dotenv": "^8.2.0",
1216
"express": "^4.17.1",
1317
"express-session": "^1.17.1",
18+
"morgan": "^1.10.0",
1419
"passport": "^0.4.1",
1520
"passport-local": "^1.0.0",
1621
"redis": "^3.0.2"

src/config.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1-
let BASE_PATH = process.env.BASE_PATH || '/';
1+
require('dotenv').config();
22

3-
if (BASE_PATH.endsWith('/')) {
4-
BASE_PATH = BASE_PATH.substr(0, BASE_PATH.length - 1);
3+
function normalizePath(pathStr) {
4+
return (pathStr || '').replace(/\/$/, '');
55
}
66

7+
const BASE_PATH = normalizePath(process.env.BASE_PATH);
8+
const PROXY_PATH = normalizePath(process.env.PROXY_PATH) + BASE_PATH;
9+
710
const config = {
811
REDIS_PORT: process.env.REDIS_PORT || 6379,
912
REDIS_HOST: process.env.REDIS_HOST || 'localhost',
13+
REDIS_DB: process.env.REDIS_DB || '0',
1014
REDIS_PASSWORD: process.env.REDIS_PASSWORD,
1115
REDIS_USE_TLS: process.env.REDIS_USE_TLS,
1216
BULL_PREFIX: process.env.BULL_PREFIX || 'bull',
1317
BULL_VERSION: process.env.BULL_VERSION || 'BULLMQ',
1418
PORT: process.env.PORT || 3000,
1519
BASE_PATH: BASE_PATH,
20+
PROXY_PATH: PROXY_PATH,
1621
USER_LOGIN: process.env.USER_LOGIN,
1722
USER_PASSWORD: process.env.USER_PASSWORD,
1823

1924
AUTH_ENABLED: Boolean(process.env.USER_LOGIN && process.env.USER_PASSWORD),
2025
HOME_PAGE: BASE_PATH || '/',
2126
LOGIN_PAGE: `${BASE_PATH}/login`,
27+
PROXY_HOME_PAGE: PROXY_PATH || '/',
28+
PROXY_LOGIN_PAGE: `${PROXY_PATH}/login`,
2229
};
2330

2431
module.exports = config;

src/index.js

+34-13
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ const {authRouter} = require('./login');
1414
const config = require('./config');
1515

1616
const redisConfig = {
17-
redis: {
18-
port: config.REDIS_PORT,
19-
host: config.REDIS_HOST,
20-
...(config.REDIS_PASSWORD && {password: config.REDIS_PASSWORD}),
21-
tls: config.REDIS_USE_TLS === 'true',
22-
},
17+
redis: {
18+
port: config.REDIS_PORT,
19+
host: config.REDIS_HOST,
20+
db: config.REDIS_DB,
21+
...(config.REDIS_PASSWORD && {password: config.REDIS_PASSWORD}),
22+
tls: config.REDIS_USE_TLS === 'true',
23+
},
2324
};
2425

2526
const client = redis.createClient(redisConfig.redis);
@@ -46,18 +47,38 @@ const app = express();
4647
app.set('views', __dirname + '/views');
4748
app.set('view engine', 'ejs');
4849

49-
app.use(session({secret: Math.random().toString(), resave: false, saveUninitialized: false}));
50+
if (app.get('env') !== 'production') {
51+
const morgan = require('morgan');
52+
app.use(morgan('combined'));
53+
}
54+
55+
app.use((req, res, next) => {
56+
if (config.PROXY_PATH) req.proxyUrl = config.PROXY_PATH;
57+
next();
58+
});
59+
60+
const sessionOpts = {
61+
name: 'bull-board.sid',
62+
secret: Math.random().toString(),
63+
resave: false,
64+
saveUninitialized: false,
65+
cookie: {
66+
path: '/',
67+
httpOnly: false,
68+
secure: false
69+
}
70+
};
71+
72+
app.use(session(sessionOpts));
5073
app.use(passport.initialize({}));
5174
app.use(passport.session({}));
52-
5375
app.use(bodyParser.urlencoded({extended: false}));
5476

5577
if (config.AUTH_ENABLED) {
56-
app.use(config.LOGIN_PAGE, authRouter);
57-
app.use(config.HOME_PAGE, ensureLoggedIn(config.LOGIN_PAGE), router);
58-
}
59-
else {
60-
app.use(config.HOME_PAGE, router);
78+
app.use(config.LOGIN_PAGE, authRouter);
79+
app.use(config.HOME_PAGE, ensureLoggedIn(config.PROXY_LOGIN_PAGE), router);
80+
} else {
81+
app.use(config.HOME_PAGE, router);
6182
}
6283

6384
app.listen(config.PORT, () => {

src/login.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ authRouter.route('/')
2929
res.render('login');
3030
})
3131
.post(passport.authenticate('local', {
32-
successRedirect: config.HOME_PAGE,
33-
failureRedirect: config.LOGIN_PAGE,
32+
successRedirect: config.PROXY_HOME_PAGE,
33+
failureRedirect: config.PROXY_LOGIN_PAGE,
3434
}));
3535

3636
exports.authRouter = authRouter;

yarn.lock

+23
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ balanced-match@^1.0.0:
101101
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
102102
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==
103103

104+
basic-auth@~2.0.1:
105+
version "2.0.1"
106+
resolved "https://registry.npm.taobao.org/basic-auth/download/basic-auth-2.0.1.tgz#b998279bf47ce38344b4f3cf916d4679bbf51e3a"
107+
integrity sha1-uZgnm/R844NEtPPPkW1Gebv1Hjo=
108+
dependencies:
109+
safe-buffer "5.1.2"
110+
104111
body-parser@1.19.0, body-parser@^1.19.0:
105112
version "1.19.0"
106113
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
@@ -291,6 +298,11 @@ destroy@~1.0.4:
291298
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"
292299
integrity sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=
293300

301+
dotenv@^8.2.0:
302+
version "8.2.0"
303+
resolved "https://registry.npm.taobao.org/dotenv/download/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
304+
integrity sha1-l+YZJZradQ7qPk6j4mvO6lQksWo=
305+
294306
ee-first@1.1.1:
295307
version "1.1.1"
296308
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
@@ -685,6 +697,17 @@ moment-timezone@^0.5.31:
685697
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3"
686698
integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==
687699

700+
morgan@^1.10.0:
701+
version "1.10.0"
702+
resolved "https://registry.npm.taobao.org/morgan/download/morgan-1.10.0.tgz#091778abc1fc47cd3509824653dae1faab6b17d7"
703+
integrity sha1-CRd4q8H8R801CYJGU9rh+qtrF9c=
704+
dependencies:
705+
basic-auth "~2.0.1"
706+
debug "2.6.9"
707+
depd "~2.0.0"
708+
on-finished "~2.3.0"
709+
on-headers "~1.0.2"
710+
688711
ms@2.0.0:
689712
version "2.0.0"
690713
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

0 commit comments

Comments
 (0)