Skip to content

Commit

Permalink
Merge pull request #6 from Diluka/master
Browse files Browse the repository at this point in the history
Add PROXY_PATH
  • Loading branch information
Deadly0 authored May 15, 2021
2 parents 93f1581 + 41a1835 commit 4c0c584
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ node_modules/
jspm_packages/

.idea/
.env
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ see "Example with docker-compose" section for example with env parameters
### Environment variables
* `REDIS_HOST` - host to connect to redis (localhost by default)
* `REDIS_PORT` - redis port (6379 by default)
* `REDIS_DB` - redis db ('0' by default)
* `REDIS_USE_TLS` - enable TLS true or false (false by default)
* `REDIS_PASSWORD` - password to connect to redis (no password by default)
* `BULL_PREFIX` - prefix to your bull queue name (bull by default)
* `BULL_VERSION` - version of bull lib to use 'BULLMQ' or 'BULL' ('BULLMQ' by default)
* `BASE_PATH` - basePath for bull board, e.g. '/bull-board' ('/' by default)
* `PROXY_PATH` - proxyPath for bull board, e.g. '/bull-board' ('' by default)
* `USER_LOGIN` - login to restrict access to bull-board interface (disabled by default)
* `USER_PASSWORD` - password to restrict access to bull-board interface (disabled by default)

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
"version": "2.0.3",
"main": "src/index.js",
"license": "MIT",
"scripts": {
"start": "node ."
},
"dependencies": {
"body-parser": "^1.19.0",
"bull": "^3.13.0",
"bull-board": "^2.0.3",
"bullmq": "^1.8.4",
"connect-ensure-login": "^0.1.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-session": "^1.17.1",
"morgan": "^1.10.0",
"passport": "^0.4.1",
"passport-local": "^1.0.0",
"redis": "^3.0.2"
Expand Down
13 changes: 10 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
let BASE_PATH = process.env.BASE_PATH || '/';
require('dotenv').config();

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

const BASE_PATH = normalizePath(process.env.BASE_PATH);
const PROXY_PATH = normalizePath(process.env.PROXY_PATH) + BASE_PATH;

const config = {
REDIS_PORT: process.env.REDIS_PORT || 6379,
REDIS_HOST: process.env.REDIS_HOST || 'localhost',
REDIS_DB: process.env.REDIS_DB || '0',
REDIS_PASSWORD: process.env.REDIS_PASSWORD,
REDIS_USE_TLS: process.env.REDIS_USE_TLS,
BULL_PREFIX: process.env.BULL_PREFIX || 'bull',
BULL_VERSION: process.env.BULL_VERSION || 'BULLMQ',
PORT: process.env.PORT || 3000,
BASE_PATH: BASE_PATH,
PROXY_PATH: PROXY_PATH,
USER_LOGIN: process.env.USER_LOGIN,
USER_PASSWORD: process.env.USER_PASSWORD,

AUTH_ENABLED: Boolean(process.env.USER_LOGIN && process.env.USER_PASSWORD),
HOME_PAGE: BASE_PATH || '/',
LOGIN_PAGE: `${BASE_PATH}/login`,
PROXY_HOME_PAGE: PROXY_PATH || '/',
PROXY_LOGIN_PAGE: `${PROXY_PATH}/login`,
};

module.exports = config;
47 changes: 34 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ const {authRouter} = require('./login');
const config = require('./config');

const redisConfig = {
redis: {
port: config.REDIS_PORT,
host: config.REDIS_HOST,
...(config.REDIS_PASSWORD && {password: config.REDIS_PASSWORD}),
tls: config.REDIS_USE_TLS === 'true',
},
redis: {
port: config.REDIS_PORT,
host: config.REDIS_HOST,
db: config.REDIS_DB,
...(config.REDIS_PASSWORD && {password: config.REDIS_PASSWORD}),
tls: config.REDIS_USE_TLS === 'true',
},
};

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

app.use(session({secret: Math.random().toString(), resave: false, saveUninitialized: false}));
if (app.get('env') !== 'production') {
const morgan = require('morgan');
app.use(morgan('combined'));
}

app.use((req, res, next) => {
if (config.PROXY_PATH) req.proxyUrl = config.PROXY_PATH;
next();
});

const sessionOpts = {
name: 'bull-board.sid',
secret: Math.random().toString(),
resave: false,
saveUninitialized: false,
cookie: {
path: '/',
httpOnly: false,
secure: false
}
};

app.use(session(sessionOpts));
app.use(passport.initialize({}));
app.use(passport.session({}));

app.use(bodyParser.urlencoded({extended: false}));

if (config.AUTH_ENABLED) {
app.use(config.LOGIN_PAGE, authRouter);
app.use(config.HOME_PAGE, ensureLoggedIn(config.LOGIN_PAGE), router);
}
else {
app.use(config.HOME_PAGE, router);
app.use(config.LOGIN_PAGE, authRouter);
app.use(config.HOME_PAGE, ensureLoggedIn(config.PROXY_LOGIN_PAGE), router);
} else {
app.use(config.HOME_PAGE, router);
}

app.listen(config.PORT, () => {
Expand Down
4 changes: 2 additions & 2 deletions src/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ authRouter.route('/')
res.render('login');
})
.post(passport.authenticate('local', {
successRedirect: config.HOME_PAGE,
failureRedirect: config.LOGIN_PAGE,
successRedirect: config.PROXY_HOME_PAGE,
failureRedirect: config.PROXY_LOGIN_PAGE,
}));

exports.authRouter = authRouter;
23 changes: 23 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ balanced-match@^1.0.0:
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==

basic-auth@~2.0.1:
version "2.0.1"
resolved "https://registry.npm.taobao.org/basic-auth/download/basic-auth-2.0.1.tgz#b998279bf47ce38344b4f3cf916d4679bbf51e3a"
integrity sha1-uZgnm/R844NEtPPPkW1Gebv1Hjo=
dependencies:
safe-buffer "5.1.2"

[email protected], body-parser@^1.19.0:
version "1.19.0"
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
Expand Down Expand Up @@ -291,6 +298,11 @@ destroy@~1.0.4:
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"
integrity sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=

dotenv@^8.2.0:
version "8.2.0"
resolved "https://registry.npm.taobao.org/dotenv/download/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha1-l+YZJZradQ7qPk6j4mvO6lQksWo=

[email protected]:
version "1.1.1"
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
Expand Down Expand Up @@ -685,6 +697,17 @@ moment-timezone@^0.5.31:
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3"
integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==

morgan@^1.10.0:
version "1.10.0"
resolved "https://registry.npm.taobao.org/morgan/download/morgan-1.10.0.tgz#091778abc1fc47cd3509824653dae1faab6b17d7"
integrity sha1-CRd4q8H8R801CYJGU9rh+qtrF9c=
dependencies:
basic-auth "~2.0.1"
debug "2.6.9"
depd "~2.0.0"
on-finished "~2.3.0"
on-headers "~1.0.2"

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down

0 comments on commit 4c0c584

Please sign in to comment.