Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit fe88f18

Browse files
committed
Node proxy for running submit locally
1 parent 571d697 commit fe88f18

File tree

6 files changed

+82
-1
lines changed

6 files changed

+82
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Integrates with [Sleepingpill](https://github.com/javaBin/sleepingPillCore) whic
99
* Check out repo: `git clone [email protected]:javaBin/submit.git`
1010
* Build backend: `cd backend && mvn clean install`
1111
* Build frontend: `cd frontend && npm install`
12+
* Build nodeproxy: `cd scripts/proxy && npm install`
1213
* Create config file and fill out password: `cp backend/configuration-template.yaml backend/configuration.yaml && vi backend/configuration.yaml`
1314
* Run the app: `./scripts/start.sh`
1415

frontend/static/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<div class='app'></div>
1414
<script src='app.js'></script>
1515
<script>
16-
var url = window.location.href.includes("localhost") ? "http://localhost:8080/api" : "/api";
16+
var url = "/api";
1717
console.log(url);
1818
Elm.Main.fullscreen({
1919
url: url

scripts/proxy.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
BASEDIR=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
4+
5+
# resolve symlinks
6+
while [ -h "$BASEDIR/$0" ]; do
7+
DIR=$(dirname -- "$BASEDIR/$0")
8+
SYM=$(readlink $BASEDIR/$0)
9+
BASEDIR=$(cd $DIR && cd $(dirname -- "$SYM") && pwd)
10+
done
11+
cd ${BASEDIR}
12+
13+
# Restarter nodeproxy om den dør...
14+
until node proxy/startnodeproxy.js $@; do
15+
sleep 1
16+
done

scripts/proxy/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "proxy",
3+
"version": "1.0.0",
4+
"description": "proxy setup",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"dependencies": {
12+
"colors": "^1.1.2",
13+
"http-proxy": "^1.11.2",
14+
"request": "^2.64.0"
15+
}
16+
}

scripts/proxy/startnodeproxy.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*global require, process */
2+
3+
/*
4+
* Submit Proxy (localhost:9000)
5+
* Setup: cd proxy && ./setup.sh
6+
*/
7+
8+
var fs = require("fs"),
9+
util = require("util"),
10+
url = require("url"),
11+
path = require("path"),
12+
exec = require('child_process').exec,
13+
http = require("http");
14+
15+
try {
16+
var colors = require("colors"),
17+
request = require("request"),
18+
httpProxy = require("http-proxy");
19+
} catch (e) {
20+
if(e.code === "MODULE_NOT_FOUND") {
21+
console.error("Fant ikke moduler for node proxy. Prøv deg på en npm install!");
22+
process.exit(1);
23+
} else {
24+
throw e;
25+
}
26+
}
27+
28+
/* ----------------------------------- */
29+
30+
var proxy = httpProxy.createProxyServer({});
31+
32+
http.createServer(function(req, res) {
33+
var pathname = url.parse(req.url).pathname;
34+
35+
var proxyAndLogRequest = function(proxyPort, appName) {
36+
console.log(pathname, '=>', 'proxying to', appName, '=>', url.parse(req.url).pathname);
37+
proxy.web(req, res, { target: 'http://localhost:' + proxyPort });
38+
};
39+
40+
if(pathname.startsWith('/api')) {
41+
proxyAndLogRequest(8080, 'backend');
42+
} else {
43+
proxyAndLogRequest(8000, 'frontend');
44+
}
45+
}).listen(9000, function() {
46+
console.log('proxy listening on port 9000');
47+
});

scripts/screen.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ defc1 off
3030
zombie kr # k: destory window, r: resurrect window
3131

3232
# Open initial screens
33+
screen -t proxy bash -c "pwd && cd scripts && ./proxy.sh"
3334
screen -t frontend bash -c "pwd && cd frontend && npm run watch"
3435
screen -t backend bash -c "cd backend && mvn clean install && mvn spring-boot:run"
3536

0 commit comments

Comments
 (0)