Skip to content

Commit b21ae11

Browse files
Refactor createApp()
1 parent d47d160 commit b21ae11

File tree

3 files changed

+84
-61
lines changed

3 files changed

+84
-61
lines changed

lib/create-app.js

Lines changed: 81 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,19 @@ function createApp (argv = {}) {
4343
argv.templates = config.initTemplateDirs(configPath)
4444

4545
const ldp = new LDP(argv)
46+
4647
const app = express()
4748

4849
initAppLocals(app, argv, ldp)
4950
initHeaders(app)
51+
initViews(app, configPath)
5052

5153
// Serve the public 'common' directory (for shared CSS files, etc)
5254
app.use('/common', express.static('common'))
5355

54-
const viewsPath = config.initDefaultViews(configPath)
55-
app.set('views', viewsPath)
56-
app.engine('.hbs', handlebars({ extname: '.hbs' }))
57-
app.set('view engine', '.hbs')
58-
59-
// Use session cookies
60-
const useSecureCookies = argv.webid // argv.webid forces https and secure cookies
61-
app.use(session(sessionSettings(useSecureCookies, argv.host)))
62-
6356
// Adding proxy
6457
if (argv.proxy) {
65-
proxy(app, ldp.proxy)
58+
proxy(app, argv.proxy)
6659
}
6760

6861
// Options handler
@@ -73,36 +66,7 @@ function createApp (argv = {}) {
7366
}
7467

7568
if (argv.webid) {
76-
config.ensureWelcomePage(argv)
77-
78-
var accountRecovery = AccountRecovery({ redirect: '/' })
79-
// adds GET /api/accounts/recover
80-
// adds POST /api/accounts/recover
81-
// adds GET /api/accounts/validateToken
82-
app.use('/api/accounts/', accountRecovery)
83-
84-
let accountManager = AccountManager.from({
85-
authMethod: argv.auth,
86-
emailService: app.locals.emailService,
87-
host: argv.host,
88-
accountTemplatePath: argv.templates.account,
89-
store: ldp,
90-
multiUser: argv.idp
91-
})
92-
app.locals.accountManager = accountManager
93-
94-
// Account Management API (create account, new cert)
95-
app.use('/', API.accounts.middleware(accountManager))
96-
97-
// Set up authentication-related API endpoints and app.locals
98-
initAuthentication(argv, app)
99-
100-
// Messaging API
101-
app.post('/api/messages', bodyParser, API.messages.send())
102-
}
103-
104-
if (argv.idp) {
105-
app.use(vhost('*', LdpMiddleware(corsSettings)))
69+
initWebId(argv, app, ldp)
10670
}
10771

10872
app.use('/', LdpMiddleware(corsSettings))
@@ -132,6 +96,83 @@ function initAppLocals (app, argv, ldp) {
13296
}
13397
}
13498

99+
/**
100+
* Sets up headers common to all Solid requests (CORS-related, Allow, etc).
101+
*
102+
* @param app {Function} Express.js app instance
103+
*/
104+
function initHeaders (app) {
105+
app.use(corsSettings)
106+
107+
app.use((req, res, next) => {
108+
// Set X-Powered-By
109+
res.set('X-Powered-By', 'solid-server')
110+
// Set default Allow methods
111+
res.set('Allow', 'OPTIONS, HEAD, GET, PATCH, POST, PUT, DELETE')
112+
next()
113+
})
114+
115+
app.use('/', capabilityDiscovery())
116+
}
117+
118+
/**
119+
* Sets up the express rendering engine and views directory.
120+
*
121+
* @param app {Function} Express.js app
122+
* @param configPath {string}
123+
*/
124+
function initViews (app, configPath) {
125+
const viewsPath = config.initDefaultViews(configPath)
126+
127+
app.set('views', viewsPath)
128+
app.engine('.hbs', handlebars({ extname: '.hbs' }))
129+
app.set('view engine', '.hbs')
130+
}
131+
132+
/**
133+
* Sets up WebID-related functionality (account creation and authentication)
134+
*
135+
* @param argv {Object}
136+
* @param app {Function}
137+
* @param ldp {LDP}
138+
*/
139+
function initWebId (argv, app, ldp) {
140+
config.ensureWelcomePage(argv)
141+
142+
// Use session cookies
143+
const useSecureCookies = argv.webid // argv.webid forces https and secure cookies
144+
app.use(session(sessionSettings(useSecureCookies, argv.host)))
145+
146+
var accountRecovery = AccountRecovery({ redirect: '/' })
147+
// adds GET /api/accounts/recover
148+
// adds POST /api/accounts/recover
149+
// adds GET /api/accounts/validateToken
150+
app.use('/api/accounts/', accountRecovery)
151+
152+
let accountManager = AccountManager.from({
153+
authMethod: argv.auth,
154+
emailService: app.locals.emailService,
155+
host: argv.host,
156+
accountTemplatePath: argv.templates.account,
157+
store: ldp,
158+
multiUser: argv.idp
159+
})
160+
app.locals.accountManager = accountManager
161+
162+
// Account Management API (create account, new cert)
163+
app.use('/', API.accounts.middleware(accountManager))
164+
165+
// Set up authentication-related API endpoints and app.locals
166+
initAuthentication(argv, app)
167+
168+
if (argv.idp) {
169+
app.use(vhost('*', LdpMiddleware(corsSettings)))
170+
}
171+
172+
// Messaging API
173+
app.post('/api/messages', bodyParser, API.messages.send())
174+
}
175+
135176
/**
136177
* Sets up authentication-related routes and handlers for the app.
137178
*
@@ -165,25 +206,6 @@ function initAuthentication (argv, app) {
165206
}
166207
}
167208

168-
/**
169-
* Sets up headers common to all Solid requests (CORS-related, Allow, etc).
170-
*
171-
* @param app {Function} Express.js app instance
172-
*/
173-
function initHeaders (app) {
174-
app.use(corsSettings)
175-
176-
app.use((req, res, next) => {
177-
// Set X-Powered-By
178-
res.set('X-Powered-By', 'solid-server')
179-
// Set default Allow methods
180-
res.set('Allow', 'OPTIONS, HEAD, GET, PATCH, POST, PUT, DELETE')
181-
next()
182-
})
183-
184-
app.use('/', capabilityDiscovery())
185-
}
186-
187209
/**
188210
* Returns a settings object for Express.js sessions.
189211
*

test/integration/capability-discovery.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ describe('API', () => {
7676
apps: {
7777
'signin': '/signin/',
7878
'signup': '/signup/'
79-
}
79+
},
80+
webid: false
8081
}
8182
const solid = Solid(config)
8283
let server = supertest(solid)

test/integration/params.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('LDNODE params', function () {
2323
describe('suffixAcl', function () {
2424
describe('not passed', function () {
2525
it('should fallback on .acl', function () {
26-
var ldp = ldnode()
26+
var ldp = ldnode({ webid: false })
2727
assert.equal(ldp.locals.ldp.suffixAcl, '.acl')
2828
})
2929
})

0 commit comments

Comments
 (0)