Skip to content

Commit 3150300

Browse files
author
Sachin Maheshwari
committed
API versioning support.
1 parent 6c02662 commit 3150300

File tree

4 files changed

+70
-41
lines changed

4 files changed

+70
-41
lines changed

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# and runs it against the specified Topcoder backend (development or
33
# production) when container is executed.
44

5-
FROM node:8.2.1
6-
LABEL app="tc email" version="1.0"
5+
FROM node:6.10
6+
LABEL app="tc email" version="1.1"
77

88
WORKDIR /opt/app
99
COPY . .
1010
RUN npm install
11-
RUN npm install dotenv --save
11+
#RUN npm install dotenv --save
1212
RUN npm run lint
1313
CMD ["npm", "start"]

config/default.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ module.exports = {
4040

4141
//in every 2 minutes will retry for failed status
4242
EMAIL_RETRY_SCHEDULE: process.env.EMAIL_RETRY_SCHEDULE || '0 */2 * * * *',
43+
44+
API_CONTEXT_PATH: process.env.API_CONTEXT_PATH || 'email',
45+
API_VERSION: process.env.API_VERSION || 'v5',
46+
4347
};

src/app.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,27 @@ function configureKafkaConsumer(handlers) {
7979
// emailTries[topicName] += 1; //temporary disabling this feature
8080
emailModel.status = 'FAILED';
8181
return emailModel.save().then(() => {
82-
/*
83-
* temporary disabling this feature as there is chance of losing message during
84-
* unsubscribe/pausing due to simple kafka consumer
85-
*/
86-
/*
87-
const currentTries = emailTries[topicName];
88-
if (currentTries > maxErrors) {
89-
logger.debug(`Failed to send email. Will sleep for ${pauseTime}s`);
90-
emailTries[topicName] = 0;
91-
92-
schedule.scheduleJob(new Date(now.getTime() + pauseTime * 1000), () => {
93-
consumer.subscribe(topic, dataHandler);
94-
});
95-
96-
return consumer.unsubscribe(topic, partition).then(() => {
97-
throw result.error
98-
});
99-
} else {
100-
logger.debug(`Failed to send email (retries left ${maxErrors - currentTries})`);
101-
throw result.error;
102-
}*/
82+
/*
83+
* temporary disabling this feature as there is chance of losing message during
84+
* unsubscribe/pausing due to simple kafka consumer
85+
*/
86+
/*
87+
const currentTries = emailTries[topicName];
88+
if (currentTries > maxErrors) {
89+
logger.debug(`Failed to send email. Will sleep for ${pauseTime}s`);
90+
emailTries[topicName] = 0;
91+
92+
schedule.scheduleJob(new Date(now.getTime() + pauseTime * 1000), () => {
93+
consumer.subscribe(topic, dataHandler);
94+
});
95+
96+
return consumer.unsubscribe(topic, partition).then(() => {
97+
throw result.error
98+
});
99+
} else {
100+
logger.debug(`Failed to send email (retries left ${maxErrors - currentTries})`);
101+
throw result.error;
102+
}*/
103103
});
104104
}
105105
}).then(() => consumer.commitOffset({ topic, partition, offset: m.offset })) // commit offset
@@ -124,9 +124,9 @@ function startKafkaConsumer(consumer, handlers, dataHandler) {
124124
return consumer
125125
.init()
126126
.then(() => Promise.each(_.keys(handlers), (topicName) => { // add back the ignored topic prefix to use full topic name
127-
emailTries[topicName] = 0;
128-
return consumer.subscribe(`${config.KAFKA_TOPIC_IGNORE_PREFIX || ''}${topicName}`, dataHandler);
129-
})
127+
emailTries[topicName] = 0;
128+
return consumer.subscribe(`${config.KAFKA_TOPIC_IGNORE_PREFIX || ''}${topicName}`, dataHandler);
129+
})
130130
);
131131
}
132132

@@ -189,7 +189,8 @@ function start(handlers) {
189189
req.signature = `${def.controller}#${def.method}`;
190190
next();
191191
});
192-
if (url !== '/email/health') {
192+
if ((url !== '/email/health')
193+
&& (url !== `/${config.API_VERSION}/${config.API_CONTEXT_PATH}/health`)) {
193194
actions.push(jwtAuth());
194195
actions.push((req, res, next) => {
195196
if (!req.authUser) {

src/routes.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
'use strict';
22

3-
module.exports = {
4-
'/email/templates/eventType/:name': {
5-
get: {
6-
controller: 'TemplateController',
7-
method: 'eventTypes',
8-
},
9-
},
10-
'/email/health': {
11-
get: {
12-
controller: 'HealthController',
13-
method: 'health',
14-
},
15-
},
3+
const config = require('config');
4+
5+
var routes = {};
6+
7+
let oldkeyEventType = '/email/templates/eventType/:name';
8+
routes[oldkeyEventType] = {
9+
get: {
10+
controller: 'TemplateController',
11+
method: 'eventTypes',
12+
}
13+
};
14+
15+
let oldkeyHealthCheck = '/email/health';
16+
routes[oldkeyHealthCheck] = {
17+
get: {
18+
controller: 'HealthController',
19+
method: 'health',
20+
}
21+
};
22+
23+
let keyEventType = `/${config.API_VERSION}/${config.API_CONTEXT_PATH}/templates/eventType/:name`;
24+
routes[keyEventType] = {
25+
get: {
26+
controller: 'TemplateController',
27+
method: 'eventTypes',
28+
}
29+
};
30+
31+
let keyHealthCheck = `/${config.API_VERSION}/${config.API_CONTEXT_PATH}/health`;
32+
routes[keyHealthCheck] = {
33+
get: {
34+
controller: 'HealthController',
35+
method: 'health',
36+
}
1637
};
38+
39+
40+
module.exports = routes;

0 commit comments

Comments
 (0)