diff --git a/.pm2.json b/.pm2.json index a0c7b0552..df78bf468 100644 --- a/.pm2.json +++ b/.pm2.json @@ -1,7 +1,7 @@ { "apps" : [{ "name" : "Silex", - "script" : "dist/server/index.js", + "script" : "dist/server/silex_web.js", "watch" : "dist/server/", "env": { "NODE_ENV": "production", diff --git a/dist/server/BackwardCompat.js b/dist/server/BackwardCompat.js index 3448eafdc..d94efbb82 100644 --- a/dist/server/BackwardCompat.js +++ b/dist/server/BackwardCompat.js @@ -17,8 +17,8 @@ const constants = require('./Constants.json'); * if you start the server with NPM, it sets an env variable for you otherwise we get it from package.json * used for backward compat and for the static files URLs taken from //{{host}}/static/{{Y-Z}} */ -const LATEST_VERSION = (process.env.npm_package_version || require(Path.resolve(__dirname, '../../package.json')).version).split('.'); -console.log('Silex starts with version', LATEST_VERSION); +const LATEST_VERSION = (process.env.npm_package_version || require(Path.resolve(__dirname, '../../package.json'))['version:frontend']).split('.'); +console.log('Silex starts with front end version', LATEST_VERSION); /** * @fileoverview Handle backward compatibility when a user opens a site for edition diff --git a/dist/server/CloudExplorerRouter.js b/dist/server/CloudExplorerRouter.js index cec38abce..012aa5455 100644 --- a/dist/server/CloudExplorerRouter.js +++ b/dist/server/CloudExplorerRouter.js @@ -2,65 +2,63 @@ const Os = require('os'); const fs = require('fs'); const CloudExplorer = require('cloud-explorer'); -module.exports = function(rootUrl) { +module.exports = function(ceOptions) { - // ** - // unifile routes const routerOptions = {}; // FTP service console.log('FTP service: looking for env vars ENABLE_FTP'); - if(process.env.ENABLE_FTP) { + if(ceOptions.enableFtp) { console.log('FTP service: found'); routerOptions.ftp = { - redirectUri: rootUrl + '/ftp/signin', + redirectUri: ceOptions.rootUrl + '/ftp/signin', }; } // SFTP service console.log('SFTP service: looking for env vars ENABLE_SFTP'); - if(process.env.ENABLE_SFTP) { + if(ceOptions.enableSftp) { console.log('SFTP service: found'); routerOptions.sftp = { - redirectUri: rootUrl + '/sftp/signin', + redirectUri: ceOptions.rootUrl + '/sftp/signin', }; } // Webdav service console.log('Webdav service: looking for env vars ENABLE_WEBDAV'); - if(process.env.ENABLE_WEBDAV) { + if(ceOptions.enableWebdav) { console.log('Webdav service: found'); routerOptions.webdav = { - redirectUri: rootUrl + '/webdav/signin', + redirectUri: ceOptions.rootUrl + '/webdav/signin', }; } // Github service console.log('Github service: looking for env vars GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET'); - if(process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) { - console.log('Github service: found app', process.env.GITHUB_CLIENT_ID); + if(ceOptions.githubClientId && ceOptions.githubClientSecret) { + console.log('Github service: found app', ceOptions.githubClientId); routerOptions.github = { - clientId: process.env.GITHUB_CLIENT_ID, - clientSecret: process.env.GITHUB_CLIENT_SECRET, - redirectUri: rootUrl + '/github/oauth_callback', + clientId: ceOptions.githubClientId, + clientSecret: ceOptions.githubClientSecret, + redirectUri: ceOptions.rootUrl + '/github/oauth_callback', }; } // Dropbox service console.log('Dropbox service: looking for env vars DROPBOX_CLIENT_ID and DROPBOX_CLIENT_SECRET'); - if(process.env.DROPBOX_CLIENT_ID && process.env.DROPBOX_CLIENT_SECRET) { - console.log('Dropbox service: found app', process.env.DROPBOX_CLIENT_ID); + if(ceOptions.dropboxClientId && ceOptions.dropboxClientSecret) { + console.log('Dropbox service: found app', ceOptions.dropboxClientId); routerOptions.dropbox = { - clientId: process.env.DROPBOX_CLIENT_ID, - clientSecret: process.env.DROPBOX_CLIENT_SECRET, - redirectUri: rootUrl + '/dropbox/oauth_callback', + clientId: ceOptions.dropboxClientId, + clientSecret: ceOptions.dropboxClientSecret, + redirectUri: ceOptions.rootUrl + '/dropbox/oauth_callback', }; } // Local file system service console.log('Local file system service: looking for env vars SILEX_ELECTRON or SILEX_DEBUG or ENABLE_FS'); - if(process.env.SILEX_DEBUG || process.env.SILEX_ELECTRON || process.env.ENABLE_FS) { - const fsRoot = process.env.FS_ROOT || Os.homedir(); + if(ceOptions.enableFs) { + const fsRoot = ceOptions.fsRoot || Os.homedir(); console.info('Local file system service: ENABLED => local file system is writable, use FS_ROOT as root (', fsRoot, ')'); routerOptions.fs = { showHiddenFile: false, diff --git a/dist/server/EnvVarsConfig.js b/dist/server/EnvVarsConfig.js new file mode 100644 index 000000000..6350b4549 --- /dev/null +++ b/dist/server/EnvVarsConfig.js @@ -0,0 +1,58 @@ +////////////////////////////////////////////////// +// Silex, live web creation +// http://projects.silexlabs.org/?/silex/ +// +// Copyright (c) 2012 Silex Labs +// http://www.silexlabs.org/ +// +// Silex is available under the GPL license +// http://www.silexlabs.org/silex/silex-licensing/ +////////////////////////////////////////////////// + +// server options +const serverOptions = {}; +serverOptions.port = process.env.PORT || 6805; // 6805 is the date of sexual revolution started in paris france 8-) +serverOptions.rootUrl = process.env.SERVER_URL || `http://localhost:${serverOptions.port}`; +serverOptions.sessionSecret = process.env.SILEX_SESSION_SECRET || 'test session secret'; +serverOptions.cePath = '/ce'; + +// electron app +const electronOptions = { + enabled: process.env.SILEX_ELECTRON || false, +} + +// SSL options +const sslOptions = { + forceHttps: process.env.SILEX_FORCE_HTTPS, + trustXFPHeader: process.env.SILEX_FORCE_HTTPS_TRUST_XFP_HEADER, + privateKey: process.env.SILEX_SSL_PRIVATE_KEY, + certificate: process.env.SILEX_SSL_CERTIFICATE, + sslPort: process.env.SSL_PORT || 443, +}; + +// cloud explorer options +const ceOptions = { + enableFtp: process.env.ENABLE_FTP, + enableSftp: process.env.ENABLE_SFTP, + enableWebdav: process.env.ENABLE_WEBDAV, + githubClientId: process.env.GITHUB_CLIENT_ID, + githubClientSecret: process.env.GITHUB_CLIENT_SECRET, + dropboxClientId: process.env.DROPBOX_CLIENT_ID, + dropboxClientSecret: process.env.DROPBOX_CLIENT_SECRET, + enableFs: process.env.SILEX_DEBUG || process.env.SILEX_ELECTRON || process.env.ENABLE_FS, + fsRoot: process.env.FS_ROOT, + rootUrl: serverOptions.rootUrl, +}; + +const publisherOptions = { + rootUrl: serverOptions.rootUrl, + port: serverOptions.port, +} + +module.exports = { + ceOptions: ceOptions, + serverOptions: serverOptions, + publisherOptions: publisherOptions, + electronOptions: electronOptions, + sslOptions: sslOptions, +}; diff --git a/dist/server/PublishRouter.js b/dist/server/PublishRouter.js index 6cf2ea167..1b43ea925 100644 --- a/dist/server/PublishRouter.js +++ b/dist/server/PublishRouter.js @@ -1,7 +1,7 @@ const PublishJob = require('./PublishJob.js'); const express = require('express'); -module.exports = function(port, rootUrl, unifile) { +module.exports = function({ port, rootUrl }, unifile) { const router = express.Router(); @@ -37,16 +37,6 @@ module.exports = function(port, rootUrl, unifile) { }); } break; - case process.env.RESTART_ROUTE || 'reload': - if(!process.env.RESTART_ROUTE) { - res.status(500).send({ - message: 'You need to define an env var RESTART_ROUTE and call /{{RESTART_ROUTE}}' - }); - return; - } - res.send(); - process.send('restart'); - break; default: res.status(500).send({ message: 'Silex task "' + req.params.task + '" does not exist' diff --git a/dist/server/SslRouter.js b/dist/server/SslRouter.js index 4fe59bb83..8696e7f31 100644 --- a/dist/server/SslRouter.js +++ b/dist/server/SslRouter.js @@ -1,16 +1,16 @@ const fs = require('fs'); const express = require('express'); -module.exports = function(app) { +module.exports = function(sslOptions, app) { const router = express.Router(); // SSL // force ssl if the env var SILEX_FORCE_HTTPS is set - if(process.env.SILEX_FORCE_HTTPS) { + if(sslOptions.forceHttps) { console.log('force SSL is active (env var SILEX_FORCE_HTTPS is set)'); const forceSSL = require('express-force-ssl'); app.set('forceSSLOptions', { - trustXFPHeader: !!process.env.SILEX_FORCE_HTTPS_TRUST_XFP_HEADER + trustXFPHeader: !!sslOptions.trustXFPHeader, }); router.use(forceSSL); } @@ -20,11 +20,11 @@ module.exports = function(app) { // SSL certificate console.log('SSL: looking for env vars SILEX_SSL_CERTIFICATE and SILEX_SSL_PRIVATE_KEY'); - if(process.env.SILEX_SSL_PRIVATE_KEY && process.env.SILEX_SSL_CERTIFICATE) { - console.log('SSL: found certificate', process.env.SILEX_SSL_CERTIFICATE); + if(sslOptions.privateKey && sslOptions.certificate) { + console.log('SSL: found certificate', sslOptions.certificate); try { - var privateKey = fs.readFileSync(process.env.SILEX_SSL_PRIVATE_KEY).toString(); - var certificate = fs.readFileSync(process.env.SILEX_SSL_CERTIFICATE).toString(); + var privateKey = fs.readFileSync(sslOptions.privateKey).toString(); + var certificate = fs.readFileSync(sslOptions.certificate).toString(); var options = { key: privateKey, @@ -33,9 +33,8 @@ module.exports = function(app) { rejectUnauthorized: false }; - var sslPort = process.env.SSL_PORT || 443; - https.createServer(options, this).listen(sslPort, function() { - console.log('SSL: listening on port ', sslPort); + https.createServer(options, this).listen(sslOptions.sslPort, function() { + console.log('SSL: listening on port ', sslOptions.sslPort); }); } catch(e) { diff --git a/dist/server/WebsiteRouter.js b/dist/server/WebsiteRouter.js index 050790e4f..06f5c5470 100644 --- a/dist/server/WebsiteRouter.js +++ b/dist/server/WebsiteRouter.js @@ -22,7 +22,7 @@ const DomTools = require('./DomTools.js'); const clientRoot = Path.resolve(__dirname, '..'); const constants = require('./Constants.json'); -module.exports = function(port, rootUrl, unifile) { +module.exports = function({ port, rootUrl }, unifile) { const backwardCompat = new BackwardCompat(rootUrl); diff --git a/dist/server/index.js b/dist/server/index.js index 9bde150da..c380b0d8c 100644 --- a/dist/server/index.js +++ b/dist/server/index.js @@ -9,8 +9,6 @@ // http://www.silexlabs.org/silex/silex-licensing/ ////////////////////////////////////////////////// -'use strict'; - // node modules const Path = require('path'); const fs = require('fs'); @@ -19,53 +17,60 @@ const compression = require('compression'); const cookieParser = require('cookie-parser'); const session = require('cookie-session'); const serveStatic = require('serve-static'); +const CloudExplorerRouter = require('./CloudExplorerRouter'); const WebsiteRouter = require('./WebsiteRouter.js'); -const CloudExplorerRouter = require('./CloudExplorerRouter.js'); const PublishRouter = require('./PublishRouter.js'); const SslRouter = require('./SslRouter.js'); const bodyParser = require('body-parser'); -// 6805 is the date of sexual revolution started in paris france 8-) -const port = process.env.PORT || 6805; -const rootUrl = process.env.SERVER_URL || `http://localhost:${port}`; - -const app = express(); +module.exports = function({ + serverOptions, + publisherOptions, + ceOptions, + electronOptions, + sslOptions, +}) { + this.app = express(); -// compress gzip when possible -app.use(compression()); + // compress gzip when possible + this.app.use(compression()); -// cookie & session -app.use(bodyParser.json({limit: '1mb'})); -app.use(bodyParser.text({limit: '1mb'})); -app.use(cookieParser()); -app.use(session({ - name: 'silex-session', - secret: process.env.SILEX_SESSION_SECRET || 'test session secret' -})); + // cookie & session + this.app.use(bodyParser.json({limit: '1mb'})); + this.app.use(bodyParser.text({limit: '1mb'})); + this.app.use(cookieParser()); + this.app.use(session({ + name: 'silex-session', + secret: serverOptions.sessionSecret, + })); -// create the routes for unifile/CloudExplorer -// and for Silex tasks -const cloudExplorerRouter = new CloudExplorerRouter(rootUrl + '/ce'); -app.use('/ce', cloudExplorerRouter); -app.use(new WebsiteRouter(port, rootUrl, cloudExplorerRouter.unifile)); -app.use(new PublishRouter(port, rootUrl, cloudExplorerRouter.unifile)); -app.use(new SslRouter(app)); + // create the routes for unifile/CloudExplorer + // and for Silex tasks + this.ceRouter = new CloudExplorerRouter(ceOptions); + this.websiteRouter = new WebsiteRouter(serverOptions, this.ceRouter.unifile); + this.publishRouter = new PublishRouter(publisherOptions, this.ceRouter.unifile); + this.sslRouter = new SslRouter(sslOptions, this.app); -// add static folders to serve silex files -app.use('/', serveStatic(Path.join(__dirname, '../../dist/client'))); -// debug silex, for js source map -app.use('/js/src', serveStatic(Path.join(__dirname, '../../src'))); -// the scripts which have to be available in all versions (v2.1, v2.2, v2.3, ...) -app.use('/static', serveStatic(Path.join(__dirname, '../../static'))); + this.app.use(serverOptions.cePath, this.ceRouter); + this.app.use(this.websiteRouter); + this.app.use(this.publishRouter); + this.app.use(this.sslRouter); -// Start Silex as an Electron app -if(process.env.SILEX_ELECTRON) { - require(Path.join(__dirname, 'silex_electron')); -} + // add static folders to serve silex files + this.app.use('/', serveStatic(Path.join(__dirname, '../../dist/client'))); + // debug silex, for js source map + this.app.use('/js/src', serveStatic(Path.join(__dirname, '../../src'))); + // the scripts which have to be available in all versions (v2.1, v2.2, v2.3, ...) + this.app.use('/static', serveStatic(Path.join(__dirname, '../../static'))); -// server 'loop' -app.listen(port, function() { - console.log('Listening on ' + port); -}); + // Start Silex as an Electron app + if(electronOptions.enabled) { + require(Path.join(__dirname, 'silex_electron')); + } + // server 'loop' + this.app.listen(serverOptions.port, function() { + console.log('Listening on ' + serverOptions.port); + }); +}; diff --git a/dist/server/silex_web.js b/dist/server/silex_web.js new file mode 100644 index 000000000..220045353 --- /dev/null +++ b/dist/server/silex_web.js @@ -0,0 +1,22 @@ +////////////////////////////////////////////////// +// Silex, live web creation +// http://projects.silexlabs.org/?/silex/ +// +// Copyright (c) 2012 Silex Labs +// http://www.silexlabs.org/ +// +// Silex is available under the GPL license +// http://www.silexlabs.org/silex/silex-licensing/ +////////////////////////////////////////////////// + +'use strict'; + +const SilexServer = require('./index'); +const config = require('./EnvVarsConfig'); + +const silex = new SilexServer(config); +// here you can change routers config, +// e.g. add unifile services `silex.ceRouter.addService(unifileConnector)` +// and use `silex.app.get(...)` to add callbacks +// @see https://github.com/silexlabs/Silex/wiki/Silex-Developer-Guide#add-unifile-services-eg-for-hosting-companies + diff --git a/package.json b/package.json index e569a1195..9a5ed9e4a 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,13 @@ { "name": "silex", "description": "Free and easy website builder for everyone.", - "<-- be careful": "version is used in BackwardCompat.js -->", "version": "2.2.7", - "<-- ": "version is used in BackwardCompat.js -->", + "version:frontend": "2.2.7", "author": "Alex Hoyau (https://lexoyo.me/)", "contributors": [ "Jean-Baptiste Richardet (https://github.com/JbIPS)" ], - "main": "dist/server/index.js", + "main": "dist/server/silex_web.js", "license": "GPL-3.0", "engines": { "node": "8.10.x",