-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
294 lines (263 loc) · 9.14 KB
/
server.js
File metadata and controls
294 lines (263 loc) · 9.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
const express = require('express');
const app = express();
app.use(express.json());
app.use(handleJsonError);
require('dotenv').config();
const AWS = require('aws-sdk');
const axios = require('axios');
const winston = require('winston');
const format = winston.format;
const fs = require('fs');
// Middleware for express and json parser errors
function handleJsonError(err, req, res, next) {
if (err instanceof SyntaxError && err.type === 'entity.parse.failed') {
// Handle JSON parsing error
logger.error('Invalid JSON format');
return res.status(400).send({
success: false,
status: 'error',
code: 'Invalid JSON format'
});
}
// Pass other errors to the next middleware
next(err);
}
// Create logger
const logger = winston.createLogger({
level: 'debug', // Set the minimum logging level
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json(),
format.colorize({ all: true })
),
transports: [
new winston.transports.Console()
]
});
// Create an SQS service object
const sqs = new AWS.SQS({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.SQS_REGION,
apiVersion: process.env.SQS_VERSION,
});
// Create local configuration
const config = { //API
sqs_queue_url: process.env.SQS_QUEUE_URL,
listen_interval: process.env.LISTEN_INTERVAL_MS || 5000,
port: process.env.PORT || 3000,
}
/**
* Continuously checks for and processes messages from an SQS queue.
* This function should be executed in background in a differend deployment.
* Producer and Consumer actions are together in this project for learning purposes.
*/
const listen = () => { //Consumer
sqs.receiveMessage({
QueueUrl: config.sqs_queue_url,
MaxNumberOfMessages: 1, // Get at most one message
VisibilityTimeout: 10, // Make the message invisible for 10 seconds while processing
})
.promise()
.then(data => {
if (data.Messages && data.Messages.length > 0) {
const message = data.Messages[0];
const messageBody = JSON.parse(message.Body);
// Process the message body here (e.g., parse JSON, perform actions)
logger.info('CONSUMER: Message received:', messageBody);
// Delete the message from the queue after processing
return sqs.deleteMessage({
QueueUrl: config.sqs_queue_url,
ReceiptHandle: message.ReceiptHandle
})
.promise()
.then(() => {
//Notify on provided webhook
const url = messageBody.webhook;
const result = {
messageId: message.MessageId,
status: "processed",
}
return axios.post(url, result)
});
} else {
logger.debug('CONSUMER: No messages in the queue');
}
})
.catch(error => {
logger.error('Error receiving message:', error);
});
};
/**
* Checks if an API key is valid
*/
const validateApiKey = async(apiKey) => {
// Path to your apiKeys database
const filePath = './data-users.json';
try {
// Compare the apiKey
const data = fs.readFileSync(filePath, 'utf8');
const jsonData = JSON.parse(data);
const user = jsonData[apiKey]
if(user) {
return {
success : true,
user: user
};
}
} catch (error) {
logger.error('Error reading or parsing JSON file:', error);
return {
success:false,
http:500,
code:"Internal server error",
status: "error"
};
}
return {
success:false,
http:401,
code:"Invalid x-api-key",
status: "error"
};
}
/**
* Validates a transaction by checking if it exists in a price list.
* Checks if the user has enough credits to afford it.
*/
const validateTransaction = async (transaction, credits) => {
// Path to your prices database
const filePath = './data-prices.json';
let prices;
try {
// Compare the apiKey
const data = fs.readFileSync(filePath, 'utf8');
prices = JSON.parse(data);
} catch (error) {
logger.error('Error reading or parsing JSON file:', error);
return {
success:false,
http:500,
code:"Internal server error",
status: "error"
};
}
// Check if transaction exists
if (!prices.hasOwnProperty(transaction)) {
logger.error("Transaction invalid");
return {
success:false,
http: 400,
code: "Transaction invalid"
};
}
// Check if credits are enought
if(prices[transaction] > credits) {
logger.error("Low balance");
return {
success:false,
http: 403, code: "Low balance"};
}
return {success:true};
}
/**
* This function acts as an API endpoint for submitting jobs or tasks to a designated SQS queue.
* It performs API key validation for security.
* Sends the job data as a message to the SQS queue for asynchronous processing.
* Client acknowledges successful queuing with an appropriate status code and the message ID.
*/
app.post('/jobs', async (req, res) => { //Producer
// Accessing the x-api-key header (case-insensitive)
if (req.headers['x-api-key']) {
const apiKey = req.headers['x-api-key'];
// Logic to validate the API key
const validKey = await validateApiKey(apiKey);
// Logic to validate the payload
if (validKey.success) {
const recipe = req.body.recipe;
// Logic to validate if user has enough credits for the transaction
const transaction = recipe.transaction;
const credits = validKey.user.credits;
const validTransaction = await validateTransaction(transaction, credits);
if(!validTransaction.success) {
logger.error(`PRODUCER: ${validTransaction.code}`);
return res.status(validTransaction.http).json({
success: false,
status: "error",
code: validTransaction.code
});
}
// Validate the recipe.
// Add any validation you consider
if (!recipe.hasOwnProperty('webhook')) {
logger.error('PRODUCER: No webhook provided');
return res.status(400).json({
success: false,
status: "error",
code: "No webhook provided in the recipe"
});
}
if (!recipe.hasOwnProperty('transaction')) {
logger.error('PRODUCER: No transaction provided');
return res.status(400).json({
success: false,
status: "error",
code: "No transaction provided in the recipe"
});
}
const sendMessageParams = {
QueueUrl: config.sqs_queue_url,
MessageBody: JSON.stringify(recipe),
};
sqs.sendMessage(sendMessageParams, (err, data) => {
if (err) {
logger.error('PRODUCER: Error sending message to SQS:', err);
return res.status(400).json({
success: false,
status: "error",
code: err.code
});
} else {
logger.info('PRODUCER: Message sent to SQS:', data.MessageId);
return res.status(202).json({
success: true,
status: "queued",
code: data.MessageId
});
}
});
} else {
// Precise messaje from the key validation
logger.error(`PRODUCER: ${validKey.code}`);
return res.status(validKey.http).send({
success: false,
status: validKey.status,
code: validKey.code
});
}
} else {
logger.error('PRODUCER: Missing x-api-key header');
return res.status(401).send({
success: false,
status: 'error',
code: 'Missing x-api-key header'
});
}
});
/**
* Endpoint that handles the final result of the operation.
* The action started in the producer and was processed by the consumer.
* At this stage the consumer has completed the job and notifies the final result to this webhook.
*/
app.post('/notifications', async (req, res) => { //Webhook
//Do any verifications you need (token, user ...)
//Do any job you like
logger.info("WEBHOOK", req.body);
return res.status(200).send({success: true});
})
// Listen for messages every n seconds
setInterval(listen, config.listen_interval);
// Start the server
app.listen(config.port, () => {
console.log(`\nServer listening on port ${config.port}\n`);
});