Mholloway has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/300027

Change subject: Update to service-template-node 0.4.0
......................................................................

Update to service-template-node 0.4.0

Change-Id: Ief5a1d0806c5480f5e631007e44d96094b85d722
---
M .travis.yml
M README.md
M app.js
M lib/util.js
M package.json
M server.js
M test/utils/server.js
7 files changed, 59 insertions(+), 30 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps 
refs/changes/27/300027/1

diff --git a/.travis.yml b/.travis.yml
index 71ecd6e..f33adc7 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -5,5 +5,5 @@
 node_js:
   - "0.10"
   - "0.12"
-  - "4.3"
+  - "4"
   - "5"
diff --git a/README.md b/README.md
index f955bd9..7d7fdb9 100644
--- a/README.md
+++ b/README.md
@@ -42,7 +42,7 @@
 * Inspect/modify/configure `app.js`
 * Add routes by placing files in `routes/` (look at the files there for 
examples)
 
-You can also read [the documentation](doc/).
+You can also read [the 
documentation](https://www.mediawiki.org/wiki/ServiceTemplateNode).
 
 ### Running the service
 
diff --git a/app.js b/app.js
index 4666e47..1fe8a88 100644
--- a/app.js
+++ b/app.js
@@ -1,6 +1,8 @@
 'use strict';
 
 
+require('core-js/shim');
+
 var http = require('http');
 var BBPromise = require('bluebird');
 var express = require('express');
@@ -158,15 +160,20 @@
             if(route.constructor !== Object || !route.path || !route.router || 
!(route.api_version || route.skip_domain)) {
                 throw new TypeError('routes/' + fname + ' does not export the 
correct object!');
             }
-            // wrap the route handlers with Promise.try() blocks
-            sUtil.wrapRouteHandlers(route.router);
-            // determine the path prefix
-            var prefix = '';
-            if(!route.skip_domain) {
-                prefix = '/:domain/v' + route.api_version;
+            // normalise the path to be used as the mount point
+            if(route.path[0] !== '/') {
+                route.path = '/' + route.path;
             }
+            if(route.path[route.path.length - 1] !== '/') {
+                route.path = route.path + '/';
+            }
+            if(!route.skip_domain) {
+                route.path = '/:domain/v' + route.api_version + route.path;
+            }
+            // wrap the route handlers with Promise.try() blocks
+            sUtil.wrapRouteHandlers(route, app);
             // all good, use that route
-            app.use(prefix + route.path, route.router);
+            app.use(route.path, route.router);
         });
     }).then(function () {
         // catch errors
@@ -197,7 +204,7 @@
         );
     }).then(function () {
         app.logger.log('info',
-            'Worker ' + process.pid + ' listening on ' + app.conf.interface + 
':' + app.conf.port);
+            'Worker ' + process.pid + ' listening on ' + (app.conf.interface 
|| '*') + ':' + app.conf.port);
         return server;
     });
 
diff --git a/lib/util.js b/lib/util.js
index bbdd9c1..c326ba6 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -115,18 +115,38 @@
  * regardless of whether a handler returns/uses promises
  * or not.
  *
- * @param {Router} router object
+ * @param {Object} route the object containing the router and path to bind it 
to
+ * @param {Application} app the application object
  */
-function wrapRouteHandlers(router) {
+function wrapRouteHandlers(route, app) {
 
-    router.stack.forEach(function(routerLayer) {
+    route.router.stack.forEach(function(routerLayer) {
+        var path = (route.path + routerLayer.route.path.slice(1))
+            .replace(/\/:/g, '/--')
+            .replace(/^\//, '')
+            .replace(/[\/?]+$/, '');
+        path = app.metrics.normalizeName(path || 'root');
         routerLayer.route.stack.forEach(function(layer) {
             var origHandler = layer.handle;
             layer.handle = function(req, res, next) {
+                var startTime = Date.now();
                 BBPromise.try(function() {
                     return origHandler(req, res, next);
                 })
-                .catch(next);
+                .catch(next)
+                .finally(function() {
+                    var statusCode = parseInt(res.statusCode) || 500;
+                    if(statusCode < 100 || statusCode > 599) {
+                        statusCode = 500;
+                    }
+                    var statusClass = Math.floor(statusCode / 100) + 'xx';
+                    var stat = path + '.' + req.method + '.';
+                    app.metrics.endTiming([
+                        stat + statusCode,
+                        stat + statusClass,
+                        stat + 'ALL'
+                    ], startTime);
+                });
             };
         });
     });
diff --git a/package.json b/package.json
index 4cf4da8..4e28bea 100644
--- a/package.json
+++ b/package.json
@@ -43,6 +43,7 @@
     "bunyan": "^1.8.1",
     "cassandra-uuid": "^0.0.2",
     "compression": "^1.6.2",
+    "core-js": "^2.4.1",
     "domino": "^1.0.25",
     "express": "^4.14.0",
     "js-yaml": "^3.6.1",
diff --git a/server.js b/server.js
index da9fad8..a45b9ce 100755
--- a/server.js
+++ b/server.js
@@ -9,4 +9,4 @@
 // module(s) specified in the config 'services' section (app.js in this
 // example).
 var ServiceRunner = require('service-runner');
-return new ServiceRunner().run();
+new ServiceRunner().start();
diff --git a/test/utils/server.js b/test/utils/server.js
index 0b7972c..f632c7d 100644
--- a/test/utils/server.js
+++ b/test/utils/server.js
@@ -40,7 +40,7 @@
 // make a deep copy of it for later reference
 var origConfig = extend(true, {}, config);
 
-var stop = function () {};
+var stop = function() { return BBPromise.resolve(); };
 var options = null;
 var runner = new ServiceRunner();
 
@@ -51,20 +51,21 @@
 
     if (!assert.isDeepEqual(options, _options)) {
         console.log('server options changed; restarting');
-        stop();
-        options = _options;
-        // set up the config
-        config = extend(true, {}, origConfig);
-        extend(true, config.conf.services[myServiceIdx].conf, options);
-        return runner.run(config.conf)
-        .then(function(servers) {
-            var server = servers[0];
-            stop = function () {
-                console.log('stopping test server');
-                server.close();
-                stop = function () {};
-            };
-            return true;
+        return stop().then(function() {
+            options = _options;
+            // set up the config
+            config = extend(true, {}, origConfig);
+            extend(true, config.conf.services[myServiceIdx].conf, options);
+            return runner.start(config.conf)
+            .then(function() {
+                stop = function () {
+                    console.log('stopping test server');
+                    return runner.stop().then(function() {
+                        stop = function() { return BBPromise.resolve(); };
+                    });
+                };
+                return true;
+            });
         });
     } else {
         return BBPromise.resolve();

-- 
To view, visit https://gerrit.wikimedia.org/r/300027
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ief5a1d0806c5480f5e631007e44d96094b85d722
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: Mholloway <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to