Skip to content

Commit

Permalink
feat: implemented tests and gha for express-rest case
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLzq committed Jul 5, 2022
1 parent 6b6c47f commit e1f8b32
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 558 deletions.
31 changes: 31 additions & 0 deletions example/express/.github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Tests - example/express

on: [push]

jobs:
test:
name: Testing Simba.js API
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 16.x

- name: Install Node.js dependencies
run: yarn

- name: Revert changes into the yarn.lock file
run: git checkout -- yarn.lock

- name: Run test
run: yarn test:ci
env:
MONGO_URI: ${{ secrets.MONGO_URI }}
NODE_ENV: ci
6 changes: 3 additions & 3 deletions example/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
"author": "AnthonyLzq <sluzquinosa@uni.pe>",
"license": "MIT",
"dependencies": {
"@sinclair/typebox": "^0.24.9",
"@sinclair/typebox": "^0.24.10",
"ajv": "^6",
"cors": "^2.8.5",
"express": "^4.18.1",
"express-pino-logger": "^7.0.0",
"http-errors": "^2.0.0",
"mongoose": "^6.4.2",
"mongoose": "^6.4.3",
"pino-pretty": "^8.1.0",
"swagger-ui-express": "^4.4.0"
},
Expand Down Expand Up @@ -50,7 +50,7 @@
"eslint-plugin-promise": "^6.0.0",
"jest": "^28.1.2",
"jest-unit": "^0.0.1",
"nodemon": "^2.0.18",
"nodemon": "^2.0.19",
"prettier": "^2.7.1",
"standard-version": "^9.5.0",
"ts-jest": "^28.0.5",
Expand Down
55 changes: 55 additions & 0 deletions example/express/src/database/mongo/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { connect, connection } from 'mongoose'
import { HttpLogger } from 'express-pino-logger'

const ENVIRONMENTS_WITHOUT_RECONNECTION = ['ci', 'local']
const dbConnection = async (
logger: HttpLogger['logger']
): Promise<{
connect: () => Promise<typeof import('mongoose')>
disconnect: () => Promise<void>
}> => {
const connectionConfig = {
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true
}

connection.on('connected', () => {
logger.info('Mongo connection established.')
})
connection.on('reconnected', () => {
logger.info('Mongo connection reestablished')
})
connection.on('disconnected', () => {
if (
!ENVIRONMENTS_WITHOUT_RECONNECTION.includes(
process.env.NODE_ENV as string
)
) {
logger.info(
'Mongo connection disconnected. Trying to reconnected to Mongo...'
)
setTimeout(() => {
connect(process.env.MONGO_URI as string, {
...connection,
connectTimeoutMS: 3000,
socketTimeoutMS: 3000
})
}, 3000)
}
})
connection.on('close', () => {
logger.info('Mongo connection closed')
})
connection.on('error', (e: Error) => {
logger.info('Mongo connection error:')
logger.error(e)
})

return {
connect: () => connect(process.env.MONGO_URI as string, connectionConfig),
disconnect: () => connection.close()
}
}

export { dbConnection }
1 change: 1 addition & 0 deletions example/express/src/database/mongo/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './models'
export * from './queries'
export * from './connection'
77 changes: 24 additions & 53 deletions example/express/src/network/server.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import { Server as HttpServer } from 'http'
import express from 'express'
import mongoose from 'mongoose'
import cors from 'cors'
import pino, { HttpLogger } from 'express-pino-logger'

import { dbConnection } from 'database'
import { applyRoutes } from './router'

const PORT = (process.env.PORT as string) || 1996
const ENVIRONMENTS_WITHOUT_PRETTY_PRINT = ['production', 'ci']

class Server {
#app: express.Application
#connection: mongoose.Connection | undefined
#log: HttpLogger
#server: HttpServer | undefined
#connection: Awaited<ReturnType<typeof dbConnection>> | undefined

constructor() {
this.#app = express()
this.#log = pino({
transport:
process.env.ENVIRONMENT !== 'production'
? {
target: 'pino-pretty',
options: {
translateTime: 'HH:MM:ss Z',
ignore: 'pid,hostname'
}
transport: !ENVIRONMENTS_WITHOUT_PRETTY_PRINT.includes(
process.env.NODE_ENV as string
)
? {
target: 'pino-pretty',
options: {
translateTime: 'HH:MM:ss Z',
ignore: 'pid,hostname'
}
: undefined
}
: undefined
})
this.#config()
}
Expand Down Expand Up @@ -55,57 +57,26 @@ class Server {
applyRoutes(this.#app)
}

async #mongo(): Promise<void> {
this.#connection = mongoose.connection
const connection = {
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true
}
this.#connection.on('connected', () => {
this.#log.logger.info('Mongo connection established.')
})
this.#connection.on('reconnected', () => {
this.#log.logger.info('Mongo connection reestablished')
})
this.#connection.on('disconnected', () => {
this.#log.logger.info('Mongo connection disconnected')
this.#log.logger.info('Trying to reconnected to Mongo...')
setTimeout(() => {
mongoose.connect(process.env.MONGO_URI as string, {
...connection,
connectTimeoutMS: 3000,
socketTimeoutMS: 3000
})
}, 3000)
})
this.#connection.on('close', () => {
this.#log.logger.info('Mongo connection closed')
})
this.#connection.on('error', (e: Error) => {
this.#log.logger.info('Mongo connection error:')
this.#log.logger.error(e)
})
await mongoose.connect(process.env.MONGO_URI as string, connection)
async #dbConnection() {
this.#connection = await dbConnection(this.#log.logger)
}

public start(): void {
this.#server = this.#app.listen(PORT, () => {
this.#log.logger.info(`Server running at port ${PORT}`)
})

public async start(): Promise<void> {
try {
this.#mongo()
await this.#dbConnection()
await this.#connection?.connect()
this.#server = this.#app.listen(PORT, () => {
this.#log.logger.info(`Server running at port ${PORT}`)
})
} catch (e) {
this.#log.logger.error(e)
}
}

public stop(): void {
public async stop(): Promise<void> {
try {
if (this.#server) this.#server.close()

process.exit(0)
await this.#connection?.disconnect()
this.#server?.close()
} catch (e) {
this.#log.logger.error(e)
}
Expand Down
Loading

0 comments on commit e1f8b32

Please sign in to comment.