jsgui3-server 0.0.82 → 0.0.85
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/README.md +8 -1
- package/bundler/bundle.js +11 -0
- package/bundler/bundler.js +9 -0
- package/bundler/css-bundler.js +56 -0
- package/bundler/js-bundler.js +214 -0
- package/bundler/test_ast.js +74 -0
- package/bundler/webpage-bundler.js +286 -0
- package/bundler/website-bundler.js +23 -0
- package/controls/README.md +8 -0
- package/controls/page/admin.js +75 -0
- package/controls/panel/admin.js +11 -0
- package/examples/controls/html-server-combo-box.js +5 -5
- package/examples/html-server.js +3 -0
- package/examples/square_box.js +41 -0
- package/examples/square_box_client.js +91 -0
- package/module.js +6 -0
- package/{single-control-server.js → old/_single-control-server.js} +157 -96
- package/old/single-control-server.js +108 -158
- package/{single-page-app.js → old/single-page-app.js} +2 -0
- package/{examples/demos → old}/square_box.js +1 -1
- package/package.json +9 -9
- package/page-context.js +1 -1
- package/publishing/http-css-publisher.js +0 -0
- package/publishing/{function-publisher.js → http-function-publisher.js} +11 -1
- package/publishing/http-html-page-publisher.js +5 -0
- package/publishing/http-html-publisher.js +25 -0
- package/publishing/http-jpeg-publisher.js +0 -0
- package/publishing/http-js-publisher.js +25 -0
- package/publishing/{observable-publisher.js → http-observable-publisher.js} +12 -6
- package/publishing/http-png-publisher.js +0 -0
- package/publishing/http-publisher.js +52 -0
- package/publishing/{resource-publisher.js → http-resource-publisher.js} +20 -1
- package/publishing/http-svg-publisher.js +0 -0
- package/publishing/http-webpage-publisher.js +112 -0
- package/publishing/http-website-publisher.js +271 -0
- package/publishing/notes.md +1 -0
- package/resources/README.md +16 -0
- package/resources/_old_website-resource.js +507 -0
- package/resources/compile/server-resource-compilation.js +41 -0
- package/resources/data-resource.js +8 -0
- package/resources/fs-resource.js +2 -4
- package/resources/jsbuilder/babel/deep_iterate/deep_iterate_babel.js +3 -0
- package/resources/jsbuilder/test/test_ast_node.js +1 -1
- package/resources/jsbuilder/test/test_js_file.js +2 -2
- package/resources/notes.txt +11 -0
- package/resources/server-installed-tools.js +29 -0
- package/resources/server-resource-pool.js +1 -55
- package/resources/website-css-resource.js +1 -1
- package/resources/website-javascript-resource.js +165 -34
- package/resources/website-resource.js +52 -296
- package/resources/website-static-html-resource.js +0 -1
- package/resources/website-template-html-resource.js +231 -0
- package/roadmap.md +68 -0
- package/server.js +722 -17
- package/website/webpage.js +169 -0
- package/website/website-group.js +16 -0
- package/website/website.js +253 -0
package/server.js
CHANGED
|
@@ -11,22 +11,137 @@ Website_Resource = require('./resources/website-resource'),
|
|
|
11
11
|
Info = require('./resources/local-server-info-resource'),
|
|
12
12
|
Server_Page_Context = require('./page-context'), {Evented_Class, each, tof} = jsgui;
|
|
13
13
|
|
|
14
|
+
const lib_path = require('path');
|
|
15
|
+
|
|
16
|
+
const Web_Admin_Page_Control = require('./controls/page/admin');
|
|
17
|
+
const Web_Admin_Panel_Control = require('./controls/panel/admin');
|
|
18
|
+
|
|
19
|
+
const Website = require('./website/website');
|
|
20
|
+
const HTTP_Website_Publisher = require('./publishing/http-website-publisher');
|
|
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
|
+
|
|
14
35
|
// Operating JSGUI_Server as a proxy server or with improved proxy capabilities may help.
|
|
15
36
|
|
|
16
37
|
// The router is the same as on the client.
|
|
17
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
|
+
|
|
18
67
|
class JSGUI_Server extends Evented_Class {
|
|
19
68
|
constructor(spec = {website: true}, __type_name) {
|
|
20
69
|
// Default operations mode...
|
|
21
70
|
// Has its own website resource.
|
|
22
71
|
// All gets routed to that resource automatically.
|
|
23
72
|
//
|
|
73
|
+
super();
|
|
24
74
|
|
|
25
|
-
let is_website = spec.website === true;
|
|
26
75
|
|
|
27
|
-
|
|
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
|
+
|
|
28
139
|
this.__type_name = __type_name || 'server';
|
|
29
|
-
|
|
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({
|
|
30
145
|
// Other things can be made available through the server resource pool.
|
|
31
146
|
'access': {
|
|
32
147
|
'full': ['server_admin']
|
|
@@ -35,15 +150,29 @@ class JSGUI_Server extends Evented_Class {
|
|
|
35
150
|
//Object.defineProperty('')
|
|
36
151
|
// Maybe the server router should explicitly be a Resource?
|
|
37
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
|
+
|
|
38
158
|
const server_router = this.server_router = new Router({
|
|
39
159
|
'name': 'Server Router',
|
|
40
160
|
'pool': resource_pool
|
|
41
161
|
});
|
|
42
162
|
resource_pool.add(server_router);
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
43
166
|
if (spec.https_options) {
|
|
44
167
|
this.https_options = spec.https_options;
|
|
45
168
|
}
|
|
169
|
+
|
|
46
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
|
+
|
|
47
176
|
each(spec.routes, (app_spec, route) => {
|
|
48
177
|
var app = this.app = new Website_Resource(app_spec);
|
|
49
178
|
resource_pool.add(app);
|
|
@@ -52,16 +181,102 @@ class JSGUI_Server extends Evented_Class {
|
|
|
52
181
|
}
|
|
53
182
|
|
|
54
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.
|
|
55
188
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
+
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
const ws_resource = new Website_Resource({
|
|
256
|
+
'name': 'Website Resource',
|
|
257
|
+
'website': ws_app
|
|
258
|
+
});
|
|
259
|
+
resource_pool.add(ws_resource);
|
|
260
|
+
//server_router.set_route('/*', ws_app, ws_publisher.handle_http);
|
|
261
|
+
//server_router.set_route('/*', ws_publisher.handle_http);
|
|
262
|
+
server_router.set_route('/*', ws_publisher, ws_publisher.handle_http);
|
|
263
|
+
//console.log('route has been set');
|
|
264
|
+
// ws_publisher.start();
|
|
265
|
+
}
|
|
266
|
+
current();
|
|
267
|
+
|
|
268
|
+
|
|
63
269
|
|
|
64
270
|
}
|
|
271
|
+
|
|
272
|
+
// a 'router' property that returns the server router.
|
|
273
|
+
|
|
274
|
+
Object.defineProperty(this, 'router', {
|
|
275
|
+
get() {
|
|
276
|
+
return server_router;
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
// Don't want an admin web page by default (yet).
|
|
65
280
|
}
|
|
66
281
|
get resource_names() {
|
|
67
282
|
//console.log('this.resource_pool', this.resource_pool);
|
|
@@ -73,15 +288,44 @@ class JSGUI_Server extends Evented_Class {
|
|
|
73
288
|
|
|
74
289
|
|
|
75
290
|
'start'(port, callback, fnProcessRequest) {
|
|
291
|
+
|
|
292
|
+
if (tof(port) !== 'number') {
|
|
293
|
+
console.trace();
|
|
294
|
+
throw 'stop';
|
|
295
|
+
}
|
|
296
|
+
|
|
76
297
|
//throw 'stop';
|
|
77
298
|
// The resource_pool is not just a Data_Value. need to fix some get or create new field value code.
|
|
78
299
|
//console.log('start');
|
|
300
|
+
|
|
301
|
+
//jsgui.note('server', 'start');
|
|
302
|
+
|
|
303
|
+
// Maybe does not need a resource pool to begin with.
|
|
304
|
+
//
|
|
305
|
+
|
|
306
|
+
// Cool if this returned a promise or observable....
|
|
307
|
+
// obs would make most sense for what to return when starting a server.
|
|
308
|
+
|
|
79
309
|
const rp = this.resource_pool;
|
|
310
|
+
if (!rp) {
|
|
311
|
+
throw 'stop';
|
|
312
|
+
}
|
|
313
|
+
|
|
80
314
|
rp.start(err => {
|
|
81
315
|
if (err) {
|
|
82
316
|
throw err;
|
|
83
317
|
} else {
|
|
84
|
-
|
|
318
|
+
|
|
319
|
+
// Easy abstraction for starting on multiple ports?
|
|
320
|
+
// Seems like an important part, as it actually has the first HTTP request<>response function.
|
|
321
|
+
// Could send it to a more extended HTTP processing part of the server.
|
|
322
|
+
|
|
323
|
+
// Don't have http-server-publisher (yet).
|
|
324
|
+
|
|
325
|
+
const lsi = rp.get_resource('Local Server Info');
|
|
326
|
+
const server_router = rp.get_resource('Server Router');
|
|
327
|
+
|
|
328
|
+
/*
|
|
85
329
|
var resource_names = rp.resource_names;
|
|
86
330
|
var js = rp.get_resource('Site JavaScript');
|
|
87
331
|
var css = rp.get_resource('Site CSS');
|
|
@@ -90,8 +334,9 @@ class JSGUI_Server extends Evented_Class {
|
|
|
90
334
|
var login = rp.get_resource('Login HTML Resource');
|
|
91
335
|
var admin = rp.get_resource('Web Admin');
|
|
92
336
|
var sock_router = rp.get_resource('Server Sock Router');
|
|
93
|
-
|
|
94
|
-
|
|
337
|
+
*/
|
|
338
|
+
|
|
339
|
+
lsi.getters.net((err, net) => {
|
|
95
340
|
if (err) {
|
|
96
341
|
callback(err);
|
|
97
342
|
} else {
|
|
@@ -117,9 +362,10 @@ class JSGUI_Server extends Evented_Class {
|
|
|
117
362
|
https_server.timeout = 10800000;
|
|
118
363
|
//https_server.listen(443, ipv4_address);
|
|
119
364
|
https_server.listen(port, ipv4_address);
|
|
365
|
+
console.log('* Server running at https://' + ipv4_address + ':' + port + '/');
|
|
120
366
|
num_to_start--;
|
|
121
367
|
if (num_to_start === 0) {
|
|
122
|
-
callback(null, true);
|
|
368
|
+
if (callback) callback(null, true);
|
|
123
369
|
}
|
|
124
370
|
});
|
|
125
371
|
} else {
|
|
@@ -131,9 +377,9 @@ class JSGUI_Server extends Evented_Class {
|
|
|
131
377
|
http_server.listen(port, ipv4_address);
|
|
132
378
|
console.log('* Server running at http://' + ipv4_address + ':' + port + '/');
|
|
133
379
|
num_to_start--;
|
|
134
|
-
console.log('num_to_start', num_to_start);
|
|
380
|
+
//console.log('num_to_start', num_to_start);
|
|
135
381
|
if (num_to_start === 0) {
|
|
136
|
-
callback(null, true);
|
|
382
|
+
if (callback) callback(null, true);
|
|
137
383
|
}
|
|
138
384
|
});
|
|
139
385
|
}
|
|
@@ -144,7 +390,12 @@ class JSGUI_Server extends Evented_Class {
|
|
|
144
390
|
});
|
|
145
391
|
}
|
|
146
392
|
|
|
393
|
+
// Maybe will not have that function?
|
|
394
|
+
// Could pass it directly to the server router.
|
|
147
395
|
'process_request'(req, res) {
|
|
396
|
+
|
|
397
|
+
throw 'request should be processed elsewhere, such as the router'
|
|
398
|
+
|
|
148
399
|
var url = req.url;
|
|
149
400
|
//console.log('*** server process_request url ' + url);
|
|
150
401
|
var s_url = url.split('/');
|
|
@@ -161,7 +412,16 @@ class JSGUI_Server extends Evented_Class {
|
|
|
161
412
|
console.log('need to process short path');
|
|
162
413
|
}
|
|
163
414
|
}
|
|
415
|
+
|
|
416
|
+
// Maybe... but the server will serve a website, maybe webpage
|
|
417
|
+
// This is an HTTP request handler. Want to use various pieces of HTTP handling code, probably call it from here.
|
|
418
|
+
|
|
419
|
+
// Probably better to do a bundle process for that page / document, so that all referenced resources are known to be / made to be
|
|
420
|
+
// available.
|
|
421
|
+
|
|
422
|
+
|
|
164
423
|
'serve_document'(req, res, jsgui_html_document) {
|
|
424
|
+
throw 'deprecating.';
|
|
165
425
|
var html = jsgui_html_document.all_html_render();
|
|
166
426
|
var mime_type = 'text/html';
|
|
167
427
|
//console.log('mime_type ' + mime_type);
|
|
@@ -170,6 +430,13 @@ class JSGUI_Server extends Evented_Class {
|
|
|
170
430
|
});
|
|
171
431
|
res.end(html, 'utf-8');
|
|
172
432
|
}
|
|
433
|
+
|
|
434
|
+
// Properties and functions to better interact with what the server is hosting.
|
|
435
|
+
// ms_uptime property
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
173
440
|
}
|
|
174
441
|
|
|
175
442
|
//console.log('!!2)jsgui', !!jsgui);
|
|
@@ -179,4 +446,442 @@ JSGUI_Server.Page_Context = Server_Page_Context;
|
|
|
179
446
|
JSGUI_Server.Server_Page_Context = Server_Page_Context;
|
|
180
447
|
JSGUI_Server.Website_Resource = Website_Resource;
|
|
181
448
|
|
|
182
|
-
module.
|
|
449
|
+
// Maybe jsgui3-website should be its own module.
|
|
450
|
+
// Would (completely?) abstract away from the server.
|
|
451
|
+
|
|
452
|
+
// And maybe jsgui3-webpage
|
|
453
|
+
// Could be integrated within jsgui3-server.
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
module.exports = JSGUI_Server;
|
|
460
|
+
|
|
461
|
+
if (require.main === module) {
|
|
462
|
+
//console.log('called directly');
|
|
463
|
+
|
|
464
|
+
const args = process.argv.slice(2);
|
|
465
|
+
let port = 80;
|
|
466
|
+
|
|
467
|
+
if (args.length === 1) {
|
|
468
|
+
const i = parseInt(args[0]);
|
|
469
|
+
if (typeof i === 'number') {
|
|
470
|
+
port = i;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// Can give it a bit of content to start with....?
|
|
475
|
+
|
|
476
|
+
const server = new JSGUI_Server({
|
|
477
|
+
name: 'jsgui3 server (command line)'
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
const current = () => {
|
|
481
|
+
//console.log('server', server);
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
// And this Webpage itself is not a Control.
|
|
485
|
+
// It would need to specify it uses Control rendering (I think).
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
//const wp = new Webpage({
|
|
489
|
+
// 'name': 'Starter Webpage'
|
|
490
|
+
//});
|
|
491
|
+
|
|
492
|
+
// log various pieces of info / data from the server.
|
|
493
|
+
// Maybe its resources are the more accessible place to get data from.
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
// server.host(wp, '/');
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
// Better to start the server once the bundling is done...?
|
|
500
|
+
|
|
501
|
+
server.start(8080);
|
|
502
|
+
|
|
503
|
+
// May change to a core and extended server.
|
|
504
|
+
// Extended server could have functions (via mixin) such as serve_file that are above the routing and HTTP abstraction
|
|
505
|
+
// For the moment, keep the jsgui-server as core as possible. Maybe call it server-core?
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
}
|
|
511
|
+
current();
|
|
512
|
+
|
|
513
|
+
const old = () => {
|
|
514
|
+
|
|
515
|
+
// Server should already have a Website_Resource.
|
|
516
|
+
// Possibly just for its own admin.
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
/*
|
|
522
|
+
var app = new Website_Resource({
|
|
523
|
+
'name': 'html-server'
|
|
524
|
+
});
|
|
525
|
+
//this.publish = (...args) => app.publish(...args);
|
|
526
|
+
//console.log('app', app);
|
|
527
|
+
//throw 'stop';
|
|
528
|
+
this.resource_pool.add(app);
|
|
529
|
+
this.server_router.set_route('*', app, app.process);
|
|
530
|
+
this.app_server = app;
|
|
531
|
+
*/
|
|
532
|
+
|
|
533
|
+
//const web_admin = new Web_Admin_Page_Control({});
|
|
534
|
+
|
|
535
|
+
// Maybe server.route should be a function.
|
|
536
|
+
// but able to route by host values in incoming messages.
|
|
537
|
+
|
|
538
|
+
//server_router.set_route(route, app, app.process);
|
|
539
|
+
|
|
540
|
+
// Though needs to send it to something to render the control.
|
|
541
|
+
// a function of some sort.
|
|
542
|
+
// Could improve router / control functionality to respond to an HTTP request.
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
//server.router.set_route('admin/*', web_admin);
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
// A control publisher maybe?
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
//server.route('/admin/*', web_admin);
|
|
553
|
+
|
|
554
|
+
//
|
|
555
|
+
const app_admin = new Website_Resource({
|
|
556
|
+
name: 'Admin Website'
|
|
557
|
+
});
|
|
558
|
+
//console.log('app_admin', app_admin);
|
|
559
|
+
|
|
560
|
+
// Need to serve a JS client to that app.
|
|
561
|
+
// The /controls/page/admin.js app.
|
|
562
|
+
|
|
563
|
+
// app_admin.serve_js()
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
// Should be possible to programatically make / configure the app before serving it.
|
|
567
|
+
// Choose its controls.
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
server.resource_pool.add(app_admin);
|
|
571
|
+
|
|
572
|
+
// Need to better set up Control publishing
|
|
573
|
+
|
|
574
|
+
//server.router.set_route('/*', app, app.process);
|
|
575
|
+
//server.router.set_route('admin/*', app_admin, app_admin.process);
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
// The admin website resource won't have it's own resource pool.
|
|
579
|
+
|
|
580
|
+
// Will deal with serving JS and other content in a more intuitive / easy to code way.
|
|
581
|
+
|
|
582
|
+
let js = app_admin.resource_pool['Site JavaScript'];
|
|
583
|
+
|
|
584
|
+
//console.log('js', js);
|
|
585
|
+
|
|
586
|
+
//let js_client = this.client_package || this.js_client || 'jsgui3-client';
|
|
587
|
+
|
|
588
|
+
// Need to more easily / by default serve the suitable JS client app?
|
|
589
|
+
// Want it with one property setting or command. Not quite by default. Could be default in various configs.
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
// server.serve_js_client
|
|
593
|
+
// will a single js client cover all the served pages / site parts?
|
|
594
|
+
// an admin.js may be more appropriate, in some cases path specific JS.
|
|
595
|
+
|
|
596
|
+
// Further work on compiler / transformer type resources.
|
|
597
|
+
// Even 'publisher', as this part is about publishing the correct js in the correct format.
|
|
598
|
+
|
|
599
|
+
// Compilation and bundling seems most important for this type of server app.
|
|
600
|
+
|
|
601
|
+
// server.bundle_control
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
let js_client = lib_path.resolve('./controls/page/admin.js');
|
|
605
|
+
|
|
606
|
+
let o_serve_package = {
|
|
607
|
+
'babel': 'mini'
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// Would need to set the route in the router as well.
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
js.serve_package('admin/js/app.js', js_client, o_serve_package, (err, served) => {
|
|
615
|
+
if (err) {
|
|
616
|
+
console.log('err', err);
|
|
617
|
+
throw 'stop';
|
|
618
|
+
} else {
|
|
619
|
+
if (served) {
|
|
620
|
+
console.log('served', served);
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
const prepare_app = () => {
|
|
625
|
+
|
|
626
|
+
const server_router = server.router;
|
|
627
|
+
|
|
628
|
+
if (!server_router) {
|
|
629
|
+
throw 'no server_router';
|
|
630
|
+
}
|
|
631
|
+
var routing_tree = server_router.routing_tree;
|
|
632
|
+
|
|
633
|
+
// route /js to the js resource?
|
|
634
|
+
|
|
635
|
+
routing_tree.set('admin', (req, res) => {
|
|
636
|
+
//console.log('root path / request');
|
|
637
|
+
const o_spc = {
|
|
638
|
+
'req': req,
|
|
639
|
+
'res': res,
|
|
640
|
+
'resource_pool': app_admin.resource_pool
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
if (this.include_server_ref_in_page_context) o_spc.server = this;
|
|
644
|
+
var server_page_context = new Server_Page_Context(o_spc);
|
|
645
|
+
// then merge the context data :)
|
|
646
|
+
if (this.context_data) {
|
|
647
|
+
Object.assign(server_page_context, this.context_data);
|
|
648
|
+
}
|
|
649
|
+
// and .server property?
|
|
650
|
+
// a different way to get the server info to the components is needed.
|
|
651
|
+
|
|
652
|
+
// Page_Bounds_Specifier
|
|
653
|
+
var hd = new jsgui.Client_HTML_Document({
|
|
654
|
+
'context': server_page_context
|
|
655
|
+
});
|
|
656
|
+
hd.include_client_css();
|
|
657
|
+
hd.include_css('/admin/css/basic.css');
|
|
658
|
+
//hd.include_css('/css/controls.css');
|
|
659
|
+
|
|
660
|
+
// include compiled css too.
|
|
661
|
+
// not much of it yet.
|
|
662
|
+
|
|
663
|
+
// Will be a separate CSS file, generated upon app start.
|
|
664
|
+
if (this.css) {
|
|
665
|
+
each(this.css, (path, serve_as) => {
|
|
666
|
+
//css.serve(serve_as, path);
|
|
667
|
+
hd.include_css('/admin/css/' + serve_as);
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
const body = hd.body;
|
|
672
|
+
let o_params = this.params || {};
|
|
673
|
+
Object.assign(o_params, {
|
|
674
|
+
'context': server_page_context
|
|
675
|
+
});
|
|
676
|
+
//console.log('o_params', o_params);
|
|
677
|
+
//console.log('this.Ctrl', this.Ctrl);
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
const ctrl = this.ctrl = new Web_Admin_Panel_Control(o_params);
|
|
681
|
+
ctrl.active();
|
|
682
|
+
//var ctrl2 = new jsgui.Control({});
|
|
683
|
+
body.add(ctrl);
|
|
684
|
+
|
|
685
|
+
// it will be a client-side function.
|
|
686
|
+
|
|
687
|
+
// Should not use 'add' here.
|
|
688
|
+
// it's the script content.
|
|
689
|
+
|
|
690
|
+
// want to get around that escaping.
|
|
691
|
+
// options escaping / escape : false
|
|
692
|
+
|
|
693
|
+
hd.include_js('/admin/js/app.js');
|
|
694
|
+
|
|
695
|
+
// Would this be a place to register icons?
|
|
696
|
+
// and register client data.
|
|
697
|
+
|
|
698
|
+
// If we have the client data, we merge these items into the context.
|
|
699
|
+
// register_context_data
|
|
700
|
+
// because the page_context js object won't have been created yet...
|
|
701
|
+
|
|
702
|
+
// create the different statements.
|
|
703
|
+
// only put that resources_script in if there is something worth doing.
|
|
704
|
+
|
|
705
|
+
// Or even do the CSS compilation and property removal from the JS file, all at the same time?
|
|
706
|
+
// Maybe could do it from reference to the client controls too.
|
|
707
|
+
|
|
708
|
+
// Will work out a fairly simple way to compile together tha CSS.
|
|
709
|
+
// May have a few methods available, make use of them in different ways / at different levels.
|
|
710
|
+
|
|
711
|
+
let statement_rsr;
|
|
712
|
+
let statement_context_data;
|
|
713
|
+
let statements = [];
|
|
714
|
+
|
|
715
|
+
// Some data will be sent to the client each time.
|
|
716
|
+
// Could possibly deliver some kind of token as well to represent the user.
|
|
717
|
+
|
|
718
|
+
// Number of entries in this.app_server.def_resource_publishers
|
|
719
|
+
|
|
720
|
+
if (app_admin.def_resource_publishers) {
|
|
721
|
+
const c = Object.keys(app_admin.def_resource_publishers).length;
|
|
722
|
+
if (c > 0) {
|
|
723
|
+
statement_rsr = `jsgui.register_server_resources(${JSON.stringify(app_admin.def_resource_publishers)});`;
|
|
724
|
+
statements.push(statement_rsr);
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/*
|
|
729
|
+
if (context_data) {
|
|
730
|
+
const c = Object.keys(context_data).length;
|
|
731
|
+
if (c > 0) {
|
|
732
|
+
statement_context_data = `jsgui.register_context_data(${JSON.stringify(context_data)});`;
|
|
733
|
+
statements.push(statement_context_data);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
*/
|
|
737
|
+
|
|
738
|
+
if (statements.length > 0) {
|
|
739
|
+
let resources_script = new jsgui.script({
|
|
740
|
+
context: server_page_context
|
|
741
|
+
});
|
|
742
|
+
const strc = new jsgui.String_Control({
|
|
743
|
+
context: server_page_context,
|
|
744
|
+
text: statements.join('')
|
|
745
|
+
});
|
|
746
|
+
resources_script.add(strc);
|
|
747
|
+
body.add(resources_script);
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
hd.all_html_render(function (err, deferred_html) {
|
|
751
|
+
if (err) {
|
|
752
|
+
throw err;
|
|
753
|
+
} else {
|
|
754
|
+
//console.log('deferred_html', deferred_html);
|
|
755
|
+
var mime_type = 'text/html';
|
|
756
|
+
//console.log('mime_type ' + mime_type);
|
|
757
|
+
res.writeHead(200, {
|
|
758
|
+
'Content-Type': mime_type
|
|
759
|
+
});
|
|
760
|
+
res.end('<!DOCTYPE html>' + deferred_html, 'utf-8');
|
|
761
|
+
}
|
|
762
|
+
});
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
prepare_app();
|
|
767
|
+
|
|
768
|
+
server.start(port, (err, obj_start) => {
|
|
769
|
+
if (err) {
|
|
770
|
+
console.log('There was an error starting the server: \n', err);
|
|
771
|
+
} else {
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
console.log('Server started on port: ' + port);
|
|
775
|
+
}
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
} else {
|
|
779
|
+
throw 'stop';
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
});
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
|
|
786
|
+
//throw 'stop';
|
|
787
|
+
|
|
788
|
+
// Need to add pages etc to the website resource?
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
|
|
792
|
+
// But then need to have rendering of the content.
|
|
793
|
+
// Then there will be a router within the admin app.
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
const start_it = () => {
|
|
797
|
+
server.start(port, (err, obj_start) => {
|
|
798
|
+
if (err) {
|
|
799
|
+
console.log('There was an error starting the server: \n', err);
|
|
800
|
+
} else {
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
console.log('Server started on port: ' + port);
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
|
|
807
|
+
const do_more_post_start = () => {
|
|
808
|
+
let wr = server.resource_pool.get_resource('Website Resource');
|
|
809
|
+
//let js = server.resource_pool.get_resource('Website Resource').resource_pool.get_resource('Website JavaScript');
|
|
810
|
+
console.log('server.resource_pool', server.resource_pool);
|
|
811
|
+
console.log('wr', wr);
|
|
812
|
+
throw 'stop';
|
|
813
|
+
|
|
814
|
+
server.router.set_route('admin', (req, res) => {
|
|
815
|
+
const pc = new Server_Page_Context({
|
|
816
|
+
req: req,
|
|
817
|
+
res: res
|
|
818
|
+
});
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
|
|
822
|
+
const hd = new Web_Admin_Page_Control({'context': pc});
|
|
823
|
+
hd.include_client_css();
|
|
824
|
+
hd.include_css('/css/basic.css');
|
|
825
|
+
hd.include_css('/css/controls.css');
|
|
826
|
+
hd.include_js('/js/app.js');
|
|
827
|
+
|
|
828
|
+
// But will need to render the JS for it too.
|
|
829
|
+
|
|
830
|
+
hd.all_html_render(function (err, deferred_html) {
|
|
831
|
+
if (err) {
|
|
832
|
+
throw err;
|
|
833
|
+
} else {
|
|
834
|
+
//console.log('deferred_html', deferred_html);
|
|
835
|
+
var mime_type = 'text/html';
|
|
836
|
+
//console.log('mime_type ' + mime_type);
|
|
837
|
+
res.writeHead(200, {
|
|
838
|
+
'Content-Type': mime_type
|
|
839
|
+
});
|
|
840
|
+
res.end('<!DOCTYPE html>' + deferred_html, 'utf-8');
|
|
841
|
+
}
|
|
842
|
+
});
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
|
|
849
|
+
}
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
//start_it();
|
|
854
|
+
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
// Route all requests to a single page...
|
|
858
|
+
// An uncompiled web page?
|
|
859
|
+
// A generated one?
|
|
860
|
+
// jsgui HTML generation is a kind of compilation.
|
|
861
|
+
// could see about abstracting it out as such a bit later on.
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
//const web_page = new
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
//server.route.all(web_page);
|
|
871
|
+
// (routes all non-admin)
|
|
872
|
+
|
|
873
|
+
// server.admin.route(true)
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
} else {
|
|
886
|
+
//console.log('required as a module');
|
|
887
|
+
}
|