Skip to content

Commit 5e25ace

Browse files
author
sachin-maheshwari
authored
Merge pull request #5 from topcoder-platform/feature/version-support
API version support
2 parents 6c02662 + b59a639 commit 5e25ace

File tree

6 files changed

+83
-43
lines changed

6 files changed

+83
-43
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"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
## Dependencies
5-
- nodejs https://nodejs.org/en/ (v8+)
5+
- nodejs https://nodejs.org/en/ (v6+)
66
- Heroku Toolbelt https://toolbelt.heroku.com
77
- git
88
- PostgreSQL 9.5

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
};

deploy.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ EMAIL_PAUSE_TIME=$(eval "echo \$${ENV}_EMAIL_PAUSE_TIME")
6666
EMAIL_RETRY_SCHEDULE=$(eval "echo \"\$${ENV}_EMAIL_RETRY_SCHEDULE\"")
6767
DISABLE_LOGGING=$(eval "echo \$${ENV}_DISABLE_LOGGING")
6868

69+
API_CONTEXT_PATH=$(eval "echo \$${ENV}_API_CONTEXT_PATH")
70+
API_VERSION=$(eval "echo \$${ENV}_API_VERSION")
71+
6972
configure_aws_cli() {
7073
aws --version
7174
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
@@ -188,6 +191,14 @@ make_task_def(){
188191
{
189192
"name": "DISABLE_LOGGING",
190193
"value": "%s"
194+
},
195+
{
196+
"name": "API_CONTEXT_PATH",
197+
"value": "%s"
198+
},
199+
{
200+
"name": "API_VERSION",
201+
"value": "%s"
191202
}
192203
],
193204
"portMappings": [
@@ -209,7 +220,7 @@ make_task_def(){
209220
]
210221
}'
211222

212-
task_def=$(printf "$task_template" $family $AWS_ACCOUNT_ID $AWS_ECS_CONTAINER_NAME $AWS_ACCOUNT_ID $AWS_REGION $AWS_REPOSITORY $TAG $ENV $AUTH_DOMAIN $AUTH_SECRET $DATABASE_URL $EMAIL_FROM "$JWKS_URI" "$KAFKA_CLIENT_CERT" "$KAFKA_CLIENT_CERT_KEY" $KAFKA_GROUP_ID $KAFKA_URL $LOG_LEVEL $PORT $SENDGRID_API_KEY "$TEMPLATE_MAP" "$VALID_ISSUERS" $EMAIL_MAX_ERRORS $EMAIL_PAUSE_TIME "$EMAIL_RETRY_SCHEDULE" "$DISABLE_LOGGING" $PORT $PORT $AWS_ECS_CLUSTER $AWS_REGION $ENV)
223+
task_def=$(printf "$task_template" $family $AWS_ACCOUNT_ID $AWS_ECS_CONTAINER_NAME $AWS_ACCOUNT_ID $AWS_REGION $AWS_REPOSITORY $TAG $ENV $AUTH_DOMAIN $AUTH_SECRET $DATABASE_URL $EMAIL_FROM "$JWKS_URI" "$KAFKA_CLIENT_CERT" "$KAFKA_CLIENT_CERT_KEY" $KAFKA_GROUP_ID $KAFKA_URL $LOG_LEVEL $PORT $SENDGRID_API_KEY "$TEMPLATE_MAP" "$VALID_ISSUERS" $EMAIL_MAX_ERRORS $EMAIL_PAUSE_TIME "$EMAIL_RETRY_SCHEDULE" "$DISABLE_LOGGING" "$API_CONTEXT_PATH" "$API_VERSION" $PORT $PORT $AWS_ECS_CLUSTER $AWS_REGION $ENV)
213224

214225
}
215226

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_CONTEXT_PATH}/${config.API_VERSION}/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+
const routes = {};
6+
7+
const oldkeyEventType = '/email/templates/eventType/:name'
8+
routes[oldkeyEventType] = {
9+
get: {
10+
controller: 'TemplateController',
11+
method: 'eventTypes',
12+
}
13+
};
14+
15+
const oldkeyHealthCheck = '/email/health'
16+
routes[oldkeyHealthCheck] = {
17+
get: {
18+
controller: 'HealthController',
19+
method: 'health',
20+
}
21+
};
22+
23+
const keyEventType = `${config.API_CONTEXT_PATH}/${config.API_VERSION}/templates/eventType/:name`;
24+
routes[keyEventType] = {
25+
get: {
26+
controller: 'TemplateController',
27+
method: 'eventTypes',
28+
}
29+
};
30+
31+
const keyHealthCheck = `${config.API_CONTEXT_PATH}/${config.API_VERSION}/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)