From 7f8b3f1db075a3f035e9e9446cbc33cc7aeab9a3 Mon Sep 17 00:00:00 2001 From: Jonnie Spratley Date: Thu, 18 Dec 2014 19:58:52 -0800 Subject: [PATCH] updated server --- config/config.json | 5 +++ routes/cms-proxy.js | 74 ++++++++++++++++++++++++++++++++++ routes/cms-routes.js | 67 ------------------------------- routes/cms-server.js | 3 ++ server.js | 95 +++----------------------------------------- 5 files changed, 88 insertions(+), 156 deletions(-) delete mode 100644 routes/cms-routes.js diff --git a/config/config.json b/config/config.json index 715f5e0..2f4153a 100644 --- a/config/config.json +++ b/config/config.json @@ -13,6 +13,11 @@ "security":{ "salt":"angular-cms" }, + "https":{ + "key": "./config/apache.key", + "cert":"./config/apache.crt" + }, + "db":{ "name":"angular-cms", "username":"amadmin", diff --git a/routes/cms-proxy.js b/routes/cms-proxy.js index 4053545..26fee55 100644 --- a/routes/cms-proxy.js +++ b/routes/cms-proxy.js @@ -1,3 +1,77 @@ +var fs = require('fs'), + util = require('util'), + httpProxy = require('http-proxy'); + + module.exports = function (config, app) { console.warn( 'cms-proxy', 'initialized' ); + + /** + * @TODO - HTTPS Key and Cert + * + * This is the location of your https cert and key. + */ + var httpsKey = fs.readFileSync(config.https.key).toString(); + var httpsCert = fs.readFileSync(config.https.cert).toString(); + + + /** + * @TODO - Proxy Options + * This object holds options used for creating a proxy server. + */ + var options = { + port: null, + host: { + hostname: 'localhost', + port: 8181 + }, + proxy: { + hostname: 'localhost', + port: 5001 + }, + api: { + hostname: 'localhost', + port: 5151 + }, + key: httpsKey, + cert: httpsCert, + hostncmsOnly: true, + router: {} + }; + + //Create proxy server and proxy requests + var proxyServer = httpProxy.createServer(options, function(req, res, proxy) { + + console.log('Proxy server started on port: ' + options.host.port); + + // console.log('proxyServer', options); + if (req.url.match(/^\/api\//)) { + proxy.proxyRequest(req, res, { + host: '127.0.0.1', + port: options.api.port + }); + util.log('Routing request: API server'.warn); + } + else if (req.url.match(/^\/1\//)) { + + /* Default express server */ + proxy.proxyRequest(req, res, { + host: 'api.parse.com' + }); + util.log('Routing request: Parse Server'.warn); + + } else { + + /* Default express server */ + proxy.proxyRequest(req, res, { + host: '127.0.0.1', + port: options.api.port + }); + util.log('Routing request: App Server'.warn); + } + }); + + + //Start the proxy server + proxyServer.listen(config.proxy.port); }; diff --git a/routes/cms-routes.js b/routes/cms-routes.js deleted file mode 100644 index 6c17890..0000000 --- a/routes/cms-routes.js +++ /dev/null @@ -1,67 +0,0 @@ -var mongoose = require('mongoose'), - http = require('http'), - util = require('util'); - -module.exports.mount = function(config, app) { - 'use strict'; - - try { - //Connect to database - mongoose.connect(config.db.url); - } - catch (err) { - throw new Error('Unable to connect to MongoDB at ' + config.db.url); - } - finally { - process.exit(); - } - - - - // CONNECTION EVENTS - // When successfully connected - mongoose.connection.on('connected', function() { - console.log('Mongoose default connection open to ', config.db); - }); - - // If the connection throws an error - mongoose.connection.on('error', function(err) { - console.log('Mongoose default connection error: ' + err); - }); - - // When the connection is disconnected - mongoose.connection.on('disconnected', function() { - console.log('Mongoose default connection disconnected'); - }); - - // If the Node process ends, close the Mongoose connection - process.on('SIGINT', function() { - mongoose.connection.close(function() { - console.log('Mongoose default connection disconnected through app termination'); - process.exit(0); - }); - }); - - - - var server = http.createServer(app); - - require('./cms-auth')(config, app); - require('./cms-passport')(config, app); - require('./cms-rest')(config, app); - require('./cms-proxy')(config, app); - require('./cms-upload')(config, app); - require('./cms-sockets')(config, server); - require('./cms-server')(config, app); - - - var serverPort = process.env.PORT || config.port; - var serverHost = process.env.IP || config.host; - - return server.listen(serverPort, serverHost, function() { - util.log('App listening on port: ' + config.port + ''.verbose); - util.log(util.inspect(config, { - colors: true - })); - }); -}; diff --git a/routes/cms-server.js b/routes/cms-server.js index 41e8c1d..9e8cef7 100644 --- a/routes/cms-server.js +++ b/routes/cms-server.js @@ -2,6 +2,7 @@ var express = require('express'), path = require('path'), bodyParser = require( 'body-parser' ); + module.exports = function (config, app) { console.warn( 'cms-server initialized'); @@ -19,6 +20,8 @@ module.exports = function (config, app) { } }; + + router.use(express.static(config.staticDir, options)); app.all('/', function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); diff --git a/server.js b/server.js index 1caf1b5..67ea21f 100755 --- a/server.js +++ b/server.js @@ -2,93 +2,10 @@ * Server - This is the Node.js Server. * @object */ -var fs = require('fs'), - util = require('util'), - http = require('http'), - express = require('express'), - httpProxy = require('http-proxy'), - app = express(); - - -/** - * @TODO - Externalize configuration for server and proxy, mongodb - */ +var express = require('express'); +var fs = require('fs'); +var cmsRouter = require('./routes/cms-router.js'); +var app = express(); var config = JSON.parse(fs.readFileSync('./config/config.json')); - - -/** - * @TODO - HTTPS Key and Cert - * - * This is the location of your https cert and key. - */ -var httpsKey = fs.readFileSync('./config/apache.key').toString(); -var httpsCert = fs.readFileSync('./config/apache.crt').toString(); - -/** - * @TODO - Proxy Options - * This object holds options used for creating a proxy server. - */ -var options = { - port: null, - host: { - hostname: 'localhost', - port: 8181 - }, - proxy: { - hostname: 'localhost', - port: 5001 - }, - api: { - hostname: 'localhost', - port: 5151 - }, - key: httpsKey, - cert: httpsCert, - hostncmsOnly: true, - router: {} -}; - - - - -var cmsRoutes = require('./routes/cms-routes'); - cmsRoutes.mount(config, app); - - - - -//Create proxy server and proxy requests -var proxyServer = httpProxy.createServer(options, function(req, res, proxy) { - - console.log('Proxy server started on port: ' + options.host.port); - - // console.log('proxyServer', options); - if (req.url.match(/^\/api\//)) { - proxy.proxyRequest(req, res, { - host: '127.0.0.1', - port: options.api.port - }); - util.log('Routing request: API server'.warn); - } - else if (req.url.match(/^\/1\//)) { - - /* Default express server */ - proxy.proxyRequest(req, res, { - host: 'api.parse.com' - }); - util.log('Routing request: Parse Server'.warn); - - } else { - - /* Default express server */ - proxy.proxyRequest(req, res, { - host: '127.0.0.1', - port: options.api.port - }); - util.log('Routing request: App Server'.warn); - } -}); - - -//Start the proxy server -proxyServer.listen(process.env.PORT, process.env.IP); +var server = new cmsRouter.mount(config, app); +console.log(server);