miolo 0.2.3 → 0.3.0
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 +34 -1
- package/dist/cli/miolo.cli.iife.bundle.js +374 -0
- package/dist/cli/miolo.cli.iife.bundle.js.map +1 -0
- package/dist/cli/miolo.cli.iife.bundle.min.js +22 -0
- package/dist/cli/miolo.cli.iife.js +76 -0
- package/dist/cli/miolo.cli.iife.js.map +1 -0
- package/dist/cli/miolo.cli.iife.min.js +11 -0
- package/dist/cli/miolo.cli.min.mjs +11 -0
- package/dist/cli/miolo.cli.mjs +69 -0
- package/dist/cli/miolo.cli.mjs.map +1 -0
- package/dist/cli/miolo.cli.umd.bundle.js +375 -0
- package/dist/cli/miolo.cli.umd.bundle.js.map +1 -0
- package/dist/cli/miolo.cli.umd.bundle.min.js +22 -0
- package/dist/cli/miolo.cli.umd.js +77 -0
- package/dist/cli/miolo.cli.umd.js.map +1 -0
- package/dist/cli/miolo.cli.umd.min.js +11 -0
- package/dist/cli-react/miolo.cli-react.iife.bundle.js +1690 -0
- package/dist/cli-react/miolo.cli-react.iife.bundle.js.map +1 -0
- package/dist/cli-react/miolo.cli-react.iife.bundle.min.js +62 -0
- package/dist/cli-react/miolo.cli-react.iife.js +531 -0
- package/dist/cli-react/miolo.cli-react.iife.js.map +1 -0
- package/dist/cli-react/miolo.cli-react.iife.min.js +11 -0
- package/dist/cli-react/miolo.cli-react.min.mjs +11 -0
- package/dist/cli-react/miolo.cli-react.mjs +187 -0
- package/dist/cli-react/miolo.cli-react.mjs.map +1 -0
- package/dist/cli-react/miolo.cli-react.umd.bundle.js +1691 -0
- package/dist/cli-react/miolo.cli-react.umd.bundle.js.map +1 -0
- package/dist/cli-react/miolo.cli-react.umd.bundle.min.js +62 -0
- package/dist/cli-react/miolo.cli-react.umd.js +532 -0
- package/dist/cli-react/miolo.cli-react.umd.js.map +1 -0
- package/dist/cli-react/miolo.cli-react.umd.min.js +11 -0
- package/dist/server/miolo.ico +0 -0
- package/dist/{miolo.cjs → server/miolo.server.cjs} +320 -208
- package/dist/server/miolo.server.min.mjs +11 -0
- package/dist/{miolo.mjs → server/miolo.server.mjs} +292 -159
- package/dist/server/miolo.server.mjs.map +1 -0
- package/dist/{miolo.node.mjs → server/miolo.server.node.mjs} +321 -208
- package/dist/static/img/miolo.ico +0 -0
- package/dist/static/robots.txt +2 -0
- package/logo/favicon/miolo.ico +0 -0
- package/logo/favicon/miolo.png +0 -0
- package/logo/favicon/miolo_apple.png +0 -0
- package/logo/miolo-logo-name_200.png +0 -0
- package/package.json +63 -20
- package/dist/miolo.min.mjs +0 -11
- package/dist/miolo.mjs.map +0 -1
- package/lib/cacher/index.cjs +0 -76
- package/lib/cacher/verify.cjs +0 -27
- package/lib/config/defaults.cjs +0 -287
- package/lib/config/index.cjs +0 -12
- package/lib/emailer/index.cjs +0 -56
- package/lib/emailer/verify.cjs +0 -11
- package/lib/index.cjs +0 -40
- package/lib/logger/index.cjs +0 -91
- package/lib/logger/logger_mail.cjs +0 -56
- package/lib/logger/verify.cjs +0 -22
- package/lib/server/engines/cron/index.cjs +0 -12
- package/lib/server/engines/cron/syscheck.cjs +0 -41
- package/lib/server/engines/socket/index.cjs +0 -40
- package/lib/server/index.cjs +0 -134
- package/lib/server/middleware/auth/basic.cjs +0 -53
- package/lib/server/middleware/auth/guest.cjs +0 -72
- package/lib/server/middleware/auth/passport.cjs +0 -123
- package/lib/server/middleware/body.cjs +0 -50
- package/lib/server/middleware/catcher/index.cjs +0 -58
- package/lib/server/middleware/context.cjs +0 -23
- package/lib/server/middleware/extra.cjs +0 -27
- package/lib/server/middleware/headers.cjs +0 -68
- package/lib/server/middleware/request.cjs +0 -68
- package/lib/server/middleware/session/index.cjs +0 -26
- package/lib/server/middleware/session/store.cjs +0 -10
- package/lib/server/middleware/static.cjs +0 -25
- package/lib/server/routes/catch_js_error.cjs +0 -35
- package/lib/server/routes/html_render.cjs +0 -37
- package/lib/server/routes/robots.cjs +0 -27
- /package/{lib/server/routes → dist/server}/fallback_index.html +0 -0
- /package/{lib/server/static → dist/server}/robots.txt +0 -0
- /package/{lib/server/static/img/favicon.ico → dist/static/img/afialapis.ico} +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* miolo v0.
|
|
2
|
+
* miolo v0.3.0
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) Donato Lorenzo <donato@afialapis.com>
|
|
5
5
|
*
|
|
@@ -9,33 +9,36 @@
|
|
|
9
9
|
* @license MIT
|
|
10
10
|
*/
|
|
11
11
|
import Koa from 'koa';
|
|
12
|
+
import { initCalustraRouter } from 'calustra/router';
|
|
12
13
|
import merge from 'assign-deep';
|
|
13
|
-
import path
|
|
14
|
+
import path from 'node:path';
|
|
14
15
|
import { fileURLToPath } from 'node:url';
|
|
15
|
-
import nodemailer from 'nodemailer';
|
|
16
|
-
import { uncolor, gray, magenta, cyan, yellow, red_light, red, green, blue } from 'tinguir';
|
|
17
|
-
import util, { promisify } from 'node:util';
|
|
18
|
-
import winston, { transports, createLogger, format } from 'winston';
|
|
19
16
|
import os from 'node:os';
|
|
20
17
|
import diskspace from 'diskspace';
|
|
18
|
+
import { cyan, yellow, green, uncolor, gray, magenta, red_light, red, blue } from 'tinguir';
|
|
21
19
|
import { CronJob } from 'cron';
|
|
22
|
-
import {
|
|
23
|
-
export { getConnection } from 'calustra
|
|
20
|
+
import { getConnection } from 'calustra';
|
|
21
|
+
export { getConnection } from 'calustra';
|
|
22
|
+
import nodemailer from 'nodemailer';
|
|
23
|
+
import util from 'node:util';
|
|
24
|
+
import winston, { transports, createLogger, format } from 'winston';
|
|
24
25
|
import koa_cors from '@koa/cors';
|
|
25
26
|
import koa_proxy from 'koa-proxies';
|
|
26
27
|
import koa_body_parser from 'koa-better-body';
|
|
27
28
|
import koa_convert from 'koa-convert';
|
|
28
29
|
import koa_compress from 'koa-compress';
|
|
29
|
-
import {
|
|
30
|
+
import { constants } from 'node:zlib';
|
|
30
31
|
import koa_mount from 'koa-mount';
|
|
31
32
|
import koa_serve from 'koa-static';
|
|
32
33
|
import koa_favicon from 'koa-favicon';
|
|
33
34
|
import { performance } from 'node:perf_hooks';
|
|
34
35
|
import koa_session from 'koa-session';
|
|
35
36
|
import RedisStore from 'koa-redis';
|
|
36
|
-
import fs from 'node:fs';
|
|
37
37
|
import Router from '@koa/router';
|
|
38
|
-
import
|
|
38
|
+
import { readFileSync } from 'node:fs';
|
|
39
|
+
import React, { useState, useEffect } from 'react';
|
|
40
|
+
import { StaticRouter } from 'react-router-dom/server';
|
|
41
|
+
import { renderToString } from 'react-dom/server';
|
|
39
42
|
import jwt from 'jwt-simple';
|
|
40
43
|
import auth from 'basic-auth';
|
|
41
44
|
import passport$1 from 'koa-passport';
|
|
@@ -121,9 +124,9 @@ function _toPropertyKey(arg) {
|
|
|
121
124
|
return typeof key === "symbol" ? key : String(key);
|
|
122
125
|
}
|
|
123
126
|
|
|
124
|
-
var __my_filename = fileURLToPath(require('url').pathToFileURL(__filename).toString());
|
|
125
|
-
var __my_dirname = path.dirname(__my_filename);
|
|
126
|
-
var favicon = path.resolve(__my_dirname, '../
|
|
127
|
+
var __my_filename$3 = fileURLToPath(require('url').pathToFileURL(__filename).toString());
|
|
128
|
+
var __my_dirname$3 = path.dirname(__my_filename$3);
|
|
129
|
+
var favicon = path.resolve(__my_dirname$3, '../static/img/miolo.ico');
|
|
127
130
|
var base_config = {
|
|
128
131
|
name: 'miolo',
|
|
129
132
|
http: {
|
|
@@ -180,27 +183,25 @@ var base_config = {
|
|
|
180
183
|
},
|
|
181
184
|
|
|
182
185
|
db: {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
idleTimeoutMillis: 10000 // The maximum time, in milliseconds, that a connection can be idle before being released. Use with combination of evict for proper working, for more details read https://github.com/coopernurse/node-pool/issues/178#issuecomment-327110870,
|
|
196
|
-
},
|
|
197
|
-
|
|
198
|
-
options: {
|
|
199
|
-
log: 'silly' // will be updated on the fly with miolo logger
|
|
200
|
-
}
|
|
186
|
+
config: {
|
|
187
|
+
dialect: 'postgres',
|
|
188
|
+
host: 'localhost',
|
|
189
|
+
port: 5432,
|
|
190
|
+
database: 'miolo',
|
|
191
|
+
user: 'postgres',
|
|
192
|
+
password: 'postgres',
|
|
193
|
+
max: 5,
|
|
194
|
+
// Maximum number of connection in pool
|
|
195
|
+
min: 0,
|
|
196
|
+
// Minimum number of connection in pool
|
|
197
|
+
idleTimeoutMillis: 10000 // The maximum time, in milliseconds, that a connection can be idle before being released. Use with combination of evict for proper working, for more details read https://github.com/coopernurse/node-pool/issues/178#issuecomment-327110870,
|
|
201
198
|
},
|
|
202
199
|
|
|
203
|
-
|
|
200
|
+
options: {
|
|
201
|
+
log: 'silly',
|
|
202
|
+
// will be updated on the fly with miolo logger
|
|
203
|
+
tables: []
|
|
204
|
+
}
|
|
204
205
|
},
|
|
205
206
|
routes: {
|
|
206
207
|
bodyField: undefined,
|
|
@@ -246,12 +247,12 @@ var base_config = {
|
|
|
246
247
|
queries: undefined
|
|
247
248
|
},
|
|
248
249
|
catcher: '/sys/jserror',
|
|
249
|
-
cacher: {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
},
|
|
250
|
+
//cacher: {
|
|
251
|
+
// redis: {
|
|
252
|
+
// host: '127.0.0.1',
|
|
253
|
+
// port: 6379
|
|
254
|
+
// },
|
|
255
|
+
//},
|
|
255
256
|
log: {
|
|
256
257
|
level: 'debug',
|
|
257
258
|
format: {
|
|
@@ -403,6 +404,43 @@ function init_config(config) {
|
|
|
403
404
|
return merge(base_config, config);
|
|
404
405
|
}
|
|
405
406
|
|
|
407
|
+
function init_sys_check_and_log(logger) {
|
|
408
|
+
function toMB(bytes) {
|
|
409
|
+
if (!bytes) return 0;
|
|
410
|
+
return bytes / 1000000;
|
|
411
|
+
}
|
|
412
|
+
function toGB(kbytes) {
|
|
413
|
+
if (!kbytes) return 0;
|
|
414
|
+
return kbytes / 1000000;
|
|
415
|
+
}
|
|
416
|
+
function sys_check_and_log() {
|
|
417
|
+
var used = Math.round(toMB(os.freemem()), 2);
|
|
418
|
+
var total = Math.round(toMB(os.totalmem()), 2);
|
|
419
|
+
var perc = Math.round(used * 100 / total, 2);
|
|
420
|
+
if (perc > 80) {
|
|
421
|
+
logger.error("[SERVER][".concat(cyan('SysCheck'), "] RAM ").concat(yellow(used), " MB used of ").concat(green(total), " MB (").concat(yellow(perc), " %)"));
|
|
422
|
+
} else {
|
|
423
|
+
logger.info("[SERVER][".concat(cyan('SysCheck'), "] RAM ").concat(yellow(used), " MB used of ").concat(green(total), " MB (").concat(yellow(perc), " %)"));
|
|
424
|
+
}
|
|
425
|
+
diskspace.check('/', function (err, result) {
|
|
426
|
+
var used = Math.round(toGB(result.used), 2);
|
|
427
|
+
var total = Math.round(toGB(result.total), 2);
|
|
428
|
+
var free = Math.round(toGB(result.free), 2);
|
|
429
|
+
if (free < 1) {
|
|
430
|
+
logger.error("[SERVER][".concat(cyan('SysCheck'), "] DISK ").concat(yellow(used), " GB used of ").concat(green(total), " GB (").concat(yellow(free), " GB free)"));
|
|
431
|
+
} else {
|
|
432
|
+
logger.info("[SERVER][".concat(cyan('SysCheck'), "] DISK ").concat(yellow(used), " GB used of ").concat(green(total), " GB (").concat(yellow(free), " GB free)"));
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
return sys_check_and_log;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function init_cron(logger) {
|
|
440
|
+
var sys_check_and_log = init_sys_check_and_log(logger);
|
|
441
|
+
new CronJob('30 * * * *', sys_check_and_log, null, true, 'Europe/Madrid');
|
|
442
|
+
}
|
|
443
|
+
|
|
406
444
|
function init_emailer(options, defaults) {
|
|
407
445
|
var silent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
408
446
|
var nmailer = nodemailer.createTransport(options, defaults);
|
|
@@ -577,42 +615,43 @@ var init_logger = (config, emailer) => {
|
|
|
577
615
|
return logger;
|
|
578
616
|
};
|
|
579
617
|
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
618
|
+
var init_context_middleware = (app, config) => {
|
|
619
|
+
var emailer = init_emailer(config.mail.options, config.mail.defaults);
|
|
620
|
+
var logger = init_logger(config.log, emailer);
|
|
621
|
+
config.db.options.log = logger;
|
|
622
|
+
var getConnectionWrap = () => {
|
|
623
|
+
return getConnection(config.db.config, config.db.options);
|
|
624
|
+
};
|
|
625
|
+
var getModelWrap = name => {
|
|
626
|
+
var conn = getConnection(config.db.config, config.db.options);
|
|
627
|
+
return conn.getModel(name);
|
|
628
|
+
};
|
|
629
|
+
var db = {
|
|
630
|
+
getConnection: getConnectionWrap,
|
|
631
|
+
getModel: getModelWrap
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
// Assign miolo stuff to ctx
|
|
635
|
+
var mioloContext = {
|
|
636
|
+
config: _objectSpread2({}, config),
|
|
637
|
+
emailer,
|
|
638
|
+
logger,
|
|
639
|
+
db: db
|
|
640
|
+
};
|
|
641
|
+
function context_middleware(_x, _x2) {
|
|
642
|
+
return _context_middleware.apply(this, arguments);
|
|
588
643
|
}
|
|
589
|
-
function
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
logger.error("[SERVER][".concat(cyan('SysCheck'), "] RAM ").concat(yellow(used), " MB used of ").concat(green(total), " MB (").concat(yellow(perc), " %)"));
|
|
595
|
-
} else {
|
|
596
|
-
logger.info("[SERVER][".concat(cyan('SysCheck'), "] RAM ").concat(yellow(used), " MB used of ").concat(green(total), " MB (").concat(yellow(perc), " %)"));
|
|
597
|
-
}
|
|
598
|
-
diskspace.check('/', function (err, result) {
|
|
599
|
-
var used = Math.round(toGB(result.used), 2);
|
|
600
|
-
var total = Math.round(toGB(result.total), 2);
|
|
601
|
-
var free = Math.round(toGB(result.free), 2);
|
|
602
|
-
if (free < 1) {
|
|
603
|
-
logger.error("[SERVER][".concat(cyan('SysCheck'), "] DISK ").concat(yellow(used), " GB used of ").concat(green(total), " GB (").concat(yellow(free), " GB free)"));
|
|
604
|
-
} else {
|
|
605
|
-
logger.info("[SERVER][".concat(cyan('SysCheck'), "] DISK ").concat(yellow(used), " GB used of ").concat(green(total), " GB (").concat(yellow(free), " GB free)"));
|
|
606
|
-
}
|
|
644
|
+
function _context_middleware() {
|
|
645
|
+
_context_middleware = _asyncToGenerator(function* (ctx, next) {
|
|
646
|
+
// Assign miolo stuff to ctx
|
|
647
|
+
ctx.miolo = mioloContext;
|
|
648
|
+
yield next();
|
|
607
649
|
});
|
|
650
|
+
return _context_middleware.apply(this, arguments);
|
|
608
651
|
}
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
function init_cron(logger) {
|
|
613
|
-
var sys_check_and_log = init_sys_check_and_log(logger);
|
|
614
|
-
new CronJob('30 * * * *', sys_check_and_log, null, true, 'Europe/Madrid');
|
|
615
|
-
}
|
|
652
|
+
app.use(context_middleware);
|
|
653
|
+
app.context.miolo = mioloContext;
|
|
654
|
+
};
|
|
616
655
|
|
|
617
656
|
var _if_options = (http, field, callback) => {
|
|
618
657
|
try {
|
|
@@ -644,7 +683,8 @@ var _proxy_options = options => {
|
|
|
644
683
|
};
|
|
645
684
|
return [tpath, toptions];
|
|
646
685
|
};
|
|
647
|
-
var init_headers_middleware = (app, http
|
|
686
|
+
var init_headers_middleware = (app, http) => {
|
|
687
|
+
var logger = app.context.miolo.logger;
|
|
648
688
|
_if_options(http, 'cors', options => {
|
|
649
689
|
if (options == 'simple') {
|
|
650
690
|
logger.debug("Setting CORS the simple way");
|
|
@@ -679,7 +719,13 @@ var init_body_middleware = app => {
|
|
|
679
719
|
return content_type == 'application/json' || content_type == 'text/html';
|
|
680
720
|
},
|
|
681
721
|
//threshold: 2048,
|
|
682
|
-
|
|
722
|
+
gzip: {
|
|
723
|
+
flush: constants.Z_SYNC_FLUSH
|
|
724
|
+
},
|
|
725
|
+
deflate: {
|
|
726
|
+
flush: constants.Z_SYNC_FLUSH
|
|
727
|
+
},
|
|
728
|
+
br: false // disable brotli
|
|
683
729
|
}));
|
|
684
730
|
|
|
685
731
|
// body parser - koa-better-body
|
|
@@ -693,7 +739,7 @@ var init_body_middleware = app => {
|
|
|
693
739
|
// https://github.com/rkusa/koa-passport/issues/33#issuecomment-122611613
|
|
694
740
|
// https://github.com/tunnckoCore/opensource/tree/master/%40packages/koa-better-body#options
|
|
695
741
|
// {fields: 'body', files: 'body',}
|
|
696
|
-
// But this will cause errors on other packagers (for example, calustra
|
|
742
|
+
// But this will cause errors on other packagers (for example, calustra/router)
|
|
697
743
|
// 2.- but still need to know how dangerous it is to touch request.body (as by default,
|
|
698
744
|
// koa-better-body is saving some async function in request.body)
|
|
699
745
|
// We patch the .body on the `request` middleware
|
|
@@ -714,7 +760,8 @@ var init_body_middleware = app => {
|
|
|
714
760
|
*/
|
|
715
761
|
|
|
716
762
|
var _ONLY_WARN = [401];
|
|
717
|
-
function init_catcher_middleware(app
|
|
763
|
+
function init_catcher_middleware(app) {
|
|
764
|
+
var logger = app.context.miolo.logger;
|
|
718
765
|
function catcher_middleware(_x) {
|
|
719
766
|
return _catcher_middleware.apply(this, arguments);
|
|
720
767
|
}
|
|
@@ -759,7 +806,9 @@ function init_catcher_middleware(app, logger) {
|
|
|
759
806
|
app.context.onerror = catcher_middleware;
|
|
760
807
|
}
|
|
761
808
|
|
|
762
|
-
var
|
|
809
|
+
var __my_filename$2 = fileURLToPath(require('url').pathToFileURL(__filename).toString());
|
|
810
|
+
var __my_dirname$2 = path.dirname(__my_filename$2);
|
|
811
|
+
var fallback_favicon_path = path.resolve(__my_dirname$2, './miolo.ico');
|
|
763
812
|
var init_static_middleware = (app, config) => {
|
|
764
813
|
var {
|
|
765
814
|
favicon,
|
|
@@ -861,7 +910,9 @@ function init_session_middleware(app, config) {
|
|
|
861
910
|
app.use(koa_session(options, app));
|
|
862
911
|
}
|
|
863
912
|
|
|
864
|
-
var
|
|
913
|
+
var __my_filename$1 = fileURLToPath(require('url').pathToFileURL(__filename).toString());
|
|
914
|
+
var __my_dirname$1 = path.dirname(__my_filename$1);
|
|
915
|
+
var robots_txt = readFileSync(path.resolve(__my_dirname$1, './robots.txt'), 'utf8');
|
|
865
916
|
function init_route_robots(app) {
|
|
866
917
|
function robots(_x) {
|
|
867
918
|
return _robots.apply(this, arguments);
|
|
@@ -902,8 +953,10 @@ function init_route_catch_js_error(app, route) {
|
|
|
902
953
|
app.use(catch_js_error_router.routes());
|
|
903
954
|
}
|
|
904
955
|
|
|
905
|
-
var
|
|
906
|
-
var
|
|
956
|
+
var __my_filename = fileURLToPath(require('url').pathToFileURL(__filename).toString());
|
|
957
|
+
var __my_dirname = path.dirname(__my_filename);
|
|
958
|
+
var indexHTMLPath = path.resolve(__my_dirname, 'fallback_index.html');
|
|
959
|
+
var indexHTML = readFileSync(indexHTMLPath, 'utf8');
|
|
907
960
|
function init_route_html_render(app, html) {
|
|
908
961
|
// Server-side render
|
|
909
962
|
function html_render(_x) {
|
|
@@ -933,29 +986,33 @@ function miolo(_x, _x2, _x3) {
|
|
|
933
986
|
function _miolo() {
|
|
934
987
|
_miolo = _asyncToGenerator(function* (sconfig, render, callback) {
|
|
935
988
|
var _config$http, _config$auth, _config$auth2, _config$auth3;
|
|
936
|
-
// Init some pieces
|
|
937
|
-
var config = init_config(sconfig);
|
|
938
|
-
var emailer = init_emailer(config.mail.options, config.mail.defaults);
|
|
939
|
-
var logger = init_logger(config.log, emailer);
|
|
940
|
-
config.db.connection.options.log = logger;
|
|
941
989
|
var app = new Koa();
|
|
942
990
|
|
|
943
|
-
//
|
|
944
|
-
|
|
991
|
+
// Init some pieces
|
|
992
|
+
var config = init_config(sconfig);
|
|
993
|
+
//const emailer = init_emailer(config.mail.options, config.mail.defaults)
|
|
994
|
+
//const logger = init_logger(config.log, emailer)
|
|
995
|
+
//config.db.connection.options.log= logger
|
|
945
996
|
|
|
997
|
+
// // attach to app calustra's db methods
|
|
998
|
+
// initCalustraDbContext(app, config.db)
|
|
999
|
+
//
|
|
1000
|
+
// // attach to app some custom miolo methods
|
|
1001
|
+
// app.context.miolo = {
|
|
1002
|
+
// config: {...config},
|
|
1003
|
+
// emailer,
|
|
1004
|
+
// logger,
|
|
1005
|
+
// db: app.context.db
|
|
1006
|
+
// }
|
|
1007
|
+
//
|
|
946
1008
|
// attach to app some custom miolo methods
|
|
947
|
-
app
|
|
948
|
-
config: _objectSpread2({}, config),
|
|
949
|
-
emailer,
|
|
950
|
-
logger,
|
|
951
|
-
db: app.context.db
|
|
952
|
-
};
|
|
1009
|
+
init_context_middleware(app, config);
|
|
953
1010
|
|
|
954
1011
|
// Compress and body parser
|
|
955
1012
|
init_body_middleware(app);
|
|
956
1013
|
|
|
957
1014
|
// override koa's undocumented error handler
|
|
958
|
-
init_catcher_middleware(app
|
|
1015
|
+
init_catcher_middleware(app);
|
|
959
1016
|
|
|
960
1017
|
// Serve static files
|
|
961
1018
|
init_static_middleware(app, (_config$http = config.http) === null || _config$http === void 0 ? void 0 : _config$http.static);
|
|
@@ -980,7 +1037,7 @@ function _miolo() {
|
|
|
980
1037
|
var {
|
|
981
1038
|
init_guest_auth_middleware
|
|
982
1039
|
} = yield Promise.resolve().then(function () { return guest; });
|
|
983
|
-
init_guest_auth_middleware(app, config.auth.guest, config === null || config === void 0 ? void 0 : config.session
|
|
1040
|
+
init_guest_auth_middleware(app, config.auth.guest, config === null || config === void 0 ? void 0 : config.session);
|
|
984
1041
|
}
|
|
985
1042
|
if (config !== null && config !== void 0 && (_config$auth2 = config.auth) !== null && _config$auth2 !== void 0 && _config$auth2.basic) {
|
|
986
1043
|
var {
|
|
@@ -997,7 +1054,8 @@ function _miolo() {
|
|
|
997
1054
|
|
|
998
1055
|
// Routes to /crud
|
|
999
1056
|
if (config !== null && config !== void 0 && config.routes) {
|
|
1000
|
-
|
|
1057
|
+
var conn = app.context.miolo.db.getConnection();
|
|
1058
|
+
initCalustraRouter(app, conn, config.routes);
|
|
1001
1059
|
}
|
|
1002
1060
|
// Socket.io
|
|
1003
1061
|
// const io= init_socket(logger)
|
|
@@ -1010,7 +1068,7 @@ function _miolo() {
|
|
|
1010
1068
|
}
|
|
1011
1069
|
|
|
1012
1070
|
// CORS and other headers
|
|
1013
|
-
init_headers_middleware(app, config.http
|
|
1071
|
+
init_headers_middleware(app, config.http);
|
|
1014
1072
|
|
|
1015
1073
|
// Middleware for html render
|
|
1016
1074
|
if (render == undefined || render.html != undefined) {
|
|
@@ -1019,8 +1077,8 @@ function _miolo() {
|
|
|
1019
1077
|
app.use(render.middleware);
|
|
1020
1078
|
}
|
|
1021
1079
|
app.listen(config.http.port, config.http.hostname, function () {
|
|
1022
|
-
logger.info("miolo is listening on ".concat(config.http.hostname, ":").concat(config.http.port));
|
|
1023
|
-
init_cron(logger);
|
|
1080
|
+
app.context.miolo.logger.info("miolo is listening on ".concat(config.http.hostname, ":").concat(config.http.port));
|
|
1081
|
+
init_cron(app.context.miolo.logger);
|
|
1024
1082
|
if (callback != undefined) {
|
|
1025
1083
|
callback();
|
|
1026
1084
|
}
|
|
@@ -1030,69 +1088,143 @@ function _miolo() {
|
|
|
1030
1088
|
return _miolo.apply(this, arguments);
|
|
1031
1089
|
}
|
|
1032
1090
|
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1091
|
+
var Context = /*#__PURE__*/React.createContext();
|
|
1092
|
+
|
|
1093
|
+
var AppContext = _ref => {
|
|
1094
|
+
var {
|
|
1095
|
+
context,
|
|
1096
|
+
children
|
|
1097
|
+
} = _ref;
|
|
1098
|
+
var [innerContext, setInnerContext] = useState(context);
|
|
1099
|
+
useEffect(() => {
|
|
1100
|
+
setInnerContext(context);
|
|
1101
|
+
}, [context]);
|
|
1102
|
+
return /*#__PURE__*/React.createElement(Context.Provider, {
|
|
1103
|
+
value: {
|
|
1104
|
+
context: innerContext,
|
|
1105
|
+
setContext: setInnerContext
|
|
1106
|
+
}
|
|
1107
|
+
}, children);
|
|
1108
|
+
};
|
|
1109
|
+
|
|
1110
|
+
var AppSsr = _ref => {
|
|
1111
|
+
var {
|
|
1112
|
+
url,
|
|
1113
|
+
context,
|
|
1114
|
+
children
|
|
1115
|
+
} = _ref;
|
|
1116
|
+
return /*#__PURE__*/React.createElement(StaticRouter, {
|
|
1117
|
+
location: url
|
|
1118
|
+
}, /*#__PURE__*/React.createElement(AppContext, {
|
|
1119
|
+
context: context
|
|
1120
|
+
}, children));
|
|
1121
|
+
};
|
|
1122
|
+
|
|
1123
|
+
var ErrMessage = _ref => {
|
|
1124
|
+
var {
|
|
1125
|
+
ctx,
|
|
1126
|
+
error
|
|
1127
|
+
} = _ref;
|
|
1128
|
+
return /*#__PURE__*/React.createElement("div", null, "[MIOLO-SERVER_TOOLS] Missing SSR renderer: ".concat(error.toString()));
|
|
1129
|
+
};
|
|
1130
|
+
|
|
1131
|
+
var html = "\n\t\t<!DOCTYPE html>\n\t\t<html lang=\"en\">\n\t\t<head>\n\t\t\t<meta charset=\"utf-8\">\n\t\t\t<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n\t\t\t<title>miolo</title>\n\t\t\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n\t\t\t<meta name=\"description\" content=\"miolo\" />\n\t\t\t<meta name=\"keywords\" content=\"miolo web\" />\n\t\t\t<meta name=\"author\" content=\"miolo.afialapis.com\" />\n\n\t\t\t<!-- Touch Icons - iOS and Android 2.1+ 180x180 pixels in size. --> \n\t\t\t<!--<link rel=\"apple-touch-icon-precomposed\" href=\"/favicon.ico\"/>-->\n\t\t\t<!-- Firefox, Chrome, Safari, IE 11+ and Opera. 196x196 pixels in size. -->\n\t\t\t<link rel=\"icon\" href=\"/favicon.ico\"/>\n\n\t\t\t{styles}\n\n\t\t\t<script>\n\t\t\t\twindow.__CONTEXT = {context}\n\t\t\t</script>\t\t\n\t\t</head>\n\n\t\t<body>\n\t\t\t<div id=\"root\">{children}</div>\n\t\t\t{bundle}\n\t\t</body>\n\t\t</html>\n ";
|
|
1132
|
+
|
|
1133
|
+
function init_render_middleware(loader, renderer, options) {
|
|
1134
|
+
// parse options
|
|
1135
|
+
var html$1 = html,
|
|
1136
|
+
port = 8000,
|
|
1137
|
+
use_css = true;
|
|
1138
|
+
try {
|
|
1139
|
+
if ((options === null || options === void 0 ? void 0 : options.html) != undefined) {
|
|
1140
|
+
html$1 = options.html;
|
|
1141
|
+
}
|
|
1142
|
+
} catch (_) {}
|
|
1143
|
+
try {
|
|
1144
|
+
if ((options === null || options === void 0 ? void 0 : options.port) != undefined) {
|
|
1145
|
+
if (!isNaN(parseInt(options.port))) {
|
|
1146
|
+
port = parseInt(options.port);
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
} catch (_) {}
|
|
1150
|
+
try {
|
|
1151
|
+
use_css = (options === null || options === void 0 ? void 0 : options.css) !== false;
|
|
1152
|
+
} catch (_) {}
|
|
1153
|
+
var bundleURL = '';
|
|
1154
|
+
var cssURL = use_css ? '' : '';
|
|
1155
|
+
var def_renderer = ctx => {
|
|
1038
1156
|
try {
|
|
1039
|
-
|
|
1040
|
-
} catch (
|
|
1041
|
-
|
|
1157
|
+
return renderer(ctx);
|
|
1158
|
+
} catch (error) {
|
|
1159
|
+
ctx.miolo.logger.error('Missing renderer in the render middleware');
|
|
1160
|
+
return ErrMessage({
|
|
1161
|
+
ctx,
|
|
1162
|
+
error
|
|
1163
|
+
});
|
|
1042
1164
|
}
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
return yield _getKey(key);
|
|
1055
|
-
});
|
|
1056
|
-
return _redisGet.apply(this, arguments);
|
|
1057
|
-
}
|
|
1058
|
-
function redisExists(_x2) {
|
|
1059
|
-
return _redisExists.apply(this, arguments);
|
|
1060
|
-
}
|
|
1061
|
-
function _redisExists() {
|
|
1062
|
-
_redisExists = _asyncToGenerator(function* (key) {
|
|
1063
|
-
var r = yield _existsKey(key);
|
|
1064
|
-
return r == 1 ? true : false;
|
|
1065
|
-
});
|
|
1066
|
-
return _redisExists.apply(this, arguments);
|
|
1067
|
-
}
|
|
1068
|
-
function redisSet(_x3, _x4) {
|
|
1069
|
-
return _redisSet.apply(this, arguments);
|
|
1070
|
-
}
|
|
1071
|
-
function _redisSet() {
|
|
1072
|
-
_redisSet = _asyncToGenerator(function* (key, value) {
|
|
1073
|
-
var expiration = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 86400;
|
|
1074
|
-
var r = yield _setKey(key, value, 'EX', expiration);
|
|
1075
|
-
return r == 'OK' ? true : false;
|
|
1165
|
+
};
|
|
1166
|
+
var def_loader = /*#__PURE__*/function () {
|
|
1167
|
+
var _ref = _asyncToGenerator(function* (ctx) {
|
|
1168
|
+
var res = {};
|
|
1169
|
+
try {
|
|
1170
|
+
res = yield loader(ctx);
|
|
1171
|
+
} catch (e) {
|
|
1172
|
+
ctx.miolo.logger.error('Error produced by loader in the render middleware');
|
|
1173
|
+
ctx.miolo.logger.error(e);
|
|
1174
|
+
}
|
|
1175
|
+
return res;
|
|
1076
1176
|
});
|
|
1077
|
-
return
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1177
|
+
return function def_loader(_x) {
|
|
1178
|
+
return _ref.apply(this, arguments);
|
|
1179
|
+
};
|
|
1180
|
+
}();
|
|
1181
|
+
function render_middleware(_x2) {
|
|
1182
|
+
return _render_middleware.apply(this, arguments);
|
|
1081
1183
|
}
|
|
1082
|
-
function
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1184
|
+
function _render_middleware() {
|
|
1185
|
+
_render_middleware = _asyncToGenerator(function* (ctx) {
|
|
1186
|
+
/*
|
|
1187
|
+
|
|
1188
|
+
*/
|
|
1189
|
+
var ssr_data = yield def_loader(ctx);
|
|
1190
|
+
//const ssr_comp = def_renderer(ctx)
|
|
1191
|
+
|
|
1192
|
+
var isAuthed = false;
|
|
1193
|
+
try {
|
|
1194
|
+
isAuthed = (ctx === null || ctx === void 0 ? void 0 : ctx.isAuthenticated()) === true;
|
|
1195
|
+
} catch (e) {}
|
|
1196
|
+
try {
|
|
1197
|
+
if (!isAuthed) {
|
|
1198
|
+
isAuthed = ctx.user.name === 'guest' && ctx.user.token != undefined;
|
|
1199
|
+
}
|
|
1200
|
+
} catch (e) {}
|
|
1201
|
+
var user = undefined;
|
|
1202
|
+
try {
|
|
1203
|
+
if (ctx.state.user != undefined) {
|
|
1204
|
+
user = ctx.state.user;
|
|
1205
|
+
}
|
|
1206
|
+
} catch (_) {}
|
|
1207
|
+
try {
|
|
1208
|
+
if (ctx.user != undefined) {
|
|
1209
|
+
user = ctx.user;
|
|
1210
|
+
}
|
|
1211
|
+
} catch (_) {}
|
|
1212
|
+
var context = {
|
|
1213
|
+
user: user,
|
|
1214
|
+
authenticated: isAuthed,
|
|
1215
|
+
ssr_data: ssr_data,
|
|
1216
|
+
extra: ctx === null || ctx === void 0 ? void 0 : ctx.extra
|
|
1217
|
+
};
|
|
1218
|
+
var ssr_html = renderToString( /*#__PURE__*/React.createElement(AppSsr, {
|
|
1219
|
+
url: ctx.url,
|
|
1220
|
+
context: context
|
|
1221
|
+
}, () => def_renderer(ctx)));
|
|
1222
|
+
var parsed_html = html$1.replace('{context}', JSON.stringify(context, null, 2)).replace(/{bundleURL}/g, bundleURL).replace('{children}', ssr_html).replace('{styles}', cssURL).replace('{bundle}', bundleURL);
|
|
1223
|
+
ctx.body = parsed_html;
|
|
1086
1224
|
});
|
|
1087
|
-
return
|
|
1225
|
+
return _render_middleware.apply(this, arguments);
|
|
1088
1226
|
}
|
|
1089
|
-
|
|
1090
|
-
get: redisGet,
|
|
1091
|
-
exists: redisExists,
|
|
1092
|
-
set: redisSet,
|
|
1093
|
-
del: redisDel
|
|
1094
|
-
};
|
|
1095
|
-
return cacher;
|
|
1227
|
+
return render_middleware;
|
|
1096
1228
|
}
|
|
1097
1229
|
|
|
1098
1230
|
function _guest_token_make_with_jwt(session, logger) {
|
|
@@ -1121,7 +1253,8 @@ function _get_cookie_properties(session) {
|
|
|
1121
1253
|
httpOnly: false
|
|
1122
1254
|
};
|
|
1123
1255
|
}
|
|
1124
|
-
var init_guest_auth_middleware = (app, options, session
|
|
1256
|
+
var init_guest_auth_middleware = (app, options, session) => {
|
|
1257
|
+
var logger = app.context.miolo.logger;
|
|
1125
1258
|
var _make_guest_token = () => {
|
|
1126
1259
|
try {
|
|
1127
1260
|
var {
|
|
@@ -1325,5 +1458,5 @@ var passport = /*#__PURE__*/Object.freeze({
|
|
|
1325
1458
|
init_passport_auth_middleware: init_passport_auth_middleware
|
|
1326
1459
|
});
|
|
1327
1460
|
|
|
1328
|
-
export {
|
|
1329
|
-
//# sourceMappingURL=miolo.mjs.map
|
|
1461
|
+
export { AppSsr, init_render_middleware, miolo };
|
|
1462
|
+
//# sourceMappingURL=miolo.server.mjs.map
|