Skip to content

Instantly share code, notes, and snippets.

@mrsimonemms
Last active August 11, 2016 07:51
Show Gist options
  • Save mrsimonemms/828d461eda58aef930e108d7f0aefb4b to your computer and use it in GitHub Desktop.
Save mrsimonemms/828d461eda58aef930e108d7f0aefb4b to your computer and use it in GitHub Desktop.
SPA hosting script
# To be used with the Dockerfile
server {
listen ${BS_SERVER_PORT};
server_name localhost;
# access_log /var/log/nginx/nginx.access.log;
# error_log /var/log/nginx/nginx.error.log;
root /opt/browserspy;
index index.html
charset utf-8;
location /api {
rewrite /api(.*) /$1 break;
proxy_pass ${BS_API_URL};
}
location /playback {
rewrite /playback(.*) /$1 break;
proxy_pass ${BS_PLAYBACK_URL};
}
location / {
try_files ${DOLLAR}uri ${DOLLAR}uri/ /index.html =404;
}
}
########################################
# Docker #
# #
# An Nginx container that enables the #
# application to run #
########################################
# Run with
# docker build -t nginx/server .
# docker run -it --rm --name web -p 7777:9999 nginx/server
FROM nginx:latest
MAINTAINER Simon Emms
# Environment variables
ENV BS_API_URL=https://api.browserspy.io
ENV BS_PLAYBACK_URL=https://playback.browserspy.io
ENV BS_SERVER_PORT=9999
# Set the work directory and add the project files to it
WORKDIR /opt/browserspy
ADD . /opt/browserspy
# Populate the config with the envvars
RUN DOLLAR='$' envsubst < /opt/browserspy/nginx/browserspy.conf > /etc/nginx/conf.d/browserspy.conf
# Expose the ports
EXPOSE 9999
/**
* server
*/
"use strict";
/* Node modules */
const util = require("util");
/* Third-party modules */
const bunyan = require("bunyan");
const datatypes = require("datautils").data;
const express = require("express");
const proxy = require("http-proxy-middleware");
/* Files */
const config = {
apiUrl: datatypes.setString(process.env.BS_API_URL, "http://localhost:9999"),
logLevel: datatypes.setString(process.env.BS_LOG_LEVEL, "trace"),
port: datatypes.setInt(process.env.BS_APP_PORT, 5000),
publicDir: datatypes.setString(process.env.BS_APP_PUBLIC_DIR, __dirname)
};
const logger = bunyan.createLogger({
name: "playback",
level: config.logLevel
});
const logProvider = () => {
/* Map log to debug */
logger.log = logger.debug;
return logger;
};
const app = express();
/* Treat anything starting /socket.io as a websocket */
app.use("/socket.io", proxy({
changeOrigin: true,
target: config.apiUrl,
ws: true,
logProvider
}));
/* Treat anything starting /api as an API call */
app.use("/api/", proxy({
changeOrigin: true,
pathRewrite: {
"^/api/": "/"
},
target: config.apiUrl,
logProvider
}));
/* Serve up any static files */
app.use(express.static(config.publicDir));
/* For everything else, defer to the index */
app.all("/*", (req, res) => {
res.sendFile(config.publicDir + "/player.html");
});
/* Start up the server */
app.listen(config.port, () => {
util.log("Listening on port %s", config.port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment