jsgui3-server 0.0.94 → 0.0.95

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
@@ -13,12 +13,16 @@ const jsgui = require('jsgui3-html'),
13
13
  each,
14
14
  tof
15
15
  } = jsgui;
16
+
17
+
16
18
  const lib_path = require('path');
17
19
  const Web_Admin_Page_Control = require('./controls/page/admin');
18
20
  const Web_Admin_Panel_Control = require('./controls/panel/admin');
19
21
  const Website = require('./website/website');
20
22
  const HTTP_Website_Publisher = require('./publishing/http-website-publisher');
21
23
  const Webpage = require('./website/webpage');
24
+ const HTTP_Webpage_Publisher = require('./publishing/http-webpage-publisher');
25
+
22
26
  class JSGUI_Server extends Evented_Class {
23
27
  constructor(spec = {
24
28
  website: true
@@ -28,42 +32,11 @@ class JSGUI_Server extends Evented_Class {
28
32
  if (spec.disk_path_client_js) {
29
33
  disk_path_client_js = spec.disk_path_client_js;
30
34
  };
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
- });
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
- });
35
+ Object.defineProperty(this, 'disk_path_client_js', {get: () => disk_path_client_js, set: (value) => disk_path_client_js = value})
36
+ let Ctrl = spec.Ctrl || undefined
37
+ Object.defineProperty(this, 'Ctrl', {get: () => Ctrl, set: value => Ctrl = value})
38
+ let name = spec.name || undefined;
39
+ Object.defineProperty(this, 'name', {get: () => name, set: value => name = value})
67
40
  this.__type_name = __type_name || 'server';
