@saltcorn/server 0.6.1-beta.1 → 0.6.2-beta.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/auth/index.js +15 -2
- package/index.js +17 -1
- package/locales/en.json +7 -1
- package/markup/admin.js +24 -17
- package/markup/index.js +14 -1
- package/package.json +14 -8
- package/public/saltcorn.js +17 -1
- package/restart_watcher.js +146 -0
- package/routes/fields.js +13 -13
- package/routes/homepage.js +124 -104
- package/routes/index.js +37 -0
- package/routes/tables.js +21 -19
- package/routes/tenant.js +58 -25
- package/routes/utils.js +1 -1
- package/routes/view.js +4 -4
- package/serve.js +125 -113
package/serve.js
CHANGED
|
@@ -33,11 +33,14 @@ const { setTenant, getSessionStore } = require("./routes/utils");
|
|
|
33
33
|
const passport = require("passport");
|
|
34
34
|
const { authenticate } = require("passport");
|
|
35
35
|
const View = require("@saltcorn/data/models/view");
|
|
36
|
+
const {
|
|
37
|
+
listenForChanges,
|
|
38
|
+
getRelevantPackages,
|
|
39
|
+
getPluginDirectories,
|
|
40
|
+
} = require("./restart_watcher");
|
|
36
41
|
|
|
37
42
|
// helpful https://gist.github.com/jpoehls/2232358
|
|
38
|
-
|
|
39
43
|
/**
|
|
40
|
-
* @param {object} opts
|
|
41
44
|
* @param {object} opts
|
|
42
45
|
* @param {boolean} opts.disableMigrate
|
|
43
46
|
* @param {boolean} [useClusterAdaptor = true]
|
|
@@ -74,9 +77,9 @@ const initMaster = async ({ disableMigrate }, useClusterAdaptor = true) => {
|
|
|
74
77
|
};
|
|
75
78
|
|
|
76
79
|
/**
|
|
77
|
-
* @param {object} opts
|
|
78
|
-
* @param {object} opts.tenant
|
|
79
|
-
* @param {...*} opts.msg
|
|
80
|
+
* @param {object} opts
|
|
81
|
+
* @param {object} opts.tenant
|
|
82
|
+
* @param {...*} opts.msg
|
|
80
83
|
* @returns {void}
|
|
81
84
|
*/
|
|
82
85
|
const workerDispatchMsg = ({ tenant, ...msg }) => {
|
|
@@ -90,8 +93,12 @@ const workerDispatchMsg = ({ tenant, ...msg }) => {
|
|
|
90
93
|
});
|
|
91
94
|
}
|
|
92
95
|
if (msg.refresh) getState()[`refresh_${msg.refresh}`](true);
|
|
93
|
-
if (msg.createTenant)
|
|
96
|
+
if (msg.createTenant) {
|
|
94
97
|
create_tenant(msg.createTenant, loadAllPlugins, "", true);
|
|
98
|
+
db.runWithTenant(msg.createTenant, async () => {
|
|
99
|
+
getState().refresh(true);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
95
102
|
if (msg.installPlugin) {
|
|
96
103
|
loadAndSaveNewPlugin(msg.installPlugin, msg.force, true);
|
|
97
104
|
}
|
|
@@ -100,11 +107,11 @@ const workerDispatchMsg = ({ tenant, ...msg }) => {
|
|
|
100
107
|
};
|
|
101
108
|
|
|
102
109
|
/**
|
|
103
|
-
*
|
|
104
|
-
* @param {*} masterState
|
|
105
|
-
* @param {object} opts
|
|
106
|
-
* @param {string} opts.port
|
|
107
|
-
* @param {boolean} opts.watchReaper
|
|
110
|
+
*
|
|
111
|
+
* @param {*} masterState
|
|
112
|
+
* @param {object} opts
|
|
113
|
+
* @param {string} opts.port
|
|
114
|
+
* @param {boolean} opts.watchReaper
|
|
108
115
|
* @param {boolean} opts.disableScheduler
|
|
109
116
|
* @param {number} opts.pid
|
|
110
117
|
* @returns {function}
|
|
@@ -133,120 +140,125 @@ const onMessageFromWorker = (
|
|
|
133
140
|
}
|
|
134
141
|
};
|
|
135
142
|
|
|
136
|
-
module.exports =
|
|
137
|
-
/**
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
143
|
+
module.exports =
|
|
144
|
+
/**
|
|
145
|
+
* @function
|
|
146
|
+
* @name "module.exports function"
|
|
147
|
+
* @param {object} [opts = {}]
|
|
148
|
+
* @param {number} [opts.port = 3000]
|
|
149
|
+
* @param {boolean} opts.watchReaper
|
|
150
|
+
* @param {boolean} opts.disableScheduler
|
|
151
|
+
* @param {number} opts.defaultNCPUs
|
|
152
|
+
* @param {boolean} opts.dev
|
|
153
|
+
* @param {...*} opts.appargs
|
|
154
|
+
* @returns {Promise<void>}
|
|
155
|
+
*/
|
|
156
|
+
async ({
|
|
157
|
+
port = 3000,
|
|
158
|
+
watchReaper,
|
|
159
|
+
disableScheduler,
|
|
160
|
+
defaultNCPUs,
|
|
161
|
+
dev,
|
|
162
|
+
...appargs
|
|
163
|
+
} = {}) => {
|
|
164
|
+
if (dev && cluster.isMaster) {
|
|
165
|
+
listenForChanges(getRelevantPackages(), await getPluginDirectories());
|
|
166
|
+
}
|
|
167
|
+
const useNCpus = process.env.SALTCORN_NWORKERS
|
|
168
|
+
? +process.env.SALTCORN_NWORKERS
|
|
169
|
+
: defaultNCPUs;
|
|
158
170
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
171
|
+
const letsEncrypt = await getConfig("letsencrypt", false);
|
|
172
|
+
const masterState = {
|
|
173
|
+
started: false,
|
|
174
|
+
listeningTo: new Set([]),
|
|
175
|
+
};
|
|
164
176
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
177
|
+
const addWorker = (worker) => {
|
|
178
|
+
worker.on(
|
|
179
|
+
"message",
|
|
180
|
+
onMessageFromWorker(masterState, {
|
|
181
|
+
port,
|
|
182
|
+
watchReaper,
|
|
183
|
+
disableScheduler,
|
|
184
|
+
pid: worker.process.pid,
|
|
185
|
+
})
|
|
186
|
+
);
|
|
187
|
+
};
|
|
176
188
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
189
|
+
if (port === 80 && letsEncrypt) {
|
|
190
|
+
const admin_users = await User.find({ role_id: 1 }, { orderBy: "id" });
|
|
191
|
+
const file_store = db.connectObj.file_store;
|
|
192
|
+
const Greenlock = require("greenlock");
|
|
193
|
+
const greenlock = Greenlock.create({
|
|
194
|
+
packageRoot: __dirname,
|
|
195
|
+
configDir: path.join(file_store, "greenlock.d"),
|
|
196
|
+
maintainerEmail: admin_users[0].email,
|
|
197
|
+
});
|
|
198
|
+
const certs = await greenlock._find({});
|
|
199
|
+
console.log("Certificates:", certs);
|
|
188
200
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
201
|
+
if (certs && certs.length > 0) {
|
|
202
|
+
const app = await getApp(appargs);
|
|
203
|
+
const timeout = +getState().getConfig("timeout", 120);
|
|
204
|
+
const initMasterListeners = () => {
|
|
205
|
+
Object.entries(cluster.workers).forEach(([id, w]) => {
|
|
206
|
+
if (!masterState.listeningTo.has(id)) {
|
|
207
|
+
addWorker(w);
|
|
208
|
+
masterState.listeningTo.add(id);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
if (masterState.listeningTo.size < useNCpus)
|
|
212
|
+
setTimeout(initMasterListeners, 250);
|
|
213
|
+
};
|
|
214
|
+
require("greenlock-express")
|
|
215
|
+
.init({
|
|
216
|
+
packageRoot: __dirname,
|
|
217
|
+
configDir: path.join(file_store, "greenlock.d"),
|
|
218
|
+
maintainerEmail: admin_users[0].email,
|
|
219
|
+
cluster: true,
|
|
220
|
+
workers: useNCpus,
|
|
221
|
+
})
|
|
222
|
+
.ready((glx) => {
|
|
223
|
+
const httpsServer = glx.httpsServer();
|
|
224
|
+
setupSocket(httpsServer);
|
|
225
|
+
httpsServer.setTimeout(timeout * 1000);
|
|
226
|
+
process.on("message", workerDispatchMsg);
|
|
227
|
+
glx.serveApp(app);
|
|
228
|
+
process.send && process.send("Start");
|
|
229
|
+
})
|
|
230
|
+
.master(() => {
|
|
231
|
+
initMaster(appargs).then(initMasterListeners);
|
|
232
|
+
});
|
|
221
233
|
|
|
222
|
-
|
|
234
|
+
return; // none of stuff below will execute
|
|
235
|
+
}
|
|
223
236
|
}
|
|
224
|
-
|
|
225
|
-
// No greenlock!
|
|
237
|
+
// No greenlock!
|
|
226
238
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
239
|
+
if (cluster.isMaster) {
|
|
240
|
+
const forkAnyWorkers = useNCpus > 1 && process.platform !== "win32";
|
|
241
|
+
await initMaster(appargs, forkAnyWorkers);
|
|
230
242
|
|
|
231
|
-
|
|
232
|
-
|
|
243
|
+
if (forkAnyWorkers) {
|
|
244
|
+
for (let i = 0; i < useNCpus; i++) addWorker(cluster.fork());
|
|
233
245
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
246
|
+
cluster.on("exit", (worker, code, signal) => {
|
|
247
|
+
console.log(`worker ${worker.process.pid} died`);
|
|
248
|
+
addWorker(cluster.fork());
|
|
249
|
+
});
|
|
250
|
+
} else {
|
|
251
|
+
await nonGreenlockWorkerSetup(appargs, port);
|
|
252
|
+
}
|
|
253
|
+
Trigger.emitEvent("Startup");
|
|
238
254
|
} else {
|
|
239
255
|
await nonGreenlockWorkerSetup(appargs, port);
|
|
240
256
|
}
|
|
241
|
-
|
|
242
|
-
} else {
|
|
243
|
-
await nonGreenlockWorkerSetup(appargs, port);
|
|
244
|
-
}
|
|
245
|
-
};
|
|
257
|
+
};
|
|
246
258
|
|
|
247
259
|
/**
|
|
248
|
-
* @param {*} appargs
|
|
249
|
-
* @param {*} port
|
|
260
|
+
* @param {*} appargs
|
|
261
|
+
* @param {*} port
|
|
250
262
|
* @returns {Promise<void>}
|
|
251
263
|
*/
|
|
252
264
|
const nonGreenlockWorkerSetup = async (appargs, port) => {
|
|
@@ -294,8 +306,8 @@ const nonGreenlockWorkerSetup = async (appargs, port) => {
|
|
|
294
306
|
};
|
|
295
307
|
|
|
296
308
|
/**
|
|
297
|
-
*
|
|
298
|
-
* @param {...*} servers
|
|
309
|
+
*
|
|
310
|
+
* @param {...*} servers
|
|
299
311
|
*/
|
|
300
312
|
const setupSocket = (...servers) => {
|
|
301
313
|
// https://socket.io/docs/v4/middlewares/
|