Skip to content

Commit

Permalink
Merge pull request #40 from deptagency/aj/setup-scribe-http-server
Browse files Browse the repository at this point in the history
Setting up basic http on scribe, with health check endpoint (dupe)
  • Loading branch information
eremzeit committed Feb 18, 2022
2 parents 734ba15 + f3d07c1 commit d1fbae7
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 43 deletions.
2 changes: 2 additions & 0 deletions apps/api/src/api/build-app.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { generateHealthRoutes } from '@algomart/shared/modules'
import {
fastifyContainerPlugin,
fastifyKnexPlugin,
Expand Down Expand Up @@ -77,6 +78,7 @@ export default async function buildApp(config: AppConfig) {
// no hooks yet

// Services
await app.register(generateHealthRoutes(), { prefix: '/health' })
await app.register(accountsRoutes, { prefix: '/accounts' })
await app.register(auctionsRoutes, { prefix: '/auctions' })
await app.register(bidsRoutes, { prefix: '/bids' })
Expand Down
4 changes: 4 additions & 0 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ buildApp({
configureTasks(app)
return app.listen(Configuration.port, Configuration.host)
})
.then(() => {
const addr = `${Configuration.host}:${Configuration.port}`
logger.info(`API service is listening at ${addr}`)
})
.catch((error) => {
logger.error(error)
throw error
Expand Down
34 changes: 10 additions & 24 deletions apps/scribe/src/build-app.ts → apps/scribe/src/app/build-app.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
import {
fastifyContainerPlugin,
fastifyKnexPlugin,
fastifyTransactionPlugin,
} from '@algomart/shared/plugins'
import { DependencyResolver } from '@algomart/shared/utils'
import ajvCompiler from '@fastify/ajv-compiler'
import ajvFormats from 'ajv-formats'
import fastify, { FastifyServerOptions } from 'fastify'
import { fastifySchedule } from 'fastify-schedule'
import fastifySensible from 'fastify-sensible'
import fastifySwagger from 'fastify-swagger'

import swaggerOptions from './configuration/swagger'
// import { accountsRoutes } from '@scribe/modules/accounts'
// import { auctionsRoutes } from '@scribe/modules/auctions'
// import { bidsRoutes } from '@scribe/modules/bids'
// import { collectiblesRoutes } from '@scribe/modules/collectibles'
// import { collectionsRoutes } from '@scribe/modules/collections'
// import { faqsRoutes } from '@scribe/modules/faqs'
// import { homepageRoutes } from '@scribe/modules/homepage'
// import { languagesRoutes } from '@scribe/modules/languages'
// import { packsRoutes } from '@scribe/modules/packs'
// import { pageRoute } from '@scribe/modules/pages'
// import { paymentRoutes } from '@scribe/modules/payments'
// import { setsRoutes } from '@scribe/modules/sets'
// import fastifyKnex from '@scribe/plugins/knex.plugin'

import {
fastifyContainerPlugin,
fastifyTransactionPlugin,
} from '@algomart/shared/plugins'

import { DependencyResolver } from '@algomart/shared/utils'
import swaggerOptions from '../configuration/swagger'
import { generateHealthRoutes } from '@algomart/shared/modules'

export interface AppConfig {
//// knex: Knex.Config
// knex: Knex.Config
fastify?: FastifyServerOptions
container: DependencyResolver
}
Expand Down Expand Up @@ -67,7 +54,6 @@ export default async function buildApp(config: AppConfig) {
await app.register(fastifySensible)

// Our Plugins
/////await app.register(fastifyKnex, { knex: config.knex })
await app.register(fastifyContainerPlugin, { container: config.container })
await app.register(fastifyTransactionPlugin)

Expand All @@ -78,7 +64,7 @@ export default async function buildApp(config: AppConfig) {
// no hooks yet

// Services
// TODO: implement routes to handle data changed callbacks from CMS
await app.register(generateHealthRoutes(), { prefix: '/health' })

return app
}
2 changes: 1 addition & 1 deletion apps/scribe/src/configuration/configure-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DependencyResolver } from '@algomart/shared/utils'
import { Configuration } from './'
import { MailerAdapter } from '@algomart/shared/adapters'
import { logger } from '../utils/logger'
import { logger } from './logger'

export function configureResolver() {
const resolver = new DependencyResolver()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Configuration } from '../configuration'

import { createLogger } from '@algomart/shared/utils'

import { Configuration } from './'

/**
* Only use this logger if you do not have access to a Fastify request.
*/
Expand Down
10 changes: 7 additions & 3 deletions apps/scribe/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import buildApp from './build-app'
import buildApp from './app/build-app'
import { Configuration } from './configuration'
import { configureTasks } from './tasks'
import { configureResolver } from './configuration/configure-resolver'
import { logger } from './utils/logger'
import { logger } from './configuration/logger'

buildApp({
fastify: {
Expand All @@ -14,7 +14,11 @@ buildApp({
configureTasks(app)
return app.listen(Configuration.port, Configuration.host)
})
.then(() => {
const addr = `${Configuration.host}:${Configuration.port}`
logger.info(`SCRIBE service is listening at ${addr}`)
})
.catch((error) => {
logger.error(error)
logger.error(error, 'Scribe service error')
throw error
})
13 changes: 0 additions & 13 deletions apps/scribe/src/main.ts

This file was deleted.

3 changes: 3 additions & 0 deletions libs/shared/modules/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]]
}
18 changes: 18 additions & 0 deletions libs/shared/modules/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions libs/shared/modules/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# shared-modules

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test shared-modules` to execute the unit tests via [Jest](https://jestjs.io).
15 changes: 15 additions & 0 deletions libs/shared/modules/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
displayName: 'shared-modules',
preset: '../../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../../coverage/libs/shared/modules',
}
23 changes: 23 additions & 0 deletions libs/shared/modules/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"root": "libs/shared/modules",
"sourceRoot": "libs/shared/modules/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["libs/shared/modules/**/*.ts"]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/libs/shared/modules"],
"options": {
"jestConfig": "libs/shared/modules/jest.config.js",
"passWithNoTests": true
}
}
},
"tags": []
}
1 change: 1 addition & 0 deletions libs/shared/modules/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/health'
22 changes: 22 additions & 0 deletions libs/shared/modules/src/lib/health/health.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { verify } from 'crypto'
import { FastifyReply, FastifyRequest } from 'fastify'

/**
* Used to generate a route for checking health status
*
* @param verifyFn - (optional) pass in a custom function that can implement
* whatever custom checks makes sense for your particular service (eg. checking a
* db connection, etc)
*/
export function generateHealthRoute(verifyFn?: () => Promise<string>) {
return async (request: FastifyRequest<unknown>, reply: FastifyReply) => {
verifyFn = verifyFn ?? (async () => undefined)

const errorStr = await verifyFn()
if (errorStr ?? undefined === undefined) {
reply.send({ status: 'healthy' })
}

reply.status(500).send({ status: `Service Unhealthy: ${errorStr}` })
}
}
9 changes: 9 additions & 0 deletions libs/shared/modules/src/lib/health/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { generateHealthRoute } from './health.routes'
export * from './health.routes'
import { FastifyInstance } from 'fastify'

export function generateHealthRoutes(verifyFn?: () => Promise<string>) {
return async (app: FastifyInstance) => {
app.get('/', {}, generateHealthRoute(verifyFn))
}
}
13 changes: 13 additions & 0 deletions libs/shared/modules/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
11 changes: 11 additions & 0 deletions libs/shared/modules/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["**/*.spec.ts", "**/*.test.ts"],
"include": ["**/*.ts"]
}
19 changes: 19 additions & 0 deletions libs/shared/modules/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"**/*.test.ts",
"**/*.spec.ts",
"**/*.test.tsx",
"**/*.spec.tsx",
"**/*.test.js",
"**/*.spec.js",
"**/*.test.jsx",
"**/*.spec.jsx",
"**/*.d.ts"
]
}
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@algomart/schemas": ["libs/schemas/src/index.ts"],
"@algomart/shared/adapters": ["libs/shared/adapters/src/index.ts"],
"@algomart/shared/models": ["libs/shared/models/src/index.ts"],
"@algomart/shared/modules": ["libs/shared/modules/src/index.ts"],
"@algomart/shared/plugins": ["libs/shared/plugins/src/index.ts"],
"@algomart/shared/services": ["libs/shared/services/src/index.ts"],
"@algomart/shared/utils": ["libs/shared/utils/src/index.ts"],
Expand Down
1 change: 1 addition & 0 deletions workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scribe": "apps/scribe",
"shared-adapters": "libs/shared/adapters",
"shared-models": "libs/shared/models",
"shared-modules": "libs/shared/modules",
"shared-plugins": "libs/shared/plugins",
"shared-services": "libs/shared/services",
"shared-utils": "libs/shared/utils",
Expand Down

0 comments on commit d1fbae7

Please sign in to comment.