@@ -11,15 +11,14 @@ const proxy = require('./handlers/proxy')
1111const SolidHost = require ( './models/solid-host' )
1212const AccountManager = require ( './models/account-manager' )
1313const vhost = require ( 'vhost' )
14- const fs = require ( 'fs-extra' )
15- const path = require ( 'path' )
1614const EmailService = require ( './models/email-service' )
1715const AccountRecovery = require ( './account-recovery' )
1816const capabilityDiscovery = require ( './capability-discovery' )
1917const bodyParser = require ( 'body-parser' ) . urlencoded ( { extended : false } )
2018const API = require ( './api' )
2119const errorPages = require ( './handlers/error-pages' )
2220const OidcManager = require ( './models/oidc-manager' )
21+ const config = require ( './server-config' )
2322const defaults = require ( '../config/defaults' )
2423const 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).
0 commit comments