Skip to content

Commit 0419ef0

Browse files
committed
Connect to AMQP and fix io-ts import issues
1 parent bb604f3 commit 0419ef0

File tree

10 files changed

+38
-17
lines changed

10 files changed

+38
-17
lines changed

Diff for: dev/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
FROM node:10
22
WORKDIR /app
33

4-
COPY src ./src
54
COPY package.json yarn.lock tsconfig.json ./
65
RUN yarn
6+
COPY src ./src
77

88
CMD yarn run ts-node -r tsconfig-paths/register src/apps/webapp/index.ts

Diff for: docker-compose.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ services:
3636
- "5672:5672"
3737
- "15672:15672"
3838
environment:
39-
RABBITMQ_DEFAULT_USER: username
40-
RABBITMQ_DEFAULT_PASS: password
39+
RABBITMQ_DEFAULT_USER: guest
40+
RABBITMQ_DEFAULT_PASS: guest

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"amqplib": "0.5.5",
2222
"body-parser": "1.19.0",
2323
"express": "4.17.1",
24-
"io-ts": "2.2.1",
24+
"io-ts": "2.2.2",
2525
"io-ts-reporters": "1.0.0",
2626
"io-ts-types": "0.5.6",
2727
"pg": "8.0.3",

Diff for: src/adapters/rabbitmq/index.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import * as amqp from 'amqplib'
22
import { Ctx } from '@lib'
33

4+
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
5+
46
export async function $adapter (): Promise<Ctx.ContextAdapter> {
5-
const connection = await amqp.connect('amqp://localhost')
7+
const connection = await attemptConnection()
68
const channel = await connection.createChannel()
79

810
await channel.assertQueue('messages')
@@ -26,3 +28,23 @@ export async function $adapter (): Promise<Ctx.ContextAdapter> {
2628
}
2729
}
2830
}
31+
32+
async function attemptConnection (count: number = 6): Promise<amqp.Connection> {
33+
try {
34+
const connection = await amqp.connect('amqp://guest:guest@rabbitmq:5672')
35+
console.log('Connected to AMQP')
36+
37+
return connection
38+
39+
} catch (err) {
40+
if (!count) {
41+
console.log('AMQP connection failed')
42+
throw err
43+
44+
} else {
45+
console.log('Retrying AMQP connection in 5 seconds...')
46+
await wait(5000)
47+
return await attemptConnection(count - 1)
48+
}
49+
}
50+
}

Diff for: src/lib/entities/Book.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import t from 'io-ts'
1+
import * as t from 'io-ts'
22
import { UUID } from 'io-ts-types/lib/UUID'
33
import { $cast } from './cast'
44

Diff for: src/lib/entities/Loan.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import t from 'io-ts'
1+
import * as t from 'io-ts'
22
import { UUID } from 'io-ts-types/lib/UUID'
33

44
import { User } from './User'

Diff for: src/lib/entities/User.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import t from 'io-ts'
1+
import * as t from 'io-ts'
22
import { UUID } from 'io-ts-types/lib/UUID'
33
import { $cast } from './cast'
44

Diff for: src/lib/operations/book.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Context } from '../context'
33

44
export async function addBook (ctx: Context, bookInput: BookInput): Promise<Book> {
55
const {
6-
backend: { bookStore },
7-
middleware: { events }
6+
backend: { bookStore },
7+
events
88
} = ctx
99

1010
const book = await bookStore.add(bookInput)

Diff for: src/lib/operations/loan.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { Book, LoanInput, LoanResolution, User } from '../entities'
77

88
export async function loanBook (ctx: Context, loanInput: LoanInput): Promise<LoanResolution> {
99
const {
10-
backend: { userStore, bookStore, loanStore },
11-
middleware: { events }
10+
backend: { userStore, bookStore, loanStore },
11+
events
1212
} = ctx
1313

1414
const user = await userStore.find(loanInput.userId)

Diff for: src/lib/operations/user.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import { User, UserInput } from '../entities'
33
import { Context } from '../context'
44
import { UserDoesNotExist, UserHasOutstandingLoans } from '../errors'
55

6-
76
export async function addUser (ctx: Context, userInput: UserInput): Promise<User> {
87
const {
9-
backend: { userStore },
10-
middleware: { events }
8+
backend: { userStore },
9+
events
1110
} = ctx
1211

1312
const user = await userStore.add(userInput)
@@ -22,8 +21,8 @@ export async function addUser (ctx: Context, userInput: UserInput): Promise<User
2221

2322
export async function removeUser (ctx: Context, userId: UUID): Promise<void> {
2423
const {
25-
backend: { loanStore, userStore },
26-
middleware: { events }
24+
backend: { loanStore, userStore },
25+
events
2726
} = ctx
2827

2928
const user = await userStore.find(userId)

0 commit comments

Comments
 (0)