Skip to content

Commit

Permalink
Support PUBLIC_URL environment variable as URL prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
jezhiggins committed Sep 19, 2019
1 parent d32569b commit 80e7db6
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions web-server/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import process from 'process';
import express from 'express';
import fileUpload from 'express-fileupload';
import path from 'path';
Expand All @@ -8,6 +9,19 @@ import { StartAngelsWings } from './hashDB/AngelsWings';

const app = express();

function urlPrefix() {
let prefix = process.env.PUBLIC_URL;
if (prefix)
prefix = prefix.trim();

if (!prefix || prefix.length === 0)
return null;

if (prefix[0] === '/')
return prefix;
return `/${prefix}`;
}

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
Expand All @@ -18,11 +32,22 @@ app.use(fileUpload({
tempFileDir : '/tmp/'
}));

app.get('/', (req, res) => res.redirect(301, '/index.html'));
app.use(express.static(path.join(__dirname, 'static')));
const routes = express.Router()
routes.get('/', (req, res) => res.redirect(301, 'index.html'));
routes.use(express.static(path.join(__dirname, 'static')));

routes.post('/fingerprint', fingerprint);
routes.get('/authenticate', authenticate);

const prefix = urlPrefix()
if (prefix) {
app.get('/', (req, res) => res.redirect(301, `${prefix}/index.html`));
app.use(prefix, routes);
} else {
app.use('/', routes);
}

app.post('/fingerprint', fingerprint);
app.get('/authenticate', authenticate);
console.log(`url prefix is ${prefix}`);

StartAngelsWings();

Expand Down

0 comments on commit 80e7db6

Please sign in to comment.