@saltcorn/server 0.6.0-beta.3 → 0.6.0-beta.4

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/app.js CHANGED
@@ -112,6 +112,12 @@ const getApp = async (opts = {}) => {
112
112
  )
113
113
  );
114
114
 
115
+ if (process.env.SALTCORN_SERVE_ADDITIONAL_DIR)
116
+ app.use(
117
+ express.static(process.env.SALTCORN_SERVE_ADDITIONAL_DIR, {
118
+ maxAge: development_mode ? 0 : 1000 * 60 * 15,
119
+ })
120
+ );
115
121
  let version_tag = db.connectObj.version_tag;
116
122
 
117
123
  app.use(
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@saltcorn/server",
3
- "version": "0.6.0-beta.3",
3
+ "version": "0.6.0-beta.4",
4
4
  "description": "Server app for Saltcorn, open-source no-code platform",
5
5
  "homepage": "https://saltcorn.com",
6
6
  "main": "index.js",
7
7
  "license": "MIT",
8
8
  "dependencies": {
9
- "@saltcorn/base-plugin": "0.6.0-beta.3",
10
- "@saltcorn/builder": "0.6.0-beta.3",
11
- "@saltcorn/data": "0.6.0-beta.3",
9
+ "@saltcorn/base-plugin": "0.6.0-beta.4",
10
+ "@saltcorn/builder": "0.6.0-beta.4",
11
+ "@saltcorn/data": "0.6.0-beta.4",
12
12
  "greenlock-express": "^4.0.3",
13
- "@saltcorn/markup": "0.6.0-beta.3",
14
- "@saltcorn/sbadmin2": "0.6.0-beta.3",
13
+ "@saltcorn/markup": "0.6.0-beta.4",
14
+ "@saltcorn/sbadmin2": "0.6.0-beta.4",
15
15
  "@socket.io/cluster-adapter": "^0.1.0",
16
16
  "@socket.io/sticky": "^1.0.1",
17
17
  "connect-flash": "^0.1.1",
package/routes/admin.js CHANGED
@@ -393,11 +393,12 @@ router.post(
393
393
  isAdmin,
394
394
  error_catcher(async (req, res) => {
395
395
  if (db.getTenantSchema() === db.connectObj.default_schema) {
396
- process.send("RestartServer");
397
- //process.exit(0);
396
+ if (process.send) process.send("RestartServer");
397
+ else process.exit(0);
398
398
  } else {
399
399
  await restart_tenant(loadAllPlugins);
400
- process.send({ restart_tenant: true, tenant: db.getTenantSchema() });
400
+ process.send &&
401
+ process.send({ restart_tenant: true, tenant: db.getTenantSchema() });
401
402
  req.flash("success", req.__("Restart complete"));
402
403
  res.redirect("/admin");
403
404
  }
package/routes/utils.js CHANGED
@@ -9,6 +9,8 @@ const { get_base_url } = require("@saltcorn/data/models/config");
9
9
  const { input } = require("@saltcorn/markup/tags");
10
10
  const session = require("express-session");
11
11
  const cookieSession = require("cookie-session");
12
+ const is = require("contractis/is");
13
+
12
14
  function loggedIn(req, res, next) {
13
15
  if (req.user && req.user.id && req.user.tenant === db.getTenantSchema()) {
14
16
  next();
package/serve.js CHANGED
@@ -35,7 +35,7 @@ const View = require("@saltcorn/data/models/view");
35
35
 
36
36
  // helpful https://gist.github.com/jpoehls/2232358
37
37
 
38
- const initMaster = async ({ disableMigrate }) => {
38
+ const initMaster = async ({ disableMigrate }, useClusterAdaptor = true) => {
39
39
  let sql_log;
40
40
  try {
41
41
  sql_log = await getConfig("log_sql");
@@ -63,7 +63,7 @@ const initMaster = async ({ disableMigrate }) => {
63
63
  if (db.is_it_multi_tenant()) {
64
64
  await init_multi_tenant(loadAllPlugins, disableMigrate);
65
65
  }
66
- setupPrimary();
66
+ if (useClusterAdaptor) setupPrimary();
67
67
  };
68
68
 
69
69
  const workerDispatchMsg = ({ tenant, ...msg }) => {
@@ -177,7 +177,7 @@ module.exports = async ({
177
177
  httpsServer.setTimeout(timeout * 1000);
178
178
  process.on("message", workerDispatchMsg);
179
179
  glx.serveApp(app);
180
- process.send("Start");
180
+ process.send && process.send("Start");
181
181
  })
182
182
  .master(() => {
183
183
  initMaster(appargs).then(initMasterListeners);
@@ -189,60 +189,67 @@ module.exports = async ({
189
189
  // No greenlock!
190
190
 
191
191
  if (cluster.isMaster) {
192
- await initMaster(appargs);
192
+ const forkAnyWorkers = useNCpus > 1 && process.platform !== "win32";
193
+ await initMaster(appargs, forkAnyWorkers);
193
194
 
194
- for (let i = 0; i < useNCpus; i++) addWorker(cluster.fork());
195
+ if (forkAnyWorkers) {
196
+ for (let i = 0; i < useNCpus; i++) addWorker(cluster.fork());
195
197
 
198
+ cluster.on("exit", (worker, code, signal) => {
199
+ console.log(`worker ${worker.process.pid} died`);
200
+ addWorker(cluster.fork());
201
+ });
202
+ } else {
203
+ await nonGreenlockWorkerSetup(appargs, port);
204
+ }
196
205
  Trigger.emitEvent("Startup");
197
-
198
- cluster.on("exit", (worker, code, signal) => {
199
- console.log(`worker ${worker.process.pid} died`);
200
- addWorker(cluster.fork());
201
- });
202
206
  } else {
203
- process.on("message", workerDispatchMsg);
207
+ await nonGreenlockWorkerSetup(appargs, port);
208
+ }
209
+ };
204
210
 
205
- const app = await getApp(appargs);
211
+ const nonGreenlockWorkerSetup = async (appargs, port) => {
212
+ process.send && process.on("message", workerDispatchMsg);
206
213
 
207
- const cert = getState().getConfig("custom_ssl_certificate", "");
208
- const key = getState().getConfig("custom_ssl_private_key", "");
209
- const timeout = +getState().getConfig("timeout", 120);
214
+ const app = await getApp(appargs);
210
215
 
211
- // Server with http on port 80 / https on 443
212
- // todo resolve hardcode
213
- if (port === 80 && cert && key) {
214
- const https = require("https");
215
- const http = require("http");
216
- const httpServer = http.createServer(app);
217
- const httpsServer = https.createServer({ key, cert }, app);
218
- // todo timeout to config
219
- httpServer.setTimeout(timeout * 1000);
220
- httpsServer.setTimeout(timeout * 1000);
221
- setupSocket(httpServer, httpsServer);
222
- httpServer.listen(port, () => {
223
- console.log("HTTP Server running on port 80");
224
- });
216
+ const cert = getState().getConfig("custom_ssl_certificate", "");
217
+ const key = getState().getConfig("custom_ssl_private_key", "");
218
+ const timeout = +getState().getConfig("timeout", 120);
219
+ // Server with http on port 80 / https on 443
220
+ // todo resolve hardcode
221
+ if (port === 80 && cert && key) {
222
+ const https = require("https");
223
+ const http = require("http");
224
+ const httpServer = http.createServer(app);
225
+ const httpsServer = https.createServer({ key, cert }, app);
226
+ // todo timeout to config
227
+ httpServer.setTimeout(timeout * 1000);
228
+ httpsServer.setTimeout(timeout * 1000);
229
+ setupSocket(httpServer, httpsServer);
230
+ httpServer.listen(port, () => {
231
+ console.log("HTTP Server running on port 80");
232
+ });
225
233
 
226
- // todo port to config
227
- httpsServer.listen(443, () => {
228
- console.log("HTTPS Server running on port 443");
229
- });
230
- } else {
231
- // server with http only
232
- const http = require("http");
233
- const httpServer = http.createServer(app);
234
- setupSocket(httpServer);
234
+ // todo port to config
235
+ httpsServer.listen(443, () => {
236
+ console.log("HTTPS Server running on port 443");
237
+ });
238
+ } else {
239
+ // server with http only
240
+ const http = require("http");
241
+ const httpServer = http.createServer(app);
242
+ setupSocket(httpServer);
235
243
 
236
- // todo timeout to config
237
- // todo refer in doc to httpserver doc
238
- // todo there can be added other parameters for httpserver
239
- httpServer.setTimeout(timeout * 1000);
240
- httpServer.listen(port, () => {
241
- console.log(`Saltcorn listening on http://localhost:${port}/`);
242
- });
243
- }
244
- process.send("Start");
244
+ // todo timeout to config
245
+ // todo refer in doc to httpserver doc
246
+ // todo there can be added other parameters for httpserver
247
+ httpServer.setTimeout(timeout * 1000);
248
+ httpServer.listen(port, () => {
249
+ console.log(`Saltcorn listening on http://localhost:${port}/`);
250
+ });
245
251
  }
252
+ process.send && process.send("Start");
246
253
  };
247
254
  const setupSocket = (...servers) => {
248
255
  // https://socket.io/docs/v4/middlewares/
@@ -258,7 +265,7 @@ const setupSocket = (...servers) => {
258
265
  io.use(wrap(getSessionStore()));
259
266
  io.use(wrap(passport.initialize()));
260
267
  io.use(wrap(passport.session()));
261
- io.adapter(createAdapter());
268
+ if (process.send) io.adapter(createAdapter());
262
269
  getState().setRoomEmitter((viewname, room_id, msg) => {
263
270
  io.to(`${viewname}_${room_id}`).emit("message", msg);
264
271
  });