-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
731 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Port number | ||
PORT=3000 | ||
|
||
# Environment | ||
ENVIRONMENT=development | ||
|
||
# Postgres URL | ||
DATABASE_URL=postgresql://user:[email protected]:5432/my-db | ||
|
||
# Redis URL (for caching) | ||
REDIS_URL=redis://user:[email protected]:6379 | ||
|
||
# Redis URL (for queue) | ||
QUEUE_REDIS_HOST=127.0.0.1 | ||
QUEUE_REDIS_PORT=6380 | ||
|
||
# JWT | ||
JWT_SECRET=<secret> | ||
JWT_ACCESS_EXPIRATION_MINUTES=30 | ||
JWT_REFRESH_EXPIRATION_DAYS=6 | ||
JWT_RESET_PASSWORD_EXPIRATION_MINUTES=30 | ||
JWT_VERIFY_EMAIL_EXPIRATION_MINUTES=30 | ||
|
||
# Logging | ||
SHOW_SQL_QUERIES=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,13 @@ const envVarsSchema = Joi.object() | |
SHOW_SQL_QUERIES: Joi.bool().default(false), | ||
SENTRY_DSN: Joi.string().default(''), | ||
SENTRY_ENVIRONMENT: Joi.string().default('development'), | ||
COMPANY_SENDER_EMAIL: Joi.string().email().default('[email protected]'), | ||
QUEUE_REDIS_HOST: Joi.string().default(''), | ||
QUEUE_REDIS_PORT: Joi.number().default(6379), | ||
QUEUE_CONCURRENCY: Joi.number().default(1), | ||
AWS_REGION: Joi.string().default(''), | ||
AWS_SES_API_VERSION: Joi.string().default(''), | ||
API_STATUS: Joi.string().default(''), | ||
}) | ||
.unknown(); | ||
|
||
|
@@ -32,6 +39,9 @@ export default { | |
environment: envVars.ENVIRONMENT, | ||
port: envVars.PORT, | ||
redisUrl: envVars.REDIS_URL, | ||
api: { | ||
status: envVars.API_STATUS, | ||
}, | ||
logging: { | ||
showSqlQueries: envVars.SHOW_SQL_QUERIES, | ||
}, | ||
|
@@ -46,4 +56,16 @@ export default { | |
dsn: envVars.SENTRY_DSN, | ||
environment: envVars.SENTRY_ENVIRONMENT, | ||
}, | ||
emailService: { | ||
companySenderEmail: envVars.COMPANY_SENDER_EMAIL, | ||
queue: { | ||
redisHost: envVars.QUEUE_REDIS_HOST, | ||
redisPort: envVars.QUEUE_REDIS_PORT, | ||
concurrency: envVars.QUEUE_CONCURRENCY, | ||
}, | ||
}, | ||
aws: { | ||
region: envVars.AWS_REGION, | ||
ses_api_version: envVars.AWS_SES_API_VERSION, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import config from '../config'; | ||
import { ApiStatus } from '../../types/apiMetadata'; | ||
import { ApiUnavailable } from '../../utils/ApiError'; | ||
|
||
export const ValidateApiMetadata = async (req: Request, res: Response, next: NextFunction) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
(async () => { | ||
if (config.api.status === ApiStatus.Maintenance) throw new ApiUnavailable(); | ||
})() | ||
.then(() => next()) | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
.catch((error) => next(new ApiUnavailable())); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as cron from 'node-cron'; | ||
import apiMetadataRepository from '../repositories/apiMetadata'; | ||
import logger from '../core/logger'; | ||
import config from '../core/config'; | ||
import { ApiStatus } from '../types/apiMetadata'; | ||
|
||
export const ApiMaintenanceJob = cron.schedule( | ||
'*/10 * * * * *', | ||
async () => { | ||
let apiStatus = await apiMetadataRepository.getApiMetadata('status'); | ||
if (!apiStatus) { | ||
logger.warn('Api status is null, adding status immediately.'); | ||
apiStatus = await apiMetadataRepository.createApiMetadata('status', ApiStatus.Maintenance); | ||
} | ||
config.api.status = apiStatus.value; | ||
if (config.api.status === ApiStatus.Live) logger.info('Api is live.'); | ||
else if (config.api.status === ApiStatus.Maintenance) | ||
logger.warn('Api is in maintenance mode.'); | ||
}, | ||
{ | ||
scheduled: false, | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import logger from '../core/logger'; | ||
import { ApiMaintenanceJob } from './apiMaintenanace.job'; | ||
|
||
export const StartAllJobs = () => { | ||
ApiMaintenanceJob.start(); | ||
logger.info('API maintenance job started...'); | ||
}; | ||
|
||
export const StopAllJobs = () => { | ||
ApiMaintenanceJob.stop(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Email } from '../../types/email'; | ||
import { Job } from 'bullmq'; | ||
import { SES } from 'aws-sdk'; | ||
import nodemailer from 'nodemailer'; | ||
|
||
import config from '../../core/config'; | ||
|
||
const emailTransporter = nodemailer.createTransport({ | ||
SES: new SES({ | ||
region: config.aws.region, | ||
apiVersion: config.aws.ses_api_version, | ||
}), | ||
}); | ||
|
||
export default (job: Job<Email>) => emailTransporter.sendMail(job.data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { EMAIL_QUEUE_IDENTIFIER, Email } from '../../types/email'; | ||
import { Queue, Job } from 'bullmq'; | ||
import { redisConfig } from './redisConfig'; | ||
import EmailWorker from './email.worker'; | ||
import logger from '../../core/logger'; | ||
import { captureException } from '../../core/sentry'; | ||
|
||
export const emailQueue = new Queue<Email>(EMAIL_QUEUE_IDENTIFIER, { | ||
connection: redisConfig, | ||
}); | ||
|
||
EmailWorker.on('ready', () => { | ||
logger.info(`Email queue worker ready.`); | ||
}); | ||
|
||
EmailWorker.on('completed', (job: Job<Email>) => { | ||
logger.info(`Email sent to ${job.data.recipient} from ${job.data.sender}.`); | ||
}); | ||
|
||
EmailWorker.on('failed', (job: Job<Email> | undefined, error) => { | ||
logger.warn(`Sending email failed to ${job?.data.recipient} with error ${error}`); | ||
captureException(error, { | ||
recipient: job?.data.recipient, | ||
jobName: job?.name, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Worker } from 'bullmq'; | ||
import { redisConfig } from './redisConfig'; | ||
import { EMAIL_QUEUE_IDENTIFIER } from '../../types/email'; | ||
|
||
const EmailWorker = new Worker(EMAIL_QUEUE_IDENTIFIER, __dirname + '/email.processor.ts', { | ||
connection: redisConfig, | ||
}); | ||
|
||
export default EmailWorker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Redis from 'ioredis'; | ||
import logger from '../../core/logger'; | ||
|
||
export const redisConfig = new Redis({ | ||
port: 6380, | ||
host: '127.0.0.1', | ||
maxRetriesPerRequest: null, | ||
}); | ||
|
||
redisConfig.connect(() => { | ||
logger.info('Connected to queue redis.'); | ||
}); |
Oops, something went wrong.