This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebsite.js
62 lines (56 loc) · 1.68 KB
/
website.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var express = require('express');
var app = express();
var path = require('path');
var cors = require('cors');
var bonjour = require('bonjour')();
var exec = require('exec');
var request = require('request');
cors({
credentials: true,
origin: true
});
app.use(cors()); // Support cross origin requests
bonjour.find(
{ type: 'http' },
function(service) {
console.log(JSON.stringify(service));
if (service.name == 'IceBox') {
app.get('/serviceip', function(req, res) {
res.json({ ip: service.host, port: service.port });
});
app.get('/doreset', function(req, res) {
console.log("calling reset.");
exec('./restart.sh', function(a, b, c) {
exec('./restart.sh', function(a, b, c) {
res.json({ok: true});
});
});
});
app.use('/proxy/:icebox/', function(req, res) {
if(req.params.icebox !== service.host + ':' + service.port) {
res.error(403);
}
var upstream_url = 'http://' + service.host + ':' + service.port + req.url;
req.pipe(request(upstream_url)).pipe(res);
});
}
});
app.get(
'/',
function(req, res) {
app.use(express.static(__dirname + '/static'));
res.sendFile(path.join(__dirname + '/static/index.html'));
});
app.get(
'/image',
function(req, res) {
app.use(express.static(__dirname + '/static/image'));
res.sendFile(path.join(__dirname + '/static/image/bottle.png'));
});
app.get(
'/isiceboxdown',
function(req, res) {
res.sendFile(path.join(__dirname + '/static/reset.html'));
});
console.log("starting to server on port " + (process.env.ICEBOX_WEB_PORT || 80));
app.listen(process.env.ICEBOX_WEB_PORT || 80);