jsgui3-server 0.0.91 → 0.0.93

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/server.js CHANGED
@@ -1,903 +1,383 @@
1
- //var sockjs = require('sockjs'), jsgui = require('jsgui3-html'),
2
1
  const jsgui = require('jsgui3-html'),
3
- //os = require('os'),
4
- http = require('http'),
5
- https = require('https'),
6
- Resource = jsgui.Resource,
7
- Server_Resource_Pool = require('./resources/server-resource-pool'),
8
- // routing-tree? make a generalised module?
9
- Router = jsgui.Router,
10
- Website_Resource = require('./resources/website-resource'),
11
- Info = require('./resources/local-server-info-resource'),
12
- Server_Page_Context = require('./page-context'), {Evented_Class, each, tof} = jsgui;
13
-
2
+ http = require('http'),
3
+ https = require('https'),
4
+ {prop, read_only} = require('obext'),
5
+ Resource = jsgui.Resource,
6
+ Server_Resource_Pool = require('./resources/server-resource-pool'),
7
+ Router = jsgui.Router,
8
+ Website_Resource = require('./resources/website-resource'),
9
+ Info = require('./resources/local-server-info-resource'),
10
+ Server_Page_Context = require('./page-context'),
11
+ {
12
+ Evented_Class,
13
+ each,
14
+ tof
15
+ } = jsgui;
14
16
  const lib_path = require('path');
15
-
16
17
  const Web_Admin_Page_Control = require('./controls/page/admin');
17
18
  const Web_Admin_Panel_Control = require('./controls/panel/admin');
18
-
19
19
  const Website = require('./website/website');
20
20
  const HTTP_Website_Publisher = require('./publishing/http-website-publisher');
21
21
  const Webpage = require('./website/webpage');