68
41
  const resource_pool = this.resource_pool = new Server_Resource_Pool({
69
42
  'access': {
@@ -75,9 +48,7 @@ class JSGUI_Server extends Evented_Class {
75
48
  'pool': resource_pool
76
49
  });
77
50
  resource_pool.add(server_router);
78
- if (spec.https_options) {
79
- this.https_options = spec.https_options;
80
- }
51
+ this.https_options = spec.https_options || undefined;
81
52
  if (spec.routes) {
82
53
  throw 'NYI - will use Website class rather than Website_Resource here'
83
54
  each(spec.routes, (app_spec, route) => {
@@ -86,47 +57,66 @@ class JSGUI_Server extends Evented_Class {
86
57
  server_router.set_route(route, app, app.process);
87
58
  });
88
59
  }
89
- if (true) {
90
- const old = () => {
91
- const app = this.app = new Website_Resource({
92
- name: 'Website'
60
+ const opts_website = {
61
+ 'name': this.name || 'Website'
62
+ };
63
+ const opts_webpage = {
64
+ 'name': this.name || 'Website'
65
+ };
66
+
67
+ if (Ctrl) {
68
+ // could be a web page, not a web site.
69
+ // But a site can contain one page. Easy enough default?
70
+ // Though more directly serving a page seems simpler. More logical too, if we are not serving a site with it.
71
+ //opts_website.content = Ctrl;
72
+ //opts_webpage.content = Ctrl;
73
+
74
+ // set up a web page with the ctrl, and a web page publisher.
75
+
76
+ const wp_app = new Webpage({content: Ctrl});
77
+ const opts_wp_publisher = {
78
+ 'webpage': wp_app
79
+ };
80
+ const wp_publisher = new HTTP_Webpage_Publisher(opts_wp_publisher);
81
+ console.log('waiting for wp_publisher ready');
82
+ wp_publisher.on('ready', () => {
83
+ console.log('wp publisher is ready');
84
+ const wp_resource = new Website_Resource({
85
+ 'name': 'Webpage Resource',
86
+ 'webpage': wp_app
93
87
  });
94
- resource_pool.add(app);
95
- server_router.set_route('/*', app.process);
88
+ resource_pool.add(wp_resource);
89
+ server_router.set_route('/', wp_publisher, wp_publisher.handle_http);
90
+ this.raise('ready');
91
+ });
92
+
93
+
94
+ } else {
95
+ const ws_app = this.app = new Website(opts_website);
96
+ // Be able to treat Webpage as an app?
97
+
98
+ const opts_ws_publisher = {
99
+ 'website': ws_app
100
+ };
101
+ if (disk_path_client_js) {
102
+ opts_ws_publisher.disk_path_client_js = disk_path_client_js;
96
103
  }
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 = {
104
+ const ws_publisher = new HTTP_Website_Publisher(opts_ws_publisher);
105
+ ws_publisher.on('ready', () => {
106
+ console.log('ws publisher is ready');
107
+ const ws_resource = new Website_Resource({
108
+ 'name': 'Website Resource',
106
109
  '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
110
  });
122
- }
123
- current();
111
+ resource_pool.add(ws_resource);
112
+ server_router.set_route('/*', ws_publisher, ws_publisher.handle_http);
113
+ this.raise('ready');
114
+ });
124
115
  }
125
- Object.defineProperty(this, 'router', {
126
- get() {
127
- return server_router;
128
- }
129
- });
116
+
117
+
118
+
119
+ Object.defineProperty(this, 'router', { get: () => server_router })
130
120
  }
131
121
  get resource_names() {
132
122
  return this.resource_pool.resource_names;
@@ -191,32 +181,6 @@ class JSGUI_Server extends Evented_Class {
191
181
  }
192
182
  });
193
183
  }
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
- }
220
184
  }
221
185
  JSGUI_Server.HTML = require('jsgui3-html');
222
186
  JSGUI_Server.Resource = Resource;
@@ -240,144 +204,63 @@ if (require.main === module) {
240
204
  server.start(8080);
241
205
  }
242
206
  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 {}
207
+
208
+ } else {}
209
+
210
+ //
211
+
212
+ const summary = {
213
+ "classes": [
214
+ "JSGUI_Server",
215
+ "Server_Resource_Pool",
216
+ "Router",
217
+ "Website_Resource",
218
+ "Server_Page_Context",
219
+ "Web_Admin_Page_Control",
220
+ "Web_Admin_Panel_Control",
221
+ "Website",
222
+ "HTTP_Website_Publisher",
223
+ "Webpage"
224
+ ],
225
+ "methods": {
226
+ "JSGUI_Server": [
227
+ "constructor",
228
+ "start",
229
+ "stop"
230
+ ],
231
+ "Server_Resource_Pool": [
232
+ "constructor"
233
+ ],
234
+ "Router": [
235
+ "constructor",
236
+ "set_route",
237
+ "unset_route"
238
+ ],
239
+ "Website_Resource": [
240
+ "constructor",
241
+ "process"
242
+ ],
243
+ "Server_Page_Context": [
244
+ "constructor",
245
+ "respond_string"
246
+ ],
247
+ "Web_Admin_Page_Control": [
248
+ "constructor"
249
+ ],
250
+ "Web_Admin_Panel_Control": [
251
+ "constructor"
252
+ ],
253
+ "Website": [
254
+ "constructor",
255
+ "add_page",
256
+ "add_page_resource",
257
+ "add_page_resource_from_webpage"
258
+ ],
259
+ "HTTP_Website_Publisher": [
260
+ "constructor"
261
+ ],
262
+ "Webpage": [
263
+ "constructor"
264
+ ]
265
+ }
266
+ }
@@ -18,7 +18,9 @@ const {Collection} = jsgui;
18
18
  // For the moment, an abstraction to represent a website.
19
19
 
20
20
  // A 'name property' mixin?
21
-
21
+ // Though could / should an HTML document control be used here?
22
+ // Probably not, because it's a specific type of web page. By far the most common one I think though.
23
+ // And the Webpage (helpfully) connects the path with the document.
22
24
  class Webpage {
23
25
  constructor(spec = {}) {
24
26
  // Variety of routes get served with variety of different formats and options.
@@ -26,144 +28,20 @@ class Webpage {
26
28
 
27
29
  // Could contain a bunch of resources.
28
30
  // Then there are the relevant resource publishers.
29
- let name;
30
- if (spec.name) {
31
- name = spec.name;
32
- };
33
- Object.defineProperty(this, 'name', {
34
- get() {
35
- return name;
36
- },
37
- set(value) {
38
- name = value;
39
- }
40
- });
41
-
42
- let title;
43
- if (spec.title) {
44
- title = spec.title;
45
- };
46
- Object.defineProperty(this, 'title', {
47
- get() {
48
- return title;
49
- },
50
- set(value) {
51
- title = value;
52
- }
53
- });
54
-
55
- // A URL or URL path property would make sense
56
-
57
- let path;
58
- if (spec.path) {
59
- path = spec.path;
60
- };
61
- Object.defineProperty(this, 'path', {
62
- get() {
63
- return path;
64
- },
65
- set(value) {
66
- path = value;
67
- }
68
- });
69
- Object.defineProperty(this, 'url_path', {
70
- get() {
71
- return path;
72
- },
73
- set(value) {
74
- path = value;
75
- }
76
- });
77
-
78
- // Then its content
79
- // Could be as HTML string.
80
- // Could be encoded somehow (eg template, rjx)
81
- // Could be a Control that represents a page
82
- // Could be a Control that goes within a page, and the system needs to serve it within the right page.
83
-
84
- let content;
85
-
86
- if (spec.content) {
87
- content = spec.content;
88
- };
89
-
90
- // Could be a Web_Document_Control or similar.
91
- // A Control that takes up the whole of a web page.
92
- Object.defineProperty(this, 'content', {
93
- get() {
94
- return content;
95
- },
96
- set(value) {
97
- content = value;
98
-
99
- // then depending on the type of content, it will prepare bundles in different ways.
100
-
101
- }
102
- });
103
-
104
- // .prepare_bundle function
105
- // though the webpage publisher may be better for this.
106
-
107
-
108
-
109
- // Requirements...
110
- // Would depend on the content.
111
-
112
- // Pages or routes...
113
-
114
- // A Collection of Pages.
115
- // May not be the best indexed way.
116
- // Should be fine for the moment.
117
-
118
- // Storing them all in a tree may be better.
119
- // Could make other types of Collection with the same / very similar Collection API.
31
+ let name = spec.name;
32
+ Object.defineProperty(this, 'name', { get() { return name; }, set(value) { name = value; } });
120
33
 
121
- // The website may have a routing tree, not a router?
122
- // Or both.
123
-
124
- // The website can have pages, sections, menus etc.
125
- // This could be a place (not Control) where the Controls are held together.
126
-
127
- // Web pages....
128
- // As well as applications / controls.
129
-
130
- // Web pages can have a path (to serve on). The path is something more of a server property, but when editing it
131
- // intuitively is a property of the page itself.
132
- // Web pages can load in other content
133
- // Some of it required to exist / be available at a specific address.
134
- /*
135
- website.pages.add(name, content);
136
-
137
- website.pages.front = ...
138
- */
34
+ let title = spec.title;
35
+ Object.defineProperty(this, 'title', { get() { return title; }, set(value) { title = value; } });
139
36
 
37
+ let path = spec.path;
38
+ Object.defineProperty(this, 'path', { get() { return path; }, set(value) { path = value; } });
39
+ Object.defineProperty(this, 'url_path', { get() { return path; }, set(value) { path = value; } });
140
40
 
41
+ let content = spec.content;
42
+ Object.defineProperty(this, 'content', { get() { return content; }, set(value) { content = value; } });
141
43
  }
142
44
 
143
-
144
- // A render function...?
145
- // Async / observable interface by default.
146
-
147
- // Rendering the different parts of it, including building the JS?
148
- // Or rendering just applies to the Controls.
149
- // Build or Compile would prepare the relevant dependencies.
150
-
151
- // render_html() returns Observable
152
-
153
- // compile() returns Observable which returns Buffer for each compiled file.
154
- // build() has got wider connotations than complile(), such as preparing images.
155
-
156
- // Could provide client js files specifically built for that page within that page's directory structure, using the same file name
157
- // but with .js extension. A single js file may be a good choice, such as for an app that gets embedded within a larger website.
158
-
159
-
160
-
161
-
162
- // /pagesubdir/pagename_client.js
163
-
164
-
165
-
166
-
167
45
  }
168
46
 
169
47
  module.exports = Webpage;