Skip to content

Commit d47d160

Browse files
Extract template init code to server-config.js
1 parent 0012143 commit d47d160

File tree

2 files changed

+146
-101
lines changed

2 files changed

+146
-101
lines changed

lib/create-app.js

Lines changed: 9 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ const proxy = require('./handlers/proxy')
1111
const SolidHost = require('./models/solid-host')
1212
const AccountManager = require('./models/account-manager')
1313
const vhost = require('vhost')
14-
const fs = require('fs-extra')
15-
const path = require('path')
1614
const EmailService = require('./models/email-service')
1715
const AccountRecovery = require('./account-recovery')
1816
const capabilityDiscovery = require('./capability-discovery')
1917
const bodyParser = require('body-parser').urlencoded({ extended: false })
2018
const API = require('./api')
2119
const errorPages = require('./handlers/error-pages')
2220
const OidcManager = require('./models/oidc-manager')
21+
const config = require('./server-config')
2322
const defaults = require('../config/defaults')
2423
const options = require('./handlers/options')
2524

@@ -40,27 +39,25 @@ function createApp (argv = {}) {
4039

4140
argv.host = SolidHost.from({ port: argv.port, serverUri: argv.serverUri })
4241

43-
const configPath = initConfigPath(argv)
44-
45-
argv.templates = initTemplateDirs(configPath)
42+
const configPath = config.initConfigDir(argv)
43+
argv.templates = config.initTemplateDirs(configPath)
4644

4745
const ldp = new LDP(argv)
4846
const app = express()
4947

48+
initAppLocals(app, argv, ldp)
49+
initHeaders(app)
50+
5051
// Serve the public 'common' directory (for shared CSS files, etc)
5152
app.use('/common', express.static('common'))
5253

53-
const viewsPath = initDefaultViews(configPath)
54+
const viewsPath = config.initDefaultViews(configPath)
5455
app.set('views', viewsPath)
5556
app.engine('.hbs', handlebars({ extname: '.hbs' }))
5657
app.set('view engine', '.hbs')
5758

58-
initAppLocals(app, argv, ldp)
59-
60-
initHeaders(app)
61-
6259
// Use session cookies
63-
let useSecureCookies = argv.webid // argv.webid forces https and secure cookies
60+
const useSecureCookies = argv.webid // argv.webid forces https and secure cookies
6461
app.use(session(sessionSettings(useSecureCookies, argv.host)))
6562

6663
// Adding proxy
@@ -76,7 +73,7 @@ function createApp (argv = {}) {
7673
}
7774

7875
if (argv.webid) {
79-
ensureWelcomePage(argv)
76+
config.ensureWelcomePage(argv)
8077

8178
var accountRecovery = AccountRecovery({ redirect: '/' })
8279
// adds GET /api/accounts/recover
@@ -116,95 +113,6 @@ function createApp (argv = {}) {
116113
return app
117114
}
118115

119-
function initConfigPath (argv) {
120-
let configPath = path.resolve(argv.configPath)
121-
fs.mkdirp(configPath)
122-
123-
return configPath
124-
}
125-
126-
function initDefaultViews (configPath) {
127-
let defaultViewsPath = path.resolve('./default-views')
128-
let viewsPath = path.join(configPath, 'views')
129-
130-
ensureDirCopy(defaultViewsPath, viewsPath)
131-
132-
return viewsPath
133-
}
134-
135-
function initTemplateDirs (configPath) {
136-
let accountTemplatePath = ensureDirCopy(
137-
'./default-templates/new-account',
138-
path.join(configPath, 'templates', 'new-account')
139-
)
140-
141-
let emailTemplatesPath = ensureDirCopy(
142-
'./default-templates/emails',
143-
path.join(configPath, 'templates', 'emails')
144-
)
145-
146-
let serverTemplatePath = ensureDirCopy(
147-
'./default-templates/server',
148-
path.join(configPath, 'templates', 'server')
149-
)
150-
151-
return {
152-
account: accountTemplatePath,
153-
email: emailTemplatesPath,
154-
server: serverTemplatePath
155-
}
156-
}
157-
158-
function ensureWelcomePage (argv) {
159-
let multiUser = argv.idp
160-
if (!argv.root) {
161-
console.error('Cannot ensure welcome page, no root param')
162-
return
163-
}
164-
let rootDir = path.resolve(argv.root)
165-
let templates = argv.templates
166-
let serverRootDir
167-
168-
if (multiUser) {
169-
serverRootDir = path.join(rootDir, argv.host.hostname)
170-
} else {
171-
serverRootDir = rootDir
172-
}
173-
174-
let defaultIndexPage = path.join(templates.server, 'index.html')
175-
let existingIndexPage = path.join(serverRootDir, 'index.html')
176-
let defaultIndexPageAcl = path.join(templates.server, 'index.html.acl')
177-
let existingIndexPageAcl = path.join(serverRootDir, 'index.html.acl')
178-
179-
if (!fs.existsSync(existingIndexPage)) {
180-
fs.mkdirp(serverRootDir)
181-
fs.copySync(defaultIndexPage, existingIndexPage)
182-
fs.copySync(defaultIndexPageAcl, existingIndexPageAcl)
183-
}
184-
}
185-
186-
/**
187-
* Ensures that a directory has been copied / initialized. Used to ensure that
188-
* account templates, email templates and default apps have been copied from
189-
* their defaults to the customizable config directory, at server startup.
190-
*
191-
* @param fromDir {string} Path to copy from (defaults)
192-
*
193-
* @param toDir {string} Path to copy to (customizable config)
194-
*
195-
* @return {string} Returns the absolute path for `toDir`
196-
*/
197-
function ensureDirCopy (fromDir, toDir) {
198-
fromDir = path.resolve(fromDir)
199-
toDir = path.resolve(toDir)
200-
201-
if (!fs.existsSync(toDir)) {
202-
fs.copySync(fromDir, toDir)
203-
}
204-
205-
return toDir
206-
}
207-
208116
/**
209117
* Initializes `app.locals` parameters for downstream use (typically by route
210118
* handlers).

lib/server-config.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
'use strict'
2+
/**
3+
* Server config initialization utilities
4+
*/
5+
6+
const fs = require('fs-extra')
7+
const path = require('path')
8+
9+
/**
10+
* Ensures that a directory has been copied / initialized. Used to ensure that
11+
* account templates, email templates and default apps have been copied from
12+
* their defaults to the customizable config directory, at server startup.
13+
*
14+
* @param fromDir {string} Path to copy from (defaults)
15+
*
16+
* @param toDir {string} Path to copy to (customizable config)
17+
*
18+
* @return {string} Returns the absolute path for `toDir`
19+
*/
20+
function ensureDirCopyExists (fromDir, toDir) {
21+
fromDir = path.resolve(fromDir)
22+
toDir = path.resolve(toDir)
23+
24+
if (!fs.existsSync(toDir)) {
25+
fs.copySync(fromDir, toDir)
26+
}
27+
28+
return toDir
29+
}
30+
31+
/**
32+
* Creates (copies from the server templates dir) a Welcome index page for the
33+
* server root web directory, if one does not already exist. This page
34+
* typically has links to account signup and login, and can be overridden by
35+
* the server operator.
36+
*
37+
* @param argv {Function} Express.js app object
38+
*/
39+
function ensureWelcomePage (argv) {
40+
let multiUser = argv.idp
41+
let rootDir = path.resolve(argv.root)
42+
let templates = argv.templates
43+
let serverRootDir
44+
45+
if (multiUser) {
46+
serverRootDir = path.join(rootDir, argv.host.hostname)
47+
} else {
48+
serverRootDir = rootDir
49+
}
50+
51+
let defaultIndexPage = path.join(templates.server, 'index.html')
52+
let existingIndexPage = path.join(serverRootDir, 'index.html')
53+
let defaultIndexPageAcl = path.join(templates.server, 'index.html.acl')
54+
let existingIndexPageAcl = path.join(serverRootDir, 'index.html.acl')
55+
56+
if (!fs.existsSync(existingIndexPage)) {
57+
fs.mkdirp(serverRootDir)
58+
fs.copySync(defaultIndexPage, existingIndexPage)
59+
fs.copySync(defaultIndexPageAcl, existingIndexPageAcl)
60+
}
61+
}
62+
63+
/**
64+
* Ensures that the server config directory (something like '/etc/solid-server'
65+
* or './config', taken from the `configPath` config.json file) exists, and
66+
* creates it if not.
67+
*
68+
* @param argv
69+
*
70+
* @return {string} Path to the server config dir
71+
*/
72+
function initConfigDir (argv) {
73+
let configPath = path.resolve(argv.configPath)
74+
fs.mkdirp(configPath)
75+
76+
return configPath
77+
}
78+
79+
/**
80+
* Ensures that the customizable 'views' folder exists for this installation
81+
* (copies it from default views if not).
82+
*
83+
* @param configPath {string} Location of configuration directory (from the
84+
* local config.json file or passed in as cli parameter)
85+
*
86+
* @return {string} Path to the views dir
87+
*/
88+
function initDefaultViews (configPath) {
89+
let defaultViewsPath = path.resolve('./default-views')
90+
let viewsPath = path.join(configPath, 'views')
91+
92+
ensureDirCopyExists(defaultViewsPath, viewsPath)
93+
94+
return viewsPath
95+
}
96+
97+
/**
98+
* Makes sure that the various template directories (email templates, new
99+
* account templates, etc) have been copied from the default directories to
100+
* this server's own config directory.
101+
*
102+
* @param configPath {string} Location of configuration directory (from the
103+
* local config.json file or passed in as cli parameter)
104+
*
105+
* @return {Object} Returns a hashmap of template directories by type
106+
* (new account, email, server)
107+
*/
108+
function initTemplateDirs (configPath) {
109+
let accountTemplatePath = ensureDirCopyExists(
110+
'./default-templates/new-account',
111+
path.join(configPath, 'templates', 'new-account')
112+
)
113+
114+
let emailTemplatesPath = ensureDirCopyExists(
115+
'./default-templates/emails',
116+
path.join(configPath, 'templates', 'emails')
117+
)
118+
119+
let serverTemplatePath = ensureDirCopyExists(
120+
'./default-templates/server',
121+
path.join(configPath, 'templates', 'server')
122+
)
123+
124+
return {
125+
account: accountTemplatePath,
126+
email: emailTemplatesPath,
127+
server: serverTemplatePath
128+
}
129+
}
130+
131+
module.exports = {
132+
ensureDirCopyExists,
133+
ensureWelcomePage,
134+
initConfigDir,
135+
initDefaultViews,
136+
initTemplateDirs
137+
}

0 commit comments

Comments
 (0)