Skip to content

Commit

Permalink
feat: Split SNS webhook server from main HTTP server
Browse files Browse the repository at this point in the history
  • Loading branch information
diego3g committed Aug 13, 2021
1 parent 4e89a57 commit c810331
Show file tree
Hide file tree
Showing 21 changed files with 75 additions and 35 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev:server": "tsnd --respawn --transpile-only --ignore-watch node_modules --exit-child --clear -r tsconfig-paths/register src/infra/http/server.ts",
"dev:worker": "tsnd --respawn --transpile-only --ignore-watch node_modules --exit-child --clear -r tsconfig-paths/register src/infra/queue/worker.ts",
"dev:kafka": "tsnd --respawn --transpile-only --ignore-watch node_modules --exit-child --clear -r tsconfig-paths/register src/infra/kafka/app.ts",
"dev:webhook": "tsnd --respawn --transpile-only --ignore-watch node_modules --exit-child --clear -r tsconfig-paths/register src/infra/sns-webhook/server.ts",
"start": "node dist/infra/http/server.js",
"lint": "eslint src --ext ts,tsx",
"test": "jest",
Expand Down
5 changes: 5 additions & 0 deletions scripts/webhook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

(cd /home/node/app && node_modules/.bin/prisma migrate deploy)
(cd /home/node/app && node_modules/.bin/prisma generate)
(cd /home/node/app && node dist/infra/sns-webhook/server.js)
5 changes: 3 additions & 2 deletions src/infra/http/routes/contacts.routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import express from 'express'

import { adaptMiddleware } from '../adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '../adapters/ExpressRouteAdapter'
import { adaptMiddleware } from '@core/infra/adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '@core/infra/adapters/ExpressRouteAdapter'

import { makeBlockContactController } from '../factories/controllers/BlockContactControllerFactory'
import { makeCreateContactController } from '../factories/controllers/CreateContactControllerFactory'
import { makeGetContactDetailsController } from '../factories/controllers/GetContactDetailsControllerFactory'
Expand Down
16 changes: 0 additions & 16 deletions src/infra/http/routes/events.routes.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/infra/http/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Router } from 'express'

import { contactsRouter } from './contacts.routes'
import { eventsRouter } from './events.routes'
import { messagesRouter } from './messages.routes'
import { recipientsRouter } from './recipients.routes'
import { sendersRouter } from './senders.routes'
Expand All @@ -15,7 +14,6 @@ const router = Router()
router.use('/users', usersRouter)
router.use('/sessions', sessionsRouter)
router.use('/messages', messagesRouter)
router.use('/events', eventsRouter)
router.use('/contacts', contactsRouter)
router.use('/senders', sendersRouter)
router.use('/tags', tagsRouter)
Expand Down
5 changes: 3 additions & 2 deletions src/infra/http/routes/messages.routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import express from 'express'

import { adaptMiddleware } from '../adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '../adapters/ExpressRouteAdapter'
import { adaptMiddleware } from '@core/infra/adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '@core/infra/adapters/ExpressRouteAdapter'

import { makeCreateMessageController } from '../factories/controllers/CreateMessageControllerFactory'
import { makeGetMessageDetailsController } from '../factories/controllers/GetMessageDetailsControllerFactory'
import { makeGetMessageStatsController } from '../factories/controllers/GetMessageStatsControllerFactory'
Expand Down
5 changes: 3 additions & 2 deletions src/infra/http/routes/recipients.routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import express from 'express'

import { adaptMiddleware } from '../adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '../adapters/ExpressRouteAdapter'
import { adaptMiddleware } from '@core/infra/adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '@core/infra/adapters/ExpressRouteAdapter'

import { makeCountRecipientsFromTagsController } from '../factories/controllers/CountRecipientsFromTagsControllerFactory'
import { makeEnsureAuthenticatedMiddleware } from '../factories/middlewares/EnsureAuthenticatedMiddlewareFactory'

Expand Down
5 changes: 3 additions & 2 deletions src/infra/http/routes/senders.routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import express from 'express'

import { adaptMiddleware } from '../adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '../adapters/ExpressRouteAdapter'
import { adaptMiddleware } from '@core/infra/adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '@core/infra/adapters/ExpressRouteAdapter'

import { makeCreateSenderController } from '../factories/controllers/CreateSenderControllerFactory'
import { makeGetAllSendersController } from '../factories/controllers/GetAllSendersControllerFactory'
import { makeRemoveSenderController } from '../factories/controllers/RemoveSenderControllerFactory'
Expand Down
3 changes: 2 additions & 1 deletion src/infra/http/routes/sessions.routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express'

