This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserverSetup.js
394 lines (381 loc) · 16 KB
/
serverSetup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
For copyright information, see the AUTHORS.md file in the docs directory of this distribution and at
https://github.com/fluid-project/sjrk-story-telling/blob/main/docs/AUTHORS.md
Licensed under the New BSD license. You may not use this file except in compliance with this licence.
You may obtain a copy of the BSD License at
https://raw.githubusercontent.com/fluid-project/sjrk-story-telling/main/LICENSE.txt
*/
"use strict";
var fluid = require("infusion"),
sjrk = fluid.registerNamespace("sjrk"),
kettle = require("kettle"),
fs = require("fs"),
MemoryStore = require("memorystore")(kettle.npm.expressSession);
require("fluid-express-user");
// The main Kettle server configuration grade
fluid.defaults("sjrk.storyTelling.server", {
gradeNames: ["fluid.component"],
components: {
// the Kettle server proper
server: {
type: "kettle.server",
options: {
gradeNames: ["kettle.server.sessionAware", "fluid.contextAware"],
globalConfig: {
// Config values are stored in the external config file
// named "sjrk.storyTelling.server.themed.json5"
// and are merged in on server startup
// port: "",
// theme: "",
// themeIndexFile": "",
// authoringEnabled: true
// sessionCheckPeriod: 86400000 //in ms
},
secureConfig: {
baseThemeName: "base",
themesPath: "./themes/%theme",
binaryUploadDirectory: "./uploads",
uploadedFilesHandlerPath: "/uploads",
deletedFilesRecoveryPath: "/deleted_uploads",
secretsConfigPath: "./secrets.json",
secrets: "@expand:sjrk.storyTelling.server.resolveJSONFile({that}.options.secureConfig.secretsConfigPath)"
},
port: "{that}.options.globalConfig.port",
session: {
store: "@expand:sjrk.storyTelling.server.makeMemorySessionStore({that}.options.globalConfig.sessionCheckPeriod)",
middlewareOptions: {
secret: "{server}.options.secureConfig.secrets.session"
}
},
authorCredentialConfig: "{that}.options.secureConfig.secrets.authorCredentialConfig",
contextAwareness: {
protocol: {
checks: {
https: {
contextValue: "{that}.options.secureConfig.secrets.https",
gradeNames: "sjrk.storyTelling.server.https"
}
}
}
},
distributeOptions: {
couchDBURL: {
record: "@expand:kettle.resolvers.env(COUCHDB_URL)",
target: "{that sjrk.storyTelling.server.dataSource.couch.core}.options.host"
},
couchDBAuthorsURL: {
record: "@expand:kettle.resolvers.env(COUCHDB_URL)",
target: "{that expressUserUtils}.options.dataSourceConfig.host"
},
sessionOptions: {
source: "{that}.options.session",
target: "{that > kettle.middlewareHolder > session}.options"
},
authorCredentialConfig: {
source: "{that}.options.authorCredentialConfig",
target: "{that > expressUserUtils}.options"
}
},
components: {
// user authentication
expressUserUtils: {
type: "fluid.express.user.utils",
options: {
iterations: 100,
digest: "blake2b512",
dataSourceConfig: {
host: "http://localhost:5984",
path: "stories"
},
rules: {
createUserWrite: {
"_id": "userData.authorID",
"authorID": "userData.authorID" // may only need _id in the document
}
},
couch: {
userDbUrl: {
expander: {
funcName: "fluid.stringTemplate",
args: ["%host/%path", {
host: "{expressUserUtils}.options.dataSourceConfig.host",
path: "{expressUserUtils}.options.dataSourceConfig.path"
}]
}
}
}
}
},
// a DataSource to get a list of stories
viewDataSource: {
type: "sjrk.storyTelling.server.dataSource.couch.view"
},
// a DataSource to get a stories by author
storyByAuthorDataSource: {
type: "sjrk.storyTelling.server.dataSource.couch.authorStoriesView"
},
// a DataSource to get or save a single story
storyDataSource: {
type: "sjrk.storyTelling.server.dataSource.couch.story"
},
// a DataSource to delete a single story
deleteStoryDataSource: {
type: "sjrk.storyTelling.server.dataSource.couch.deleteStory"
},
// the Kettle app
app: {
type: "sjrk.storyTelling.server.app.storyTellingHandlers"
},
// middleware to save a block's file to the server filesystem
saveStoryFile: {
type: "sjrk.storyTelling.server.middleware.saveStoryFile",
options: {
components: {
storage: {
options: {
destination: "{server}.options.secureConfig.binaryUploadDirectory"
}
}
}
}
},
// middleware to coordinate HTTP Basic Authentication
basicAuth: {
type: "kettle.middleware.basicAuth",
options: {
middlewareOptions: {
users: {
"admin": "{server}.options.secureConfig.secrets.adminPass"
},
challenge: true
}
}
},
// middleware to restrict serving to only those directories listed
nodeModulesFilter: {
type: "sjrk.storyTelling.server.staticMiddlewareSubdirectoryFilter",
options: {
allowedSubdirectories: [
"ajv",
"fluid-binder",
"fluid-handlebars",
"fluid-json-schema",
"fluid-location-bar-relay",
"handlebars",
"infusion",
"markdown-it"
]
}
},
// static middleware for the node_modules directory
nodeModules: {
type: "kettle.middleware.static",
options: {
"root": "./node_modules"
}
},
// static middleware for the uploads directory
uploads: {
type: "kettle.middleware.static",
options: {
"root": "{server}.options.secureConfig.binaryUploadDirectory"
}
},
// static middleware for the ui directory
ui: {
type: "kettle.middleware.static",
options: {
"root": "./src/ui"
}
},
// static middleware for the static files directory
static: {
type: "kettle.middleware.static",
options: {
"root": "./src/static"
}
},
// the custom theme for the site, loaded in "on top" of base
currentTheme: {
type: "kettle.middleware.static",
options: {
root: "@expand:sjrk.storyTelling.server.getThemePath({server}.options.globalConfig.theme, {server}.options.secureConfig.themesPath)",
middlewareOptions: {
index: "{server}.options.globalConfig.themeIndexFile"
}
}
},
// the default theme and base pages for the site
baseTheme: {
type: "kettle.middleware.static",
options: {
root: "@expand:sjrk.storyTelling.server.getThemePath({server}.options.secureConfig.baseThemeName, {server}.options.secureConfig.themesPath)",
middlewareOptions: {
index: "storyBrowse.html"
}
}
}
}
}
}
}
});
fluid.defaults("sjrk.storyTelling.server.https", {
httpsServerOptions: {
key: {
expander: {
func: "fs.readFileSync",
args: ["{that}.options.secureConfig.secrets.https.key"]
}
},
cert: {
expander: {
func: "fs.readFileSync",
args: ["{that}.options.secureConfig.secrets.https.cert"]
}
}
},
members: {
httpServer: "@expand:kettle.server.httpsServer({that}.options.httpsServerOptions, {kettle.server}.expressApp)"
}
});
// the Kettle app
fluid.defaults("sjrk.storyTelling.server.app.storyTellingHandlers", {
gradeNames: ["kettle.app"],
components: {
loginValidator: {
type: "sjrk.storyTelling.server.loginValidator"
},
signupValidator: {
type: "sjrk.storyTelling.server.signupValidator"
}
},
requestHandlers: {
browseStoriesHandler: {
type: "sjrk.storyTelling.server.browseStoriesHandler",
"route": "/stories/",
"method": "get"
},
getStoryHandler: {
type: "sjrk.storyTelling.server.getStoryHandler",
"route": "/stories/:id",
"method": "get"
},
getEditStoryHandler: {
type: "sjrk.storyTelling.server.getEditStoryHandler",
"route": "/stories/:id/edit",
"method": "get"
},
saveStoryHandler: {
type: "sjrk.storyTelling.server.saveStoryHandler",
"route": "/stories/",
"method": "post"
},
saveStoryFileHandler: {
type: "sjrk.storyTelling.server.saveStoryFileHandler",
"route": "/stories/:id",
"method": "post"
},
deleteStoryHandler: {
type: "sjrk.storyTelling.server.deleteStoryHandler",
"route": "/admin/deleteStory/:id",
"method": "get"
},
uiHandler: {
type: "sjrk.storyTelling.server.uiHandler",
"route": "/*",
"prefix": "/src",
"method": "get"
},
nodeModulesHandler: {
type: "sjrk.storyTelling.server.nodeModulesHandler",
"route": "/*",
"method": "get",
"prefix": "/node_modules"
},
uploadsHandler: {
type: "sjrk.storyTelling.server.uploadsHandler",
"route": "/*",
"method": "get",
"prefix": "{server}.options.secureConfig.uploadedFilesHandlerPath"
},
clientConfigHandler: {
type: "sjrk.storyTelling.server.clientConfigHandler",
route: "/clientConfig",
method: "get"
},
sessionHandler: {
type: "sjrk.storyTelling.server.sessionHandler",
"route": "/session",
method: "get"
},
signupHandler: {
type: "sjrk.storyTelling.server.signupHandler",
"route": "/signup",
method: "post"
},
loginHandler: {
type: "sjrk.storyTelling.server.loginHandler",
"route": "/login",
method: "post"
},
logoutHandler: {
type: "sjrk.storyTelling.server.logoutHandler",
"route": "/logout",
method: "post"
},
themeHandler: {
type: "sjrk.storyTelling.server.themeHandler",
"route": "/*",
"method": "get"
}
}
});
/**
* Resolves a JSON file and parses it before returning it
*
* @param {String} jsonFilePath - the path to the JSON file to parse
*
* @return {Object} - the parsed file contents
*/
sjrk.storyTelling.server.resolveJSONFile = function (jsonFilePath) {
var file = kettle.resolvers.file(jsonFilePath);
return JSON.parse(file);
};
/**
* Returns the path to the custom theme folder. The theme folder's name is
* expected to match the theme name being passed in. If the theme is not
* specified, the path returend will be the current directory ("."). If the
* theme is specified but the resolved folder doesn't exist within themeFolder,
* an error will be reported.
*
* @param {String} theme - The name of the theme for which to find the path
* @param {String} themeFolder - The folder/path that contains the theme being retrieved
*
* @return {String} - the custom theme directory's path
*/
sjrk.storyTelling.server.getThemePath = function (theme, themeFolder) {
var themePath = ".";
if (theme) {
themePath = fluid.stringTemplate(themeFolder, { theme: theme });
if (!fs.existsSync(themePath)) {
fluid.fail("The custom theme folder " + themePath + " does not exist. Please verify that the theme name is configured properly.");
}
}
return themePath;
};
/**
* Creates an in memory session store for use by the session middleware. Configured for sessions to expire after 24hrs.
*
* @param {Integer} [checkPeriod] - (optional) the time in ms between checks to prune expired sessions. Defaults to
* 86400000 ms / 24 hr.
* @return {Object} - a MemoryStore instance
*/
sjrk.storyTelling.server.makeMemorySessionStore = function (checkPeriod) {
// TODO: Currently using https://www.npmjs.com/package/memorystore as it is a production ready memory store;
// however, the session should eventually be stored in a database to prevent clearing on server restart.
// https://issues.fluidproject.org/browse/SJRK-444
return new MemoryStore({
checkPeriod: checkPeriod || 86400000 // prune expired entries every 24h
});
};