Skip to content

Commit

Permalink
refactored silex startup script to make it possible to add custom uni…
Browse files Browse the repository at this point in the history
…file services
  • Loading branch information
lexoyo committed Sep 24, 2018
1 parent cb6928e commit 32b40fe
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .pm2.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions dist/server/BackwardCompat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 19 additions & 21 deletions dist/server/CloudExplorerRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 61 additions & 0 deletions dist/server/DefaultConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//////////////////////////////////////////////////
// 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,
}

const exported = function() {};
exported.prototype = {
ceOptions: ceOptions,
serverOptions: serverOptions,
publisherOptions: publisherOptions,
electronOptions: electronOptions,
sslOptions: sslOptions,
};
module.exports = exported;

12 changes: 1 addition & 11 deletions dist/server/PublishRouter.js
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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'
Expand Down
77 changes: 77 additions & 0 deletions dist/server/SilexServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//////////////////////////////////////////////////
// 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/
//////////////////////////////////////////////////

// node modules
const Path = require('path');
const fs = require('fs');
const express = require('express');
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 PublishRouter = require('./PublishRouter.js');
const SslRouter = require('./SslRouter.js');
const bodyParser = require('body-parser');

module.exports = function({
serverOptions,
publisherOptions,
ceOptions,
electronOptions,
sslOptions,
}) {
this.app = express();

// compress gzip when possible
this.app.use(compression());

// 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
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);
this.unifile = this.ceRouter.unifile; // for access by third party

this.app.use(serverOptions.cePath, this.ceRouter);
this.app.use(this.websiteRouter);
this.app.use(this.publishRouter);
this.app.use(this.sslRouter);

// 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')));

// 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);
});
};

19 changes: 9 additions & 10 deletions dist/server/SslRouter.js
Original file line number Diff line number Diff line change
@@ -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);
}
Expand All @@ -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,
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion dist/server/WebsiteRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
61 changes: 3 additions & 58 deletions dist/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,8 @@
// http://www.silexlabs.org/silex/silex-licensing/
//////////////////////////////////////////////////

'use strict';

// node modules
const Path = require('path');
const fs = require('fs');
const express = require('express');
const compression = require('compression');
const cookieParser = require('cookie-parser');
const session = require('cookie-session');
const serveStatic = require('serve-static');
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();

// compress gzip when possible
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'
}));

// 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));

// 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')));

// Start Silex as an Electron app
if(process.env.SILEX_ELECTRON) {
require(Path.join(__dirname, 'silex_electron'));
module.exports = {
SilexServer: require('./SilexServer'),
DefaultConfig: require('./DefaultConfig'),
}

// server 'loop'
app.listen(port, function() {
console.log('Listening on ' + port);
});


Loading

0 comments on commit 32b40fe

Please sign in to comment.