22
-
23
- // And the HTTP web page publisher?
24
-
25
- // A Server Application could be a reasonable abstraction.
26
- // The application itself gets programmed within such a structure.
27
- // Basically gets defined, rather than lots of calls made to the server to set things up.
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
- // Operating JSGUI_Server as a proxy server or with improved proxy capabilities may help.
36
-
37
- // The router is the same as on the client.
38
-
39
- // Could make JSGUI 3 Server generally more flexible and configurable.
40
- // To work as a proxy server.
41
- // To route to different ports / apps depending on the 'host'.
42
-
43
-
44
- // Could make this use / include C++ and Rust WASM tools.
45
- // Maybe some OpenCL for running algos on the server.
46
-
47
-
48
- // jsgui.compile(source, {input_lang, output_lang})
49
- // c, c++, rust all to wasm compilation would be very nice.
50
-
51
- // A server admin web application should go in somewhere.
52
- // Probably (just) a control.
53
-
54
- // Want to make this easier to use and program in.
55
-
56
- // 0.0.83 will be an ease of use release.
57
-
58
- // JSGUI_Server automatically having its admin website or website part, or it's an easy setting to make.
59
-
60
-
61
- // Going to tidy up / change the code here.
62
- // Don't have the JSGUI_Server class itself doing nearly as much
63
- // Will use abstractions that provide their own syntax sugar.
64
-
65
- // Looks like quite a core class for the moment.
66
-
67
22
  class JSGUI_Server extends Evented_Class {
68
- constructor(spec = {website: true}, __type_name) {
69
- // Default operations mode...
70
- // Has its own website resource.
71
- // All gets routed to that resource automatically.
72
- //
73
- super();
74
-
75
-
76
- // disk_path_client_js
77
- // would be a useful basic option as single page website and single control website have been retired.
78
-
79
- let disk_path_client_js;
80
-
81
- if (spec.disk_path_client_js) {
82
- disk_path_client_js = spec.disk_path_client_js;
83
- };
84
-
85
- Object.defineProperty(this, 'disk_path_client_js', {
86
- get() {
87
- return disk_path_client_js;
88
- },
89
- set(value) {
90
- disk_path_client_js = value;
91
- }
92
- });
93
-
94
- //console.log('disk_path_client_js', disk_path_client_js);
95
-
96
-
97
- let Ctrl;
98
- if (spec.Ctrl) {
99
- Ctrl = spec.Ctrl;
100
- };
101
- Object.defineProperty(this, 'Ctrl', {
102
- get() {
103
- return Ctrl;
104
- },
105
- set(value) {
106
- Ctrl = value;
107
- }
108
- });
109
-
110
-
111
- let name;
112
-
113
- // Could check for 'single control' mode.
114
- // Would only serve a single control (at least to start with).
115
- // A quick way of serving content.
116
-
117
- /*
118
- Ctrl: Demo_UI,
119
- 'client_package': require.resolve('./square_box.js')
120
- */
121
-
122
-
123
-
124
- if (spec.name) {
125
- name = spec.name;
126
- };
127
-
128
- Object.defineProperty(this, 'name', {
129
- get() {
130
- return name;
131
- },
132
- set(value) {
133
- name = value;
134
- }
135
- });
136
-
137
- //let is_website = spec.website === true;
138
-
139
- this.__type_name = __type_name || 'server';
140
-
141
- // Could make Server Resource Pool optional.
142
- // (though it does seem very useful as a concept).
143
-
144
- const resource_pool = this.resource_pool = new Server_Resource_Pool({
145
- // Other things can be made available through the server resource pool.
146
- 'access': {
147
- 'full': ['server_admin']
148
- }
149
- });
150
- //Object.defineProperty('')
151
- // Maybe the server router should explicitly be a Resource?
152
- // Or just treat Objects the same way as Data_Object (if possible) in Collection.
153
-
154
- // Interestingly, router is not a part of jsgui3-server itself.
155
-
156
-
157
-
158
- const server_router = this.server_router = new Router({
159
- 'name': 'Server Router',
160
- 'pool': resource_pool
161
- });
162
- resource_pool.add(server_router);
163
-
164
-
165
-
166
- if (spec.https_options) {
167
- this.https_options = spec.https_options;
168
- }
169
-
170
- if (spec.routes) {
171
- // Create multiple websites.
172
- // Maybe a Website_Group.
173
-
174
- throw 'NYI - will use Website class rather than Website_Resource here'
175
-
176
- each(spec.routes, (app_spec, route) => {
177
- var app = this.app = new Website_Resource(app_spec);
178
- resource_pool.add(app);
179
- server_router.set_route(route, app, app.process);
180
- });
181
- }
182
-
183
- // Multiple websites - perhaps proxying to them.
184
- //console.log('is_website', is_website);
185
- if (true) {
186
- // No, create the Website object and the HTTP_Website_Publisher.
187
- // Could make a Website_Resource that wraps it and goes in the resource pool.
188
-
189
- // A new Website object won't process HTTP requests
190
- // An HTTP_Website_Publisher will though.
191
-
192
- const old = () => {
193
- const app = this.app = new Website_Resource({
194
- name: 'Website'
195
- });
196
- //console.log('app', app);
197
- resource_pool.add(app);
198
- // Then with setting routes - seems OK to direct all to the app process.
199
- // Could bypass the server router, or set the server router to be the app's router
200
- // Replace the server router with the app's router.
201
-
202
- //server_router.set_route('/*', app, app.process);
203
- server_router.set_route('/*', app.process);
204
- }
205
- // Or could publish a single page website.
206
-
207
- const current = () => {
208
- // Could have got spec for website content in the spec {}.
209
- // But don't have system to use that yet, or to specify it.
210
-
211
- // And a Resource for the Website Publisher too?
212
-
213
- // And the website could have it's single control?
214
- const opts_website = {
215
- 'name': this.name || 'Website'
216
- };
217
-
218
- if (Ctrl) {
219
- opts_website.content = Ctrl;
220
- }
221
-
222
- const ws_app = this.app = new Website(opts_website);
223
-
224
- // The publisher, now it uses the bundler, is clearer in terms of what it needs to do
225
- // publisher needs to do a lot, but moving code to specific modules makes the publisher code higher level.
226
-
227
- // Could give the publisher the client package js (path).
228
-
229
- const opts_ws_publisher = {
230
- 'website': ws_app
231
- };
232
-
233
- if (disk_path_client_js) {
234
- opts_ws_publisher.disk_path_client_js = disk_path_client_js;
235
- }
236
-
237
-
238
- const ws_publisher = new HTTP_Website_Publisher(opts_ws_publisher);
239
-
240
- // but need to wait until that website publisher is ready.
241
-
242
-
243
-
244
-
245
- // ws publisher could have website admin (web) tools.
246
- // website admin web interface.
247
-
248
- ws_publisher.on('ready', () => {
249
- console.log('ws publisher is ready');
250
-
251
- // server not started yet though?
252
-
253
- const ws_resource = new Website_Resource({
254
- 'name': 'Website Resource',
255
- 'website': ws_app
256
- });
257
- resource_pool.add(ws_resource);
258
- //server_router.set_route('/*', ws_app, ws_publisher.handle_http);
259
- //server_router.set_route('/*', ws_publisher.handle_http);
260
-
261
- // Should render the Ctrl homepage at some point.
262
-
263
- server_router.set_route('/*', ws_publisher, ws_publisher.handle_http);
264
- //throw 'stop';
265
-
266
- this.raise('ready');
267
-
268
-
269
- });
270
-
271
-
272
-
273
-
274
-
275
- //console.log('route has been set');
276
- // ws_publisher.start();
277
- }
278
- current();
279
-
280
-
281
-
282
- }
283
-
284
- // a 'router' property that returns the server router.
285
-
286
- Object.defineProperty(this, 'router', {
287
- get() {
288
- return server_router;
289
- }
290
- });
291
- // Don't want an admin web page by default (yet).
292
- }
293
- get resource_names() {
294
- //console.log('this.resource_pool', this.resource_pool);
295
- return this.resource_pool.resource_names;
296
- }
297
-
298
- // could have those pairs. Possible websocket ports? websockets uses same ports: 80, 443.
299
- // http_port, https_port
300
-
301
-
302
- // will change to observable API.
303
-
304
- 'start'(port, callback, fnProcessRequest) {
305
-
306
- if (tof(port) !== 'number') {
307
- console.trace();
308
- throw 'stop';
309
- }
310
-
311
- //throw 'stop';
312
- // The resource_pool is not just a Data_Value. need to fix some get or create new field value code.
313
- //console.log('start');
314
-
315
- //jsgui.note('server', 'start');
316
-
317
- // Maybe does not need a resource pool to begin with.
318
- //
319
-
320
- // Cool if this returned a promise or observable....
321
- // obs would make most sense for what to return when starting a server.
322
-
323
- const rp = this.resource_pool;
324
- if (!rp) {
325
- throw 'stop';
326
- }
327
-
328
- rp.start(err => {
329
- if (err) {
330
- throw err;
331
- } else {
332
-
333
- // Easy abstraction for starting on multiple ports?
334
- // Seems like an important part, as it actually has the first HTTP request<>response function.
335
- // Could send it to a more extended HTTP processing part of the server.
336
-
337
- // Don't have http-server-publisher (yet).
338
-
339
- const lsi = rp.get_resource('Local Server Info');
340
- const server_router = rp.get_resource('Server Router');
341
-
342
- /*
343
- var resource_names = rp.resource_names;
344
- var js = rp.get_resource('Site JavaScript');
345
- var css = rp.get_resource('Site CSS');
346
- var images = rp.get_resource('Site Images');
347
- var audio = rp.get_resource('Site Audio');
348
- var login = rp.get_resource('Login HTML Resource');
349
- var admin = rp.get_resource('Web Admin');
350
- var sock_router = rp.get_resource('Server Sock Router');
351
- */
352
-
353
- // just lsi.get?
354
-
355
- lsi.getters.net((err, net) => {
356
- if (err) {
357
- callback(err);
358
- } else {
359
- //console.log('net', net);
360
- // Could host on local.
361
- // Host on every ipv4 address for the moment.
362
- var arr_ipv4_addresses = [];
363
- each(net, (arr_addresses, name) => {
364
- each(arr_addresses, (obj_address) => {
365
- if (obj_address.family === 'IPv4') {
366
- arr_ipv4_addresses.push(obj_address.address);
367
- }
368
- })
369
- });
370
- let num_to_start = arr_ipv4_addresses.length;
371
- if (this.https_options) {
372
- each(arr_ipv4_addresses, (ipv4_address) => {
373
- var https_server = https.createServer(this.https_options, function (req, res) {
374
- //console.log('process server request.url', req.url);
375
- var server_routing_res = server_router.process(req, res);
376
- //console.log('server_routing_res', server_routing_res);
377
- });
378
- https_server.timeout = 10800000;
379
- //https_server.listen(443, ipv4_address);
380
- https_server.listen(port, ipv4_address);
381
- console.log('* Server running at https://' + ipv4_address + ':' + port + '/');
382
- num_to_start--;
383
- if (num_to_start === 0) {
384
- if (callback) callback(null, true);
385
- }
386
- });
387
- } else {
388
- each(arr_ipv4_addresses, (ipv4_address) => {
389
- var http_server = http.createServer(function (req, res) {
390
- var server_routing_res = server_router.process(req, res);
391
- });
392
- http_server.timeout = 10800000;
393
- http_server.listen(port, ipv4_address);
394
- console.log('* Server running at http://' + ipv4_address + ':' + port + '/');
395
- num_to_start--;
396
- //console.log('num_to_start', num_to_start);
397
- if (num_to_start === 0) {
398
- if (callback) callback(null, true);
399
- }
400
- });
401
- }
402
- //throw 'stop';
403
- }
404
- });
405
- }
406
- });
407
- }
408
-
409
- // Maybe will not have that function?
410
- // Could pass it directly to the server router.
411
- 'process_request'(req, res) {
412
-
413
- throw 'request should be processed elsewhere, such as the router'
414
-
415
- var url = req.url;
416
- //console.log('*** server process_request url ' + url);
417
- var s_url = url.split('/');
418
- var a_path = [];
419
- each(s_url, (v, i) => {
420
- if (v.length > 0) {
421
- a_path.push(v);
422
- }
423
- });
424
- var router = this.get('router');
425
- if (a_path.length > 0) {
426
- var routing_res = router.process(req, res);
427
- } else {
428
- console.log('need to process short path');
429
- }
430
- }
431
-
432
- // Maybe... but the server will serve a website, maybe webpage
433
- // This is an HTTP request handler. Want to use various pieces of HTTP handling code, probably call it from here.
434
-
435
- // Probably better to do a bundle process for that page / document, so that all referenced resources are known to be / made to be
436
- // available.
437
-
438
-
439
- 'serve_document'(req, res, jsgui_html_document) {
440
- throw 'deprecating.';
441
- var html = jsgui_html_document.all_html_render();
442
- var mime_type = 'text/html';
443
- //console.log('mime_type ' + mime_type);
444
- res.writeHead(200, {
445
- 'Content-Type': mime_type
446
- });
447
- res.end(html, 'utf-8');
448
- }
449
-
450
- // Properties and functions to better interact with what the server is hosting.
451
- // ms_uptime property
452
-
453
-
454
-
23
+ constructor(spec = {
24
+ website: true
25
+ }, __type_name) {
26
+ super();
27
+ let disk_path_client_js;
28
+ if (spec.disk_path_client_js) {
29
+ disk_path_client_js = spec.disk_path_client_js;
30
+ };
31
+ Object.defineProperty(this, 'disk_path_client_js', {
32
+ get() {
33
+ return disk_path_client_js;
34
+ },
35
+ set(value) {
36
+ disk_path_client_js = value;
37
+ }
38
+ });
39
+ let Ctrl;
40
+ if (spec.Ctrl) {
41
+ Ctrl = spec.Ctrl;
42
+ };
43
+
44
+
45
+ Object.defineProperty(this, 'Ctrl', {
46
+ get() {
47
+ return Ctrl;
48
+ },
49
+ set(value) {
50
+ Ctrl = value;
51
+ }
52
+ });
455
53
 
54
+
55
+ let name;
56
+ if (spec.name) {
57
+ name = spec.name;
58
+ };
59
+ Object.defineProperty(this, 'name', {
60
+ get() {
61
+ return name;
62
+ },
63
+ set(value) {
64
+ name = value;
65
+ }
66
+ });
67
+ this.__type_name = __type_name || 'server';
68
+ const resource_pool = this.resource_pool = new Server_Resource_Pool({
69
+ 'access': {
70
+ 'full': ['server_admin']
71
+ }
72
+ });
73
+ const server_router = this.server_router = new Router({
74
+ 'name': 'Server Router',
75
+ 'pool': resource_pool
76
+ });
77
+ resource_pool.add(server_router);
78
+ if (spec.https_options) {
79
+ this.https_options = spec.https_options;
80
+ }
81
+ if (spec.routes) {
82
+ throw 'NYI - will use Website class rather than Website_Resource here'
83
+ each(spec.routes, (app_spec, route) => {
84
+ var app = this.app = new Website_Resource(app_spec);
85
+ resource_pool.add(app);
86
+ server_router.set_route(route, app, app.process);
87
+ });
88
+ }
89
+ if (true) {
90
+ const old = () => {
91
+ const app = this.app = new Website_Resource({
92
+ name: 'Website'
93
+ });
94
+ resource_pool.add(app);
95
+ server_router.set_route('/*', app.process);
96
+ }
97
+ const current = () => {
98
+ const opts_website = {
99
+ 'name': this.name || 'Website'
100
+ };
101
+ if (Ctrl) {
102
+ opts_website.content = Ctrl;
103
+ }
104
+ const ws_app = this.app = new Website(opts_website);
105
+ const opts_ws_publisher = {
106
+ 'website': ws_app
107
+ };
108
+ if (disk_path_client_js) {
109
+ opts_ws_publisher.disk_path_client_js = disk_path_client_js;
110
+ }
111
+ const ws_publisher = new HTTP_Website_Publisher(opts_ws_publisher);
112
+ ws_publisher.on('ready', () => {
113
+ console.log('ws publisher is ready');
114
+ const ws_resource = new Website_Resource({
115
+ 'name': 'Website Resource',
116
+ 'website': ws_app
117
+ });
118
+ resource_pool.add(ws_resource);
119
+ server_router.set_route('/*', ws_publisher, ws_publisher.handle_http);
120
+ this.raise('ready');
121
+ });
122
+ }
123
+ current();
124
+ }
125
+ Object.defineProperty(this, 'router', {
126
+ get() {
127
+ return server_router;
128
+ }
129
+ });
130
+ }
131
+ get resource_names() {
132
+ return this.resource_pool.resource_names;
133
+ }
134
+ 'start' (port, callback, fnProcessRequest) {
135
+ if (tof(port) !== 'number') {
136
+ console.trace();
137
+ throw 'stop';
138
+ }
139
+ const rp = this.resource_pool;
140
+ if (!rp) {
141
+ throw 'stop';
142
+ }
143
+ rp.start(err => {
144
+ if (err) {
145
+ throw err;
146
+ } else {
147
+ const lsi = rp.get_resource('Local Server Info');
148
+ const server_router = rp.get_resource('Server Router');
149
+ lsi.getters.net((err, net) => {
150
+ if (err) {
151
+ callback(err);
152
+ } else {
153
+ var arr_ipv4_addresses = [];
154
+ each(net, (arr_addresses, name) => {
155
+ each(arr_addresses, (obj_address) => {
156
+ if (obj_address.family === 'IPv4') {
157
+ arr_ipv4_addresses.push(obj_address.address);
158
+ }
159
+ })
160
+ });
161
+ let num_to_start = arr_ipv4_addresses.length;
162
+ if (this.https_options) {
163
+ each(arr_ipv4_addresses, (ipv4_address) => {
164
+ var https_server = https.createServer(this.https_options, function(req, res) {
165
+ var server_routing_res = server_router.process(req, res);
166
+ });
167
+ https_server.timeout = 10800000;
168
+ https_server.listen(port, ipv4_address);
169
+ console.log('* Server running at https://' + ipv4_address + ':' + port + '/');
170
+ num_to_start--;
171
+ if (num_to_start === 0) {
172
+ if (callback) callback(null, true);
173
+ }
174
+ });
175
+ } else {
176
+ each(arr_ipv4_addresses, (ipv4_address) => {
177
+ var http_server = http.createServer(function(req, res) {
178
+ var server_routing_res = server_router.process(req, res);
179
+ });
180
+ http_server.timeout = 10800000;
181
+ http_server.listen(port, ipv4_address);
182
+ console.log('* Server running at http://' + ipv4_address + ':' + port + '/');
183
+ num_to_start--;
184
+ if (num_to_start === 0) {
185
+ if (callback) callback(null, true);
186
+ }
187
+ });
188
+ }
189
+ }
190
+ });
191
+ }
192
+ });
193
+ }
194
+ 'process_request' (req, res) {
195
+ throw 'request should be processed elsewhere, such as the router'
196
+ var url = req.url;
197
+ var s_url = url.split('/');
198
+ var a_path = [];
199
+ each(s_url, (v, i) => {
200
+ if (v.length > 0) {
201
+ a_path.push(v);
202
+ }
203
+ });
204
+ var router = this.get('router');
205
+ if (a_path.length > 0) {
206
+ var routing_res = router.process(req, res);
207
+ } else {
208
+ console.log('need to process short path');
209
+ }
210
+ }
211
+ 'serve_document' (req, res, jsgui_html_document) {
212
+ throw 'deprecating.';
213
+ var html = jsgui_html_document.all_html_render();
214
+ var mime_type = 'text/html';
215
+ res.writeHead(200, {
216
+ 'Content-Type': mime_type
217
+ });
218
+ res.end(html, 'utf-8');
219
+ }
456
220
  }
