Skip to content

Commit 0efe9f1

Browse files
committed
Add server initialisation options
1 parent 4efe297 commit 0efe9f1

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

electron-app.js

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@
44

55
var DEV = false; // are we developing? Adds frame and dev tools
66

7+
// ----------------------------------------------------
8+
function help() {
9+
console.log("Espruino Web IDE");
10+
console.log(" USAGE:");
11+
console.log(" --help This help screen");
12+
console.log(" --dev Enable developer tools");
13+
process.exit(0);
14+
}
15+
// ---------------------------------------------------- arg parsing
16+
for (var i=2;i<process.argv.length;i++) {
17+
var arg = process.argv[i];
18+
if (arg=="--dev") {
19+
DEV = true;
20+
} else {
21+
if (arg!="--help") console.log("Unknown argument "+arg);
22+
help();
23+
}
24+
}
25+
// ----------------------------------------------------
26+
727
try {
828
global.electron = require('electron');
929
} catch (e) {

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "A Terminal and Graphical code Editor for Espruino JavaScript Microcontrollers",
55
"main": "index.js",
66
"bin": {
7-
"espruino-web-ide": "./electron-loader.js"
7+
"espruino-web-ide": "./electron-loader.js",
8+
"espruino-server": "./server.js"
89
},
910
"scripts": {
1011
"start": "electron ./electron-app.js"

server.js

+34-9
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,39 @@ Just start with `node server.js`, then navigate to `localhost:8080` in a web bro
77
The Web IDE should start up over the network and work like normal.
88
99
*/
10+
var Espruino = { Config : {}, Core : {}, Plugins : {} };
11+
var SERVER_PORT = 8080;
12+
Espruino.Config.BLUETOOTH_LOW_ENERGY = true;
13+
14+
// ----------------------------------------------------
15+
function help() {
16+
console.log("Espruino Web IDE Server");
17+
console.log(" USAGE:");
18+
console.log(" --help This help screen");
19+
console.log(" --port ### Listen on the given port (default 8080)");
20+
process.exit(0);
21+
}
22+
// ---------------------------------------------------- arg parsing
23+
for (var i=2;i<process.argv.length;i++) {
24+
var arg = process.argv[i];
25+
if (arg=="--port") {
26+
SERVER_PORT = parseInt(process.argv[++i]);
27+
if (!(SERVER_PORT>0 && SERVER_PORT<65536)) {
28+
console.log("Invalid port "+JSON.stringify(process.argv[i]));
29+
help();
30+
}
31+
} else {
32+
if (arg!="--help") console.log("Unknown argument "+arg);
33+
help();
34+
}
35+
}
36+
// ----------------------------------------------------
37+
38+
1039
var WebSocketServer = require('websocket').server;
1140
var http = require('http');
12-
1341
var connection;
1442

15-
var Espruino = { Config : {}, Core : {}, Plugins : {} };
16-
Espruino.Config.BLUETOOTH_LOW_ENERGY = true;
17-
1843
Espruino.callProcessor = function(a,b,cb) { cb(); }
1944
Espruino.Core.Status = {
2045
setStatus : function(t,l) { console.log(":"+t); },
@@ -43,7 +68,7 @@ Espruino.Core.Serial.startListening(function(data) {
4368
});
4469

4570
var server = http.createServer(function(request, response) {
46-
console.log((new Date()) + ' Received request for ' + request.url);
71+
console.log((new Date()) + ' HTTP '+request.method+' ' + request.url);
4772
var url = request.url.toString();
4873
if (url == "/") url = "/main.html";
4974
if (url == "/serial/ports") {
@@ -63,7 +88,7 @@ var server = http.createServer(function(request, response) {
6388
}
6489

6590
if (require("fs").existsSync(path)) {
66-
console.log("Serving file ",path);
91+
//console.log("Serving file ",path);
6792
require("fs").readFile(path, function(err, blob) {
6893
var mime;
6994
if (path.substr(-4)==".css") mime = "text/css";
@@ -89,8 +114,9 @@ var server = http.createServer(function(request, response) {
89114
response.writeHead(404);
90115
response.end();
91116
});
92-
server.listen(8080, function() {
93-
console.log((new Date()) + ' Server is listening on port 8080');
117+
118+
server.listen(SERVER_PORT, function() {
119+
console.log((new Date()) + ' Server is listening on port '+SERVER_PORT);
94120
});
95121

96122
wsServer = new WebSocketServer({
@@ -105,7 +131,6 @@ function originIsAllowed(origin) {
105131

106132
wsServer.on('request', function(request) {
107133
if (!originIsAllowed(request.origin)) {
108-
// Make sure we only accept requests from an allowed origin
109134
request.reject();
110135
console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
111136
return;

0 commit comments

Comments
 (0)