Skip to content
This repository has been archived by the owner on Apr 2, 2019. It is now read-only.

Commit

Permalink
Add grunt, linting, enable in travis-ci and make pass
Browse files Browse the repository at this point in the history
  • Loading branch information
mooeypoo committed Feb 27, 2018
1 parent dec1620 commit b298b4a
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 227 deletions.
15 changes: 15 additions & 0 deletions .eslintrc.json
@@ -0,0 +1,15 @@
{
"extends": "kartotherian",
"rules": {
"comma-dangle": [2,"never"],
"one-var": [2,"always"],
"no-param-reassign": [2,{"props":false}],
"no-plusplus": [2,{"allowForLoopAfterthoughts": true}]
},
"env": {
"browser": true
},
"globals": {
"L": false
}
}
8 changes: 8 additions & 0 deletions .stylelintrc.json
@@ -0,0 +1,8 @@
{
"extends": "stylelint-config-wikimedia",
"rules": {
"declaration-no-important": null,
"selector-max-id": null,
"indentation": 2
}
}
6 changes: 6 additions & 0 deletions .travis.yml
@@ -0,0 +1,6 @@
language: node_js
node_js:
- "4"
- "8"
before_script:
- npm install grunt-cli -g
34 changes: 34 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,34 @@
/* eslint-env node */
module.exports = function Gruntfile(grunt) {
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-eslint');
grunt.loadNpmTasks('grunt-stylelint');

grunt.initConfig({
eslint: {
code: {
src: [
'**/*.js',
'!node_modules/**',
'!vendor/**',
'!tests/externals/**',
'!static/lib/**'
]
}
},
// Lint – Styling
stylelint: {
options: {
syntax: 'less'
},
all: [
'static/**/*.css',
'!static/lib/**'
]
}
});

grunt.registerTask('lint', ['eslint', 'stylelint']);
grunt.registerTask('test', 'lint');
grunt.registerTask('default', 'test');
};
7 changes: 7 additions & 0 deletions lib/.eslintrc.json
@@ -0,0 +1,7 @@
{
"extends": "../.eslintrc.json",
"env": {
"node": true,
"es6": true
}
}
60 changes: 29 additions & 31 deletions lib/info.js
@@ -1,17 +1,8 @@
'use strict';
const infoHeaders = {},
util = require('util'),
Promise = require('bluebird');

let util = require('util'),
Promise = require('bluebird');

let core,
infoHeaders = {};

module.exports = function info(cor, router) {
core = cor;

// get source info (json)
router.get('/:src(' + core.Sources.sourceIdReStr + ')/info.json', requestHandler);
};
let core;

/**
* Web server (express) route handler to get requested tile or info
Expand All @@ -20,23 +11,30 @@ module.exports = function info(cor, router) {
* @param next will be called if request is not handled
*/
function requestHandler(req, res, next) {
let source,
start = Date.now();

return Promise.try(() => {
source = core.getPublicSource(req.params.src);
return source.getHandler().getInfoAsync().then(info => [info, infoHeaders]);
}).spread((data, dataHeaders) => {
core.setResponseHeaders(res, source, dataHeaders);
const start = Date.now();
let source;

return Promise.try(() => {
source = core.getPublicSource(req.params.src);
return source.getHandler().getInfoAsync().then(info => [info, infoHeaders]);
}).spread((data, dataHeaders) => {
core.setResponseHeaders(res, source, dataHeaders);

if (req.query && req.query.format) {
const escapedText = JSON.stringify(data, null, ' ').replace(/&/g, '&amp;').replace(/</g, '&lt;');
res.send(`<pre>${escapedText}</pre>`);
} else {
res.json(data);
}

const mx = util.format('req.%s.info', req.params.src);
core.metrics.endTiming(mx, start);
}).catch(err => core.reportRequestError(err, res)).catch(next);
}

if (req.query && req.query.format) {
let escapedText = JSON.stringify(data, null, ' ').replace(/&/g, '&amp;').replace(/</g, '&lt;');
res.send('<pre>' + escapedText + '</pre>');
} else {
res.json(data);
}
module.exports = function info(cor, router) {
core = cor;

let mx = util.format('req.%s.info', req.params.src);
core.metrics.endTiming(mx, start);
}).catch(err => core.reportRequestError(err, res)).catch(next);
}
// get source info (json)
router.get(`/:src(${core.Sources.sourceIdReStr})/info.json`, requestHandler);
};
78 changes: 37 additions & 41 deletions lib/server.js
@@ -1,42 +1,38 @@
'use strict';

let Promise = require('bluebird'),
pathLib = require('path'),
express = require('express'),
compression = require('compression');

module.exports.init = function(opts) {

return Promise.try(function () {
let router = express.Router(),
handlers = opts.requestHandlers || [];

handlers.unshift(require('./tiles'), require('./info'));
return Promise.mapSeries(handlers, function (reqHandler) {
return reqHandler(opts.core, router);
}).return(router);

}).then(function (router) {
// Add before static to prevent disk IO on each tile request
let app = opts.app,
staticOpts = {
setHeaders: function (res) {
if (app.conf.cache) {
res.header('Cache-Control', app.conf.cache);
}
if (res.req.originalUrl.endsWith('.pbf')) {
res.header('Content-Encoding', 'gzip');
}
}
};

app.use('/', router);

// Compression is nativelly handled by the tiles, so only statics need its
app.use(compression());
app.use('/', express.static(pathLib.resolve(__dirname, '../static'), staticOpts));
app.use('/leaflet', express.static(pathLib.dirname(require.resolve('leaflet')), staticOpts));

opts.core.metrics.increment('init');
});
const Promise = require('bluebird'),
pathLib = require('path'),
express = require('express'),
compression = require('compression'),
tiles = require('./tiles'),
info = require('./info');

module.exports.init = function init(opts) {
return Promise.try(() => {
const router = express.Router(),
handlers = opts.requestHandlers || [];

handlers.unshift(tiles, info);
return Promise.mapSeries(handlers, reqHandler => reqHandler(opts.core, router)).return(router);
}).then((router) => {
// Add before static to prevent disk IO on each tile request
const { app } = opts,
staticOpts = {
setHeaders(res) {
if (app.conf.cache) {
res.header('Cache-Control', app.conf.cache);
}
if (res.req.originalUrl.endsWith('.pbf')) {
res.header('Content-Encoding', 'gzip');
}
}
};

app.use('/', router);

// Compression is nativelly handled by the tiles, so only statics need its
app.use(compression());
app.use('/', express.static(pathLib.resolve(__dirname, '../static'), staticOpts));
app.use('/leaflet', express.static(pathLib.dirname(require.resolve('leaflet')), staticOpts));

opts.core.metrics.increment('init');
});
};

0 comments on commit b298b4a

Please sign in to comment.