@@ -7,14 +7,39 @@ Just start with `node server.js`, then navigate to `localhost:8080` in a web bro
7
7
The Web IDE should start up over the network and work like normal.
8
8
9
9
*/
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
+
10
39
var WebSocketServer = require ( 'websocket' ) . server ;
11
40
var http = require ( 'http' ) ;
12
-
13
41
var connection ;
14
42
15
- var Espruino = { Config : { } , Core : { } , Plugins : { } } ;
16
- Espruino . Config . BLUETOOTH_LOW_ENERGY = true ;
17
-
18
43
Espruino . callProcessor = function ( a , b , cb ) { cb ( ) ; }
19
44
Espruino . Core . Status = {
20
45
setStatus : function ( t , l ) { console . log ( ":" + t ) ; } ,
@@ -43,7 +68,7 @@ Espruino.Core.Serial.startListening(function(data) {
43
68
} ) ;
44
69
45
70
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 ) ;
47
72
var url = request . url . toString ( ) ;
48
73
if ( url == "/" ) url = "/main.html" ;
49
74
if ( url == "/serial/ports" ) {
@@ -63,7 +88,7 @@ var server = http.createServer(function(request, response) {
63
88
}
64
89
65
90
if ( require ( "fs" ) . existsSync ( path ) ) {
66
- console . log ( "Serving file " , path ) ;
91
+ // console.log("Serving file ",path);
67
92
require ( "fs" ) . readFile ( path , function ( err , blob ) {
68
93
var mime ;
69
94
if ( path . substr ( - 4 ) == ".css" ) mime = "text/css" ;
@@ -89,8 +114,9 @@ var server = http.createServer(function(request, response) {
89
114
response . writeHead ( 404 ) ;
90
115
response . end ( ) ;
91
116
} ) ;
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 ) ;
94
120
} ) ;
95
121
96
122
wsServer = new WebSocketServer ( {
@@ -105,7 +131,6 @@ function originIsAllowed(origin) {
105
131
106
132
wsServer . on ( 'request' , function ( request ) {
107
133
if ( ! originIsAllowed ( request . origin ) ) {
108
- // Make sure we only accept requests from an allowed origin
109
134
request . reject ( ) ;
110
135
console . log ( ( new Date ( ) ) + ' Connection from origin ' + request . origin + ' rejected.' ) ;
111
136
return ;
0 commit comments