457
-
458
- //console.log('!!2)jsgui', !!jsgui);
459
221
  JSGUI_Server.HTML = require('jsgui3-html');
460
222
  JSGUI_Server.Resource = Resource;
461
223
  JSGUI_Server.Page_Context = Server_Page_Context;
462
224
  JSGUI_Server.Server_Page_Context = Server_Page_Context;
463
225
  JSGUI_Server.Website_Resource = Website_Resource;
464
-
465
- // Maybe jsgui3-website should be its own module.
466
- // Would (completely?) abstract away from the server.
467
-
468
- // And maybe jsgui3-webpage
469
- // Could be integrated within jsgui3-server.
470
-
471
-
472
-
473
-
474
-
475
226
  module.exports = JSGUI_Server;
476
-
477
227
  if (require.main === module) {
478
- //console.log('called directly');
479
-
480
- const args = process.argv.slice(2);
481
- let port = 80;
482
-
483
- if (args.length === 1) {
484
- const i = parseInt(args[0]);
485
- if (typeof i === 'number') {
486
- port = i;
487
- }
488
- }
489
-
490
- // Can give it a bit of content to start with....?
491
-
492
- const server = new JSGUI_Server({
493
- name: 'jsgui3 server (command line)'
494
- });
495
-
496
- const current = () => {
497
- //console.log('server', server);
498
-
499
-
500
- // And this Webpage itself is not a Control.
501
- // It would need to specify it uses Control rendering (I think).
502
-
503
-
504
- //const wp = new Webpage({
505
- // 'name': 'Starter Webpage'
506
- //});
507
-
508
- // log various pieces of info / data from the server.
509
- // Maybe its resources are the more accessible place to get data from.
510
-
511
-
512
- // server.host(wp, '/');
513
-
514
-
515
- // Better to start the server once the bundling is done...?
516
-
517
- server.start(8080);
518
-
519
- // May change to a core and extended server.
520
- // Extended server could have functions (via mixin) such as serve_file that are above the routing and HTTP abstraction
521
- // For the moment, keep the jsgui-server as core as possible. Maybe call it server-core?
522
-
523
-
524
-
525
-
526
- }
527
- current();
528
-
529
- const old = () => {
530
-
531
- // Server should already have a Website_Resource.
532
- // Possibly just for its own admin.
533
-
534
-
535
-
536
-
537
- /*
538
- var app = new Website_Resource({
539
- 'name': 'html-server'
540
- });
541
- //this.publish = (...args) => app.publish(...args);
542
- //console.log('app', app);
543
- //throw 'stop';
544
- this.resource_pool.add(app);
545
- this.server_router.set_route('*', app, app.process);
546
- this.app_server = app;
547
- */
548
-
549
- //const web_admin = new Web_Admin_Page_Control({});
550
-
551
- // Maybe server.route should be a function.
552
- // but able to route by host values in incoming messages.
553
-
554
- //server_router.set_route(route, app, app.process);
555
-
556
- // Though needs to send it to something to render the control.
557
- // a function of some sort.
558
- // Could improve router / control functionality to respond to an HTTP request.
559
-
560
-
561
-
562
- //server.router.set_route('admin/*', web_admin);
563
-
564
-
565
- // A control publisher maybe?
566
-
567
-
568
- //server.route('/admin/*', web_admin);
569
-
570
- //
571
- const app_admin = new Website_Resource({
572
- name: 'Admin Website'
573
- });
574
- //console.log('app_admin', app_admin);
575
-
576
- // Need to serve a JS client to that app.
577
- // The /controls/page/admin.js app.
578
-
579
- // app_admin.serve_js()
580
-
581
-
582
- // Should be possible to programatically make / configure the app before serving it.
583
- // Choose its controls.
584
-
585
-
586
- server.resource_pool.add(app_admin);
587
-
588
- // Need to better set up Control publishing
589
-
590
- //server.router.set_route('/*', app, app.process);
591
- //server.router.set_route('admin/*', app_admin, app_admin.process);
592
-
593
-
594
- // The admin website resource won't have it's own resource pool.
595
-
596
- // Will deal with serving JS and other content in a more intuitive / easy to code way.
597
-
598
- let js = app_admin.resource_pool['Site JavaScript'];
599
-
600
- //console.log('js', js);
601
-
602
- //let js_client = this.client_package || this.js_client || 'jsgui3-client';
603
-
604
- // Need to more easily / by default serve the suitable JS client app?
605
- // Want it with one property setting or command. Not quite by default. Could be default in various configs.
606
-
607
-
608
- // server.serve_js_client
609
- // will a single js client cover all the served pages / site parts?
610
- // an admin.js may be more appropriate, in some cases path specific JS.
611
-
612
- // Further work on compiler / transformer type resources.
613
- // Even 'publisher', as this part is about publishing the correct js in the correct format.
614
-
615
- // Compilation and bundling seems most important for this type of server app.
616
-
617
- // server.bundle_control
618
-
619
-
620
- let js_client = lib_path.resolve('./controls/page/admin.js');
621
-
622
- let o_serve_package = {
623
- 'babel': 'mini'
624
- }
625
-
626
- // Would need to set the route in the router as well.
627
-
628
-
629
-
630
- js.serve_package('admin/js/app.js', js_client, o_serve_package, (err, served) => {
631
- if (err) {
632
- console.log('err', err);
633
- throw 'stop';
634
- } else {
635
- if (served) {
636
- console.log('served', served);
637
-
638
-
639
-
640
- const prepare_app = () => {
641
-
642
- const server_router = server.router;
643
-
644
- if (!server_router) {
645
- throw 'no server_router';
646
- }
647
- var routing_tree = server_router.routing_tree;
648
-
649
- // route /js to the js resource?
650
-
651
- routing_tree.set('admin', (req, res) => {
652
- //console.log('root path / request');
653
- const o_spc = {
654
- 'req': req,
655
- 'res': res,
656
- 'resource_pool': app_admin.resource_pool
657
- }
658
-
659
- if (this.include_server_ref_in_page_context) o_spc.server = this;
660
- var server_page_context = new Server_Page_Context(o_spc);
661
- // then merge the context data :)
662
- if (this.context_data) {
663
- Object.assign(server_page_context, this.context_data);
664
- }
665
- // and .server property?
666
- // a different way to get the server info to the components is needed.
667
-
668
- // Page_Bounds_Specifier
669
- var hd = new jsgui.Client_HTML_Document({
670
- 'context': server_page_context
671
- });
672
- hd.include_client_css();
673
- hd.include_css('/admin/css/basic.css');
674
- //hd.include_css('/css/controls.css');
675
-
676
- // include compiled css too.
677
- // not much of it yet.
678
-
679
- // Will be a separate CSS file, generated upon app start.
680
- if (this.css) {
681
- each(this.css, (path, serve_as) => {
682
- //css.serve(serve_as, path);
683
- hd.include_css('/admin/css/' + serve_as);
684
- });
685
- }
686
-
687
- const body = hd.body;
688
- let o_params = this.params || {};
689
- Object.assign(o_params, {
690
- 'context': server_page_context
691
- });
692
- //console.log('o_params', o_params);
693
- //console.log('this.Ctrl', this.Ctrl);
694
-
695
-
696
- const ctrl = this.ctrl = new Web_Admin_Panel_Control(o_params);
697
- ctrl.active();
698
- //var ctrl2 = new jsgui.Control({});
699
- body.add(ctrl);
700
-
701
- // it will be a client-side function.
702
-
703
- // Should not use 'add' here.
704
- // it's the script content.
705
-
706
- // want to get around that escaping.
707
- // options escaping / escape : false
708
-
709
- hd.include_js('/admin/js/app.js');
710
-
711
- // Would this be a place to register icons?
712
- // and register client data.
713
-
714
- // If we have the client data, we merge these items into the context.
715
- // register_context_data
716
- // because the page_context js object won't have been created yet...
717
-
718
- // create the different statements.
719
- // only put that resources_script in if there is something worth doing.
720
-
721
- // Or even do the CSS compilation and property removal from the JS file, all at the same time?
722
- // Maybe could do it from reference to the client controls too.
723
-
724
- // Will work out a fairly simple way to compile together tha CSS.
725
- // May have a few methods available, make use of them in different ways / at different levels.
726
-
727
- let statement_rsr;
728
- let statement_context_data;
729
- let statements = [];
730
-
731
- // Some data will be sent to the client each time.
732
- // Could possibly deliver some kind of token as well to represent the user.
733
-
734
- // Number of entries in this.app_server.def_resource_publishers
735
-
736
- if (app_admin.def_resource_publishers) {
737
- const c = Object.keys(app_admin.def_resource_publishers).length;
738
- if (c > 0) {
739
- statement_rsr = `jsgui.register_server_resources(${JSON.stringify(app_admin.def_resource_publishers)});`;
740
- statements.push(statement_rsr);
741
- }
742
- }
743
-
744
- /*
745
- if (context_data) {
746
- const c = Object.keys(context_data).length;
747
- if (c > 0) {
748
- statement_context_data = `jsgui.register_context_data(${JSON.stringify(context_data)});`;
749
- statements.push(statement_context_data);
750
- }
751
- }
752
- */
753
-
754
- if (statements.length > 0) {
755
- let resources_script = new jsgui.script({
756
- context: server_page_context
757
- });
758
- const strc = new jsgui.String_Control({
759
- context: server_page_context,
760
- text: statements.join('')
761
- });
762
- resources_script.add(strc);
763
- body.add(resources_script);
764
- }
765
-
766
- hd.all_html_render(function (err, deferred_html) {
767
- if (err) {
768
- throw err;
769
- } else {
770
- //console.log('deferred_html', deferred_html);
771
- var mime_type = 'text/html';
772
- //console.log('mime_type ' + mime_type);
773
- res.writeHead(200, {
774
- 'Content-Type': mime_type
775
- });
776
- res.end('<!DOCTYPE html>' + deferred_html, 'utf-8');
777
- }
778
- });
779
- });
780
- }
781
-
782
- prepare_app();
783
-
784
- server.start(port, (err, obj_start) => {
785
- if (err) {
786
- console.log('There was an error starting the server: \n', err);
787
- } else {
788
-
789
-
790
- console.log('Server started on port: ' + port);
791
- }
792
- });
793
-
794
- } else {
795
- throw 'stop';
796
- }
797
- }
798
- });
799
-
800
-
801
-
802
- //throw 'stop';
803
-
804
- // Need to add pages etc to the website resource?
805
-
806
-
807
-
808
- // But then need to have rendering of the content.
809
- // Then there will be a router within the admin app.
810
-
811
-
812
- const start_it = () => {
813
- server.start(port, (err, obj_start) => {
814
- if (err) {
815
- console.log('There was an error starting the server: \n', err);
816
- } else {
817
-
818
-
819
- console.log('Server started on port: ' + port);
820
-
821
-
822
-
823
- const do_more_post_start = () => {
824
- let wr = server.resource_pool.get_resource('Website Resource');
825
- //let js = server.resource_pool.get_resource('Website Resource').resource_pool.get_resource('Website JavaScript');
826
- console.log('server.resource_pool', server.resource_pool);
827
- console.log('wr', wr);
828
- throw 'stop';
829
-
830
- server.router.set_route('admin', (req, res) => {
831
- const pc = new Server_Page_Context({
832
- req: req,
833
- res: res
834
- });
835
-
836
-
837
-
838
- const hd = new Web_Admin_Page_Control({'context': pc});
839
- hd.include_client_css();
840
- hd.include_css('/css/basic.css');
841
- hd.include_css('/css/controls.css');
842
- hd.include_js('/js/app.js');
843
-
844
- // But will need to render the JS for it too.
845
-
846
- hd.all_html_render(function (err, deferred_html) {
847
- if (err) {
848
- throw err;
849
- } else {
850
- //console.log('deferred_html', deferred_html);
851
- var mime_type = 'text/html';
852
- //console.log('mime_type ' + mime_type);
853
- res.writeHead(200, {
854
- 'Content-Type': mime_type
855
- });
856
- res.end('<!DOCTYPE html>' + deferred_html, 'utf-8');
857
- }
858
- });
859
- });
860
- }
861
-
862
-
863
-
864
-
865
- }
866
- });
867
- }
868
-
869
- //start_it();
870
-
871
-
872
-
873
- // Route all requests to a single page...
874
- // An uncompiled web page?
875
- // A generated one?
876
- // jsgui HTML generation is a kind of compilation.
877
- // could see about abstracting it out as such a bit later on.
878
-
879
-
880
-
881
-
882
-
883
- //const web_page = new
884
-
885
-
886
- //server.route.all(web_page);
887
- // (routes all non-admin)
888
-
889
- // server.admin.route(true)
890
-
891
-
892
-
893
- }
894
-
895
-
896
-
897
-
898
-
899
-
900
-
901
- } else {
902
- //console.log('required as a module');
903
- }
228
+ const args = process.argv.slice(2);
229
+ let port = 80;
230
+ if (args.length === 1) {
231
+ const i = parseInt(args[0]);
232
+ if (typeof i === 'number') {
233
+ port = i;
234
+ }
235
+ }
236
+ const server = new JSGUI_Server({
237
+ name: 'jsgui3 server (command line)'
238
+ });
239
+ const current = () => {
240
+ server.start(8080);
241
+ }
242
+ current();
243
+ const old = () => {
244
+ const app_admin = new Website_Resource({
245
+ name: 'Admin Website'
246
+ });
247
+ server.resource_pool.add(app_admin);
248
+ let js = app_admin.resource_pool['Site JavaScript'];
249
+ let js_client = lib_path.resolve('./controls/page/admin.js');
250
+ let o_serve_package = {
251
+ 'babel': 'mini'
252
+ }
253
+ js.serve_package('admin/js/app.js', js_client, o_serve_package, (err, served) => {
254
+ if (err) {
255
+ console.log('err', err);
256
+ throw 'stop';
257
+ } else {
258
+ if (served) {
259
+ console.log('served', served);
260
+ const prepare_app = () => {
261
+ const server_router = server.router;
262
+ if (!server_router) {
263
+ throw 'no server_router';
264
+ }
265
+ var routing_tree = server_router.routing_tree;
266
+ routing_tree.set('admin', (req, res) => {
267
+ const o_spc = {
268
+ 'req': req,
269
+ 'res': res,
270
+ 'resource_pool': app_admin.resource_pool
271
+ }
272
+ if (this.include_server_ref_in_page_context) o_spc.server = this;
273
+ var server_page_context = new Server_Page_Context(o_spc);
274
+ if (this.context_data) {
275
+ Object.assign(server_page_context, this.context_data);
276
+ }
277
+ var hd = new jsgui.Client_HTML_Document({
278
+ 'context': server_page_context
279
+ });
280
+ hd.include_client_css();
281
+ hd.include_css('/admin/css/basic.css');
282
+ if (this.css) {
283
+ each(this.css, (path, serve_as) => {
284
+ hd.include_css('/admin/css/' + serve_as);
285
+ });
286
+ }
287
+ const body = hd.body;
288
+ let o_params = this.params || {};
289
+ Object.assign(o_params, {
290
+ 'context': server_page_context
291
+ });
292
+ const ctrl = this.ctrl = new Web_Admin_Panel_Control(o_params);
293
+ ctrl.active();
294
+ body.add(ctrl);
295
+ hd.include_js('/admin/js/app.js');
296
+ let statement_rsr;
297
+ let statement_context_data;
298
+ let statements = [];
299
+ if (app_admin.def_resource_publishers) {
300
+ const c = Object.keys(app_admin.def_resource_publishers).length;
301
+ if (c > 0) {
302
+ statement_rsr = `jsgui.register_server_resources(${JSON.stringify(app_admin.def_resource_publishers)});`;
303
+ statements.push(statement_rsr);
304
+ }
305
+ }
306
+ if (statements.length > 0) {
307
+ let resources_script = new jsgui.script({
308
+ context: server_page_context
309
+ });
310
+ const strc = new jsgui.String_Control({
311
+ context: server_page_context,
312
+ text: statements.join('')
313
+ });
314
+ resources_script.add(strc);
315
+ body.add(resources_script);
316
+ }
317
+ hd.all_html_render(function(err, deferred_html) {
318
+ if (err) {
319
+ throw err;
320
+ } else {
321
+ var mime_type = 'text/html';
322
+ res.writeHead(200, {
323
+ 'Content-Type': mime_type
324
+ });
325
+ res.end('<!DOCTYPE html>' + deferred_html, 'utf-8');
326
+ }
327
+ });
328
+ });
329
+ }
330
+ prepare_app();
331
+ server.start(port, (err, obj_start) => {
332
+ if (err) {
333
+ console.log('There was an error starting the server: \n', err);
334
+ } else {
335
+ console.log('Server started on port: ' + port);
336
+ }
337
+ });
338
+ } else {
339
+ throw 'stop';
340
+ }
341
+ }
342
+ });
343
+ const start_it = () => {
344
+ server.start(port, (err, obj_start) => {
345
+ if (err) {
346
+ console.log('There was an error starting the server: \n', err);
347
+ } else {
348
+ console.log('Server started on port: ' + port);
349
+ const do_more_post_start = () => {
350
+ let wr = server.resource_pool.get_resource('Website Resource');
351
+ console.log('server.resource_pool', server.resource_pool);
352
+ console.log('wr', wr);
353
+ throw 'stop';
354
+ server.router.set_route('admin', (req, res) => {
355
+ const pc = new Server_Page_Context({
356
+ req: req,
357
+ res: res
358
+ });
359
+ const hd = new Web_Admin_Page_Control({
360
+ 'context': pc
361
+ });
362
+ hd.include_client_css();
363
+ hd.include_css('/css/basic.css');
364
+ hd.include_css('/css/controls.css');
365
+ hd.include_js('/js/app.js');
366
+ hd.all_html_render(function(err, deferred_html) {
367
+ if (err) {
368
+ throw err;
369
+ } else {
370
+ var mime_type = 'text/html';
371
+ res.writeHead(200, {
372
+ 'Content-Type': mime_type
373
+ });
374
+ res.end('<!DOCTYPE html>' + deferred_html, 'utf-8');
375
+ }
376
+ });
377
+ });
378
+ }
379
+ }
380
+ });
381
+ }
382
+ }
383
+ } else {}