From 95963f7e63086cb961d099f1ce2c28e51edc6a06 Mon Sep 17 00:00:00 2001 From: Jeremy Mitchell Date: Fri, 7 Apr 2017 12:26:14 -0600 Subject: [PATCH 1/3] TO UI experimental no longer uses express node.js server to run in prod mode - only in dev mode. the http server you host it on in prod is up to you. --- traffic_ops/experimental/ui/Gruntfile.js | 13 ++--- .../experimental/ui/app/src/scripts/config.js | 6 ++- .../experimental/ui/conf/config-template.js | 49 ------------------- traffic_ops/experimental/ui/conf/config.js | 5 +- traffic_ops/experimental/ui/grunt/copy.js | 18 ------- .../ui/grunt/install-dependencies.js | 25 ---------- .../experimental/ui/grunt/ngconstant.js | 46 ----------------- traffic_ops/experimental/ui/package.json | 3 -- traffic_ops/experimental/ui/server/server.js | 39 ++++----------- 9 files changed, 19 insertions(+), 185 deletions(-) delete mode 100644 traffic_ops/experimental/ui/conf/config-template.js delete mode 100644 traffic_ops/experimental/ui/grunt/install-dependencies.js delete mode 100644 traffic_ops/experimental/ui/grunt/ngconstant.js diff --git a/traffic_ops/experimental/ui/Gruntfile.js b/traffic_ops/experimental/ui/Gruntfile.js index daeb4a04fb..a058e3204e 100644 --- a/traffic_ops/experimental/ui/Gruntfile.js +++ b/traffic_ops/experimental/ui/Gruntfile.js @@ -20,8 +20,6 @@ 'use strict'; module.exports = function (grunt) { - var os = require("os"); - var globalConfig = require('./grunt/globalConfig'); // load time grunt - helps with optimizing build times require('time-grunt')(grunt); @@ -29,25 +27,23 @@ module.exports = function (grunt) { // load grunt task configurations require('load-grunt-config')(grunt); - // default task - runs in dev mode + // default task - when you type 'grunt' it really runs as 'grunt dev' grunt.registerTask('default', ['dev']); - // dev task - when you type 'grunt dev' <-- builds unminified app and starts express server + // dev task - when you type 'grunt dev' <-- builds unminified app and puts it in in app/dist folder and starts express server which reads server.js grunt.registerTask('dev', [ 'build-dev', 'express:dev', 'watch' ]); - // dist task - when you type 'grunt dist' <-- builds minified app for distribution and generates node dependencies all wrapped up nicely in a /dist folder + // dist task - when you type 'grunt dist' <-- builds minified app for distribution all wrapped up nicely in app/dist folder grunt.registerTask('dist', [ - 'build', - 'install-dependencies' + 'build' ]); // build tasks grunt.registerTask('build', [ - 'ngconstant:prod', 'clean', 'copy:dist', 'build-css', @@ -56,7 +52,6 @@ module.exports = function (grunt) { ]); grunt.registerTask('build-dev', [ - 'ngconstant:dev', 'clean', 'copy:dev', 'build-css-dev', diff --git a/traffic_ops/experimental/ui/app/src/scripts/config.js b/traffic_ops/experimental/ui/app/src/scripts/config.js index 46606c9652..19c2431755 100644 --- a/traffic_ops/experimental/ui/app/src/scripts/config.js +++ b/traffic_ops/experimental/ui/app/src/scripts/config.js @@ -17,10 +17,12 @@ * under the License. */ +// this is the config the TO UI uses + "use strict"; - angular.module('config', []) +angular.module('config', []) -.constant('ENV', {api:{root:'/api/1.2/'}}) +.constant('ENV', { api: { root:'http://localhost:3000/api/1.2/' } }) ; diff --git a/traffic_ops/experimental/ui/conf/config-template.js b/traffic_ops/experimental/ui/conf/config-template.js deleted file mode 100644 index 1a401cc923..0000000000 --- a/traffic_ops/experimental/ui/conf/config-template.js +++ /dev/null @@ -1,49 +0,0 @@ -/* - - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - */ - -// this is the config for /opt/traffic_ops/server/server.js and is consumed when 'sudo service traffic_ops_v2 start' -module.exports = { - timeout: '120s', - useSSL: false, // set to true if you plan to use https (self-signed or trusted certs). - port: 8080, - sslPort: 8443, - proxyPort: 8009, - // if useSSL is true, generate ssl certs and provide the proper locations. - ssl: { - key: '/path/to/ssl.key', - cert: '/path/to/ssl.crt', - ca: [ - '/path/to/ssl-bundle.crt' - ] - }, - // set api 'base_url' to the traffic ops url (all api calls made from the traffic ops ui will be proxied to the api base_url) - // enter value for api 'key' if you want to append ?API_KEY=value to all api calls. It is suggested to leave blank. - api: { - base_url: 'http(s)://where-traffic-ops-is.com', - key: '' - }, - // default files location (this is where the traffic ops html, css and javascript was installed) - files: { - static: '/opt/traffic_ops_v2/public' - }, - // default log location (this is where traffic_ops logs are written) - log: { - stream: '/var/log/traffic_ops_v2/access.log' - }, - reject_unauthorized: 0 // 0 if using self-signed certs, 1 if trusted certs -}; - diff --git a/traffic_ops/experimental/ui/conf/config.js b/traffic_ops/experimental/ui/conf/config.js index 3d5c14021e..9666739c33 100644 --- a/traffic_ops/experimental/ui/conf/config.js +++ b/traffic_ops/experimental/ui/conf/config.js @@ -17,6 +17,7 @@ * under the License. */ +// this is the config that is consumed by server.js when running in dev mode module.exports = { timeout: '120s', useSSL: false, // set to true if using ssl @@ -30,10 +31,6 @@ module.exports = { '/path/to/ssl-bundle.crt' ] }, - api: { - base_url: 'http://localhost:3000', - key: '' - }, files: { static: './app/dist/public/' }, diff --git a/traffic_ops/experimental/ui/grunt/copy.js b/traffic_ops/experimental/ui/grunt/copy.js index 34a0f66c74..0bc3d60952 100644 --- a/traffic_ops/experimental/ui/grunt/copy.js +++ b/traffic_ops/experimental/ui/grunt/copy.js @@ -58,24 +58,6 @@ module.exports = { 'assets/js/**/*' ] }, - { - expand: true, - dot: true, - cwd: '<%= globalConfig.srcserverdir %>', - dest: '<%= globalConfig.distdir %>/server', - src: [ - 'server.js' - ] - }, - { - expand: true, - dot: true, - cwd: '<%= globalConfig.srcdir %>', - dest: '<%= globalConfig.distdir %>', - src: [ - 'package.json' - ] - }, { expand: true, dot: true, diff --git a/traffic_ops/experimental/ui/grunt/install-dependencies.js b/traffic_ops/experimental/ui/grunt/install-dependencies.js deleted file mode 100644 index 14e96ac42c..0000000000 --- a/traffic_ops/experimental/ui/grunt/install-dependencies.js +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -module.exports = { - options: { - cwd: '<%= globalConfig.distdir %>', - isDevelopment: false - } -}; diff --git a/traffic_ops/experimental/ui/grunt/ngconstant.js b/traffic_ops/experimental/ui/grunt/ngconstant.js deleted file mode 100644 index f8f0023274..0000000000 --- a/traffic_ops/experimental/ui/grunt/ngconstant.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -module.exports = { - options: { - space: ' ', - wrap: '"use strict";\n\n {%= __ngModule %}', - name: 'config', - dest: '<%= globalConfig.srcdir %>/scripts/config.js' - }, - dev: { - constants: { - ENV: { - api: { - "root": "/api/1.2/" // api base_url is defined in server.js - } - } - } - }, - prod: { - constants: { - ENV: { - api: { - "root": "/api/1.2/" // api base_url is defined in server.js - } - } - } - } -}; - diff --git a/traffic_ops/experimental/ui/package.json b/traffic_ops/experimental/ui/package.json index 376e8f6b6a..ec51762aa0 100755 --- a/traffic_ops/experimental/ui/package.json +++ b/traffic_ops/experimental/ui/package.json @@ -9,7 +9,6 @@ "devDependencies": { "morgan": "1.7.0", "errorhandler": "1.4.3", - "connect-modrewrite": "0.8.5", "connect-timeout": "1.7.0", "ssl-root-cas": "1.1.10", "express": "4.13.4", @@ -29,9 +28,7 @@ "grunt-contrib-watch": "0.6.1", "grunt-express-server": "0.5.2", "grunt-html2js": "0.3.5", - "grunt-install-dependencies": "0.2.0", "grunt-newer": "1.1.2", - "grunt-ng-constant": "2.0.1", "grunt-rev": "0.1.0", "grunt-usemin": "3.0.0", "grunt-wiredep": "2.0.0", diff --git a/traffic_ops/experimental/ui/server/server.js b/traffic_ops/experimental/ui/server/server.js index c6d3b38e40..a6037eb858 100644 --- a/traffic_ops/experimental/ui/server/server.js +++ b/traffic_ops/experimental/ui/server/server.js @@ -25,22 +25,11 @@ var constants = require('constants'), fs = require('fs'), morgan = require('morgan'), errorhandler = require('errorhandler'), - modRewrite = require('connect-modrewrite'), timeout = require('connect-timeout'); -var config; - -try { - // this should exist in prod environment. no need to create this file in dev as it will use the fallback (see catch) - config = require('/etc/traffic_ops_v2/conf/config'); -} -catch(e) { - // this is used for dev environment - config = require('../conf/config'); -} - -var logStream = fs.createWriteStream(config.log.stream, { flags: 'a' }); -var useSSL = config.useSSL; +var config = require('../conf/config'), + logStream = fs.createWriteStream(config.log.stream, { flags: 'a' }), + useSSL = config.useSSL; // Disable for self-signed certs in dev/test process.env.NODE_TLS_REJECT_UNAUTHORIZED = config.reject_unauthorized; @@ -60,10 +49,6 @@ app.all ("/*", function (req, res, next) { } }); -app.use(modRewrite([ - '^/api/(.*?)\\?(.*)$ ' + config.api.base_url + '/api/$1?$2&api_key=' + config.api.key + ' [P]', // match /api/{version}/foos?active=true and replace with api.base_url/api/{version}/foos?active=true&api_key=api.key - '^/api/(.*)$ ' + config.api.base_url + '/api/$1?api_key=' + config.api.key + ' [P]' // match /api/{version}/foos and replace with api.base_url/api/{version}/foos?api_key=api.key -])); app.use(express.static(config.files.static)); app.use(morgan('combined', { stream: logStream, @@ -72,14 +57,10 @@ app.use(morgan('combined', { app.use(errorhandler()); app.use(timeout(config.timeout)); -if (app.get('env') === 'dev') { - app.use(require('connect-livereload')({ - port: 35728, - excludeList: ['.woff', '.flv'] - })); -} else { - app.set('env', "production"); -} +app.use(require('connect-livereload')({ + port: 35728, + excludeList: ['.woff', '.flv'] +})); // Enable reverse proxy support in Express. This causes the // the "X-Forwarded-Proto" header field to be trusted so its @@ -112,6 +93,6 @@ if (useSSL) { sslOptions.agent = new https.Agent(sslOptions); } -console.log("Traffic Ops Port : %s", config.port); -console.log("Traffic Ops Proxy Port : %s", config.proxyPort); -console.log("Traffic Ops SSL Port : %s", config.sslPort); +console.log("Traffic Ops UI Port : %s", config.port); +console.log("Traffic Ops UI Proxy Port : %s", config.proxyPort); +console.log("Traffic Ops UI SSL Port : %s", config.sslPort); From 10dc9cd10bbded753ca028c0d9b6b67330933ce5 Mon Sep 17 00:00:00 2001 From: Jeremy Mitchell Date: Mon, 10 Apr 2017 10:54:36 -0600 Subject: [PATCH 2/3] moves server.js to root of project --- traffic_ops/experimental/ui/grunt/express.js | 2 +- traffic_ops/experimental/ui/{server => }/server.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename traffic_ops/experimental/ui/{server => }/server.js (98%) diff --git a/traffic_ops/experimental/ui/grunt/express.js b/traffic_ops/experimental/ui/grunt/express.js index b6be794261..97c3bda9a6 100644 --- a/traffic_ops/experimental/ui/grunt/express.js +++ b/traffic_ops/experimental/ui/grunt/express.js @@ -20,7 +20,7 @@ module.exports = { dev: { options: { - script: './server/server.js', + script: './server.js', node_env: 'dev' } } diff --git a/traffic_ops/experimental/ui/server/server.js b/traffic_ops/experimental/ui/server.js similarity index 98% rename from traffic_ops/experimental/ui/server/server.js rename to traffic_ops/experimental/ui/server.js index a6037eb858..002ab61b86 100644 --- a/traffic_ops/experimental/ui/server/server.js +++ b/traffic_ops/experimental/ui/server.js @@ -27,7 +27,7 @@ var constants = require('constants'), errorhandler = require('errorhandler'), timeout = require('connect-timeout'); -var config = require('../conf/config'), +var config = require('./conf/config'), logStream = fs.createWriteStream(config.log.stream, { flags: 'a' }), useSSL = config.useSSL; From 8d908896604e2c94d161d182c535d97fb3fa18af Mon Sep 17 00:00:00 2001 From: Jeremy Mitchell Date: Mon, 10 Apr 2017 10:59:08 -0600 Subject: [PATCH 3/3] only use connect-livereload in dev mode --- traffic_ops/experimental/ui/conf/config.js | 2 +- traffic_ops/experimental/ui/server.js | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/traffic_ops/experimental/ui/conf/config.js b/traffic_ops/experimental/ui/conf/config.js index 9666739c33..d1b13583b0 100644 --- a/traffic_ops/experimental/ui/conf/config.js +++ b/traffic_ops/experimental/ui/conf/config.js @@ -17,7 +17,7 @@ * under the License. */ -// this is the config that is consumed by server.js when running in dev mode +// this is the config that is consumed by server.js module.exports = { timeout: '120s', useSSL: false, // set to true if using ssl diff --git a/traffic_ops/experimental/ui/server.js b/traffic_ops/experimental/ui/server.js index 002ab61b86..5bdeee36c3 100644 --- a/traffic_ops/experimental/ui/server.js +++ b/traffic_ops/experimental/ui/server.js @@ -57,10 +57,14 @@ app.use(morgan('combined', { app.use(errorhandler()); app.use(timeout(config.timeout)); -app.use(require('connect-livereload')({ - port: 35728, - excludeList: ['.woff', '.flv'] -})); +if (app.get('env') === 'dev') { + app.use(require('connect-livereload')({ + port: 35728, + excludeList: ['.woff', '.flv'] + })); +} else { + app.set('env', 'production'); +} // Enable reverse proxy support in Express. This causes the // the "X-Forwarded-Proto" header field to be trusted so its