From 8c2048be26a9d3fea11bbb14a1d4154602fd9e4f Mon Sep 17 00:00:00 2001 From: Priit Kallas Date: Wed, 30 Mar 2016 11:23:18 +0300 Subject: [PATCH] Basing server on restify. --- lib/server/Server.js | 83 +++++++++++++++++++++++++++++++------------- package.json | 3 +- server.js | 16 ++------- 3 files changed, 63 insertions(+), 39 deletions(-) diff --git a/lib/server/Server.js b/lib/server/Server.js index e91d627..2dc810c 100644 --- a/lib/server/Server.js +++ b/lib/server/Server.js @@ -1,81 +1,116 @@ -import express from 'express'; +import path from 'path'; +import restify from 'restify'; import bodyParser from 'body-parser'; import cors from 'cors'; import logger from '../logger'; const log = logger.get(__filename); +export const Method = { + GET: 'GET', + POST: 'POST', + PUT: 'PUT' +}; + export default class Server { - static Method = { - GET: 'GET', - POST: 'POST', - PUT: 'PUT' - }; + static Method = Method; - constructor() { + constructor(serverConfig = {}) { // create the server app - this.app = express(); + this.server = restify.createServer(serverConfig); + + this.statistics = { + requestCount: 0 + }; } setupDefault() { log('performing default setup'); + this.setupCurlSupport(); this.setupCors(); this.setupBodyParser(); + this.setupGlobalRoute(); this.setupIndexRoute(); + this.setupAppRoutes(); return this; } + + setupCurlSupport() { + this.server.pre(restify.pre.userAgentConnection()); + } setupCors() { log('setting up CORS'); // allow cors from all requests - this.app.use(cors()); + this.server.use(cors()); } setupBodyParser() { log('setting up body parser'); // use json body parser - this.app.use(bodyParser.json({ + this.server.use(bodyParser.json({ limit: '1mb' })); } + setupGlobalRoute() { + log('setting up global route'); + + this.server.use((req, res, next) => { + this.updateStatistics(req, res); + + next(); + }); + } setupIndexRoute() { log('setting up index route'); - this.get('/', (req, res) => { - res.send('wazaaa'); + this.register('index', Method.GET, '/', (req, res) => { + res.send('index route response: ' + this.statistics.requestCount); }); } + setupAppRoutes() { + log('setting up app routes'); + + this.server.get(/\/apps\/?.*/, restify.serveStatic({ + directory: path.join(__dirname, '../..'), + default: 'index.html' + })); + } + + updateStatistics(/* req, res */) { + this.statistics.requestCount++; + } + + use(...args) { + return this.server.use(...args); + } + start(port) { log('starting server on port ' + port); // start the server - this.app.listen(port, () => { + this.server.listen(port, () => { log('server started on port ' + port); }); return this; } - register(method = Server.Method.GET, path, callback) { - log('registering ' + method + ' ' + path); - - this.app[method.toLowerCase()](path, callback); - } + register(name, method = Server.Method.GET, handlerPath, callback) { + log('registering route "' + name + '" to ' + method + ' ' + handlerPath); - get(path, callback) { - return this.register(Server.Method.GET, path, (req, res, next) => { - this.logRequest(req); - - callback(req, res, next); - }); + this.server[method.toLowerCase()]({ + name: name, + path: handlerPath + }, callback); } // logs the request info to console diff --git a/package.json b/package.json index 51263ff..8f4be21 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "context-broker", + "name": "cumulocity-server", "version": "1.0.0", "description": "fiware context broker communication", "main": "index.js", @@ -24,6 +24,7 @@ "inquirer": "^0.12.0", "moment": "^2.12.0", "request": "^2.69.0", + "restify": "^4.0.4", "serve-static": "^1.10.2" }, "devDependencies": { diff --git a/server.js b/server.js index b2533d7..039809a 100644 --- a/server.js +++ b/server.js @@ -1,23 +1,11 @@ -import serveStatic from 'serve-static'; import config from './config'; import Server from './lib/server/Server'; -import logger from './lib/logger'; -const log = logger.get(__filename, { env: 'dev' }); - -log('bootstrapping', 123, { 'a': 1 }); - - -const server = new Server(); +const server = new Server(config.server); server.setupDefault(); -// serve static files from apps -server.app.use(serveStatic('apps', { - index: 'index.html' -})); - -server.get('/test', (req, res) => { +server.register('test', Server.Method.GET, '/test', (req, res) => { res.send('test response'); });