import { adaptRoute } from '../adapters/ExpressRouteAdapter'
import { adaptRoute } from '@core/infra/adapters/ExpressRouteAdapter'

import { makeAuthenticateUserController } from '../factories/controllers/AuthenticateUserControllerFactory'

const sessionsRouter = express.Router()
Expand Down
4 changes: 2 additions & 2 deletions src/infra/http/routes/tags.routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express'

import { adaptMiddleware } from '../adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '../adapters/ExpressRouteAdapter'
import { adaptMiddleware } from '@core/infra/adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '@core/infra/adapters/ExpressRouteAdapter'
import { makeCreateTagController } from '../factories/controllers/CreateTagControllerFactory'
import { makeGetAllTagsController } from '../factories/controllers/GetAllTagsControllerFactory'
import { makeSearchTagsController } from '../factories/controllers/SearchTagsControllerFactory'
Expand Down
4 changes: 2 additions & 2 deletions src/infra/http/routes/templates.routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express'

import { adaptMiddleware } from '../adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '../adapters/ExpressRouteAdapter'
import { adaptMiddleware } from '@core/infra/adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '@core/infra/adapters/ExpressRouteAdapter'
import { makeCreateTemplateController } from '../factories/controllers/CreateTemplateControllerFactory'
import { makeGetAllTemplatesController } from '../factories/controllers/GetAllTemplatesControllerFactory'
import { makePreviewTemplateController } from '../factories/controllers/PreviewTemplateControllerFactory'
Expand Down
4 changes: 2 additions & 2 deletions src/infra/http/routes/users.routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express'

import { adaptMiddleware } from '../adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '../adapters/ExpressRouteAdapter'
import { adaptMiddleware } from '@core/infra/adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '@core/infra/adapters/ExpressRouteAdapter'
import { makeRegisterUserController } from '../factories/controllers/RegisterUserControllerFactory'
import { makeEnsureAuthenticatedMiddleware } from '../factories/middlewares/EnsureAuthenticatedMiddlewareFactory'

Expand Down
25 changes: 25 additions & 0 deletions src/infra/sns-webhook/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import cors from 'cors'
import { config } from 'dotenv-flow'
import express from 'express'

config({ silent: true })

import { router } from './routes' // eslint-disable-line

const app = express()

app.use(
cors({
exposedHeaders: ['x-total-count', 'Content-Type', 'Content-Length'],
})
)

app.use(
express.json({
type: ['application/json', 'text/plain'],
})
)

app.use(router)

export { app }
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Middleware } from '@core/infra/Middleware'
import { AmazonSNSValidatorMiddleware } from '@infra/http/middlewares/AmazonSNSValidatorMiddleware'
import { AmazonSNSValidatorMiddleware } from '@infra/sns-webhook/middlewares/AmazonSNSValidatorMiddleware'

export function makeAmazonSNSValidatorMiddleware(): Middleware {
const amazonSNSValidatorMiddleware = new AmazonSNSValidatorMiddleware()
Expand Down
17 changes: 17 additions & 0 deletions src/infra/sns-webhook/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import express from 'express'

import { adaptMiddleware } from '@core/infra/adapters/ExpressMiddlewareAdapter'
import { adaptRoute } from '@core/infra/adapters/ExpressRouteAdapter'

import { makeRegisterEventController } from './factories/controllers/RegisterEventControllerFactory'
import { makeAmazonSNSValidatorMiddleware } from './factories/middlewares/AmazonSNSValidatorMiddlewareFactory'

const router = express.Router()

router.post(
'/events/notifications',
adaptMiddleware(makeAmazonSNSValidatorMiddleware()),
adaptRoute(makeRegisterEventController())
)

export { router }
5 changes: 5 additions & 0 deletions src/infra/sns-webhook/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { app } from './app'

app.listen(process.env.PORT, () => {
console.log(`Server running on http://localhost:${process.env.PORT}`)
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import request from 'supertest'
import { v4 as uuid } from 'uuid'

import { app } from '@infra/http/app'
import { prisma } from '@infra/prisma/client'
import { redisConnection } from '@infra/redis/connection'
import { app } from '@infra/sns-webhook/app'

import { validEventTypes } from '../../domain/event/type'

Expand Down

0 comments on commit c810331

Please sign in to comment.