Skip to content

Commit

Permalink
Basing server on restify.
Browse files Browse the repository at this point in the history
  • Loading branch information
kallaspriit committed Mar 30, 2016
1 parent 21cf171 commit 8c2048b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 39 deletions.
83 changes: 59 additions & 24 deletions lib/server/Server.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "context-broker",
"name": "cumulocity-server",
"version": "1.0.0",
"description": "fiware context broker communication",
"main": "index.js",
Expand All @@ -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": {
Expand Down
16 changes: 2 additions & 14 deletions server.js
Original file line number Diff line number Diff line change
@@ -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');
});

Expand Down

0 comments on commit 8c2048b

Please sign in to comment.