From c9d59b2d2790764988de0a3220e26029f68c27ef Mon Sep 17 00:00:00 2001 From: Caleb Hill Date: Sat, 21 Sep 2024 10:40:30 -0600 Subject: [PATCH 1/2] backend logger is working --- server/src/index.ts | 14 +++----------- server/src/server.ts | 13 ++++--------- server/src/util/logger.ts | 38 ++++++++++++-------------------------- 3 files changed, 19 insertions(+), 46 deletions(-) diff --git a/server/src/index.ts b/server/src/index.ts index 43dbd7b..3c1a0ed 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,26 +1,18 @@ import server from './server' +import { logger } from './util/logger' async function run (): Promise { - console.log('2') - - let app - try { - app = await server() - } catch (e) { - console.error(e) - } - console.log('3') + const app = await server() await app?.listen({ host: '0.0.0.0', port: 8080 }) } -console.log('1') run() .then(r => {}) .catch(async err => { - // logger.error({ err }, 'Error in running app') + logger.error({ err }, 'Error in running app') // close any db stuff process.exit() }) diff --git a/server/src/server.ts b/server/src/server.ts index a1a74fb..743e2a6 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -13,17 +13,12 @@ import { UnauthenticatedClientError, sendBasicMessageResponse, } from './util/errors' -import { logger } from './util/logger' - -// const logger = l +import { logger, fastifyLogOpts } from './util/logger' export default async function server (): Promise { - console.log('11') - // const logger = mylogger() const fastify = Fastify({ - logger: true + logger: fastifyLogOpts }) - console.log('12') fastify.setValidatorCompiler(TypeBoxValidatorCompiler) @@ -57,10 +52,10 @@ export default async function server (): Promise { } else if (error instanceof ConflictError || error instanceof UnmodifiableError) { await sendBasicMessageResponse(409, reply, error.message) } else if (error instanceof DatabaseError) { - fastify.log.error({err: error}, 'Critical database error') + logger.error({err: error}, 'Critical database error') await sendBasicMessageResponse(500, reply, error.message) } else { - fastify.log.error({err: error}, 'Unknown internal server error') + logger.error({err: error}, 'Unknown internal server error') await sendBasicMessageResponse(error.statusCode ?? 500, reply, error.message) } } diff --git a/server/src/util/logger.ts b/server/src/util/logger.ts index 569fdbe..0a47d36 100644 --- a/server/src/util/logger.ts +++ b/server/src/util/logger.ts @@ -1,35 +1,21 @@ import { pino } from 'pino' -// export const logger = pino({ -// transport: { -// target: 'pino-pretty', -// options: { translateTime: 'UTC:yyyy-mm-dd\'T\'HH:MM:ss.l\'Z\'' } -// } -// }) const ENV_LEVELS: Record = { production: 'info', test: 'silent', default: 'debug' } -export function logger() { - const opts = { - level: ENV_LEVELS[process.env.NODE_ENV ?? 'default'], - messageKey: 'message', - formatters: { - level: (level: any) => ({ level }) // display the level not the number value of the level - }, - base: {}, // don't display the process pid, nor hostname - redact: { - // redact bearer tokens and JWTs - paths: ['req.headers.authorization', 'req.headers.assertion', 'req.headers["x-jwt-assertion"]', 'req.headers["x-jwt-assertion-original"]'], - censor: '***' - }, - transport: { - target: 'pino-pretty', - options: {translateTime: 'UTC:yyyy-mm-dd\'T\'HH:MM:ss.l\'Z\''} - } + +const loggerOptions = { + level: ENV_LEVELS[process.env.NODE_ENV ?? 'default'], + formatters: { + level: (level: any) => ({ level }) // display the level not the number value of the level + }, + base: {}, // don't display the process pid, nor hostname + transport: { + target: 'pino-pretty', + options: {translateTime: 'SYS:HH:MM:ss Z-07:00'} } - return pino(opts) } - -// export default logger; \ No newline at end of file +export const fastifyLogOpts = {...loggerOptions, messageKey: 'message' } +export const logger = pino(loggerOptions) From 4689f639859ada918bd1c0966450aec1a4d3d36d Mon Sep 17 00:00:00 2001 From: Caleb Hill Date: Sat, 21 Sep 2024 11:19:27 -0600 Subject: [PATCH 2/2] get shared typing working --- server/package-lock.json | 26 +++++++++++++++++++++++++- server/package.json | 3 ++- server/src/routes/add/root.ts | 10 ++++------ shared/package-lock.json | 6 ++++++ shared/package.json | 3 ++- shared/src/index.ts | 2 ++ shared/src/model/add.model.ts | 13 +++++++++++++ web/package-lock.json | 1 + web/package.json | 2 +- web/src/client/add.ts | 10 ++++------ web/src/components/main.tsx | 2 +- 11 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 shared/src/index.ts create mode 100644 shared/src/model/add.model.ts diff --git a/server/package-lock.json b/server/package-lock.json index f370009..83b972c 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -13,7 +13,8 @@ "@fastify/type-provider-typebox": "^5.0.0", "fastify": "^5.0.0", "pino": "^9.4.0", - "pino-pretty": "^11.2.2" + "pino-pretty": "^11.2.2", + "shared": "../shared" }, "devDependencies": { "@types/node": "^20.14.8", @@ -23,6 +24,25 @@ "typescript": "^5.6.2" } }, + "../shared": { + "version": "0.0.0", + "dependencies": { + "@fastify/autoload": "^5.7.1", + "@fastify/cors": "^8.3.0", + "@fastify/helmet": "^10.1.1", + "@fastify/sensible": "^5.2.0", + "@sinclair/typebox": "^0.33.12", + "fastify": "^4.18.0", + "fastify-cli": "^5.7.1", + "fastify-plugin": "^4.5.0", + "pino-pretty": "^10.0.0", + "vite-plugin-dts": "^3.3.1" + }, + "devDependencies": { + "typescript": "^5.0.2", + "vite": "^4.4.5" + } + }, "node_modules/@cspotcode/source-map-support": { "version": "0.8.1", "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", @@ -1037,6 +1057,10 @@ "resolved": "https://registry.npmjs.org/set-cookie-parser/-/set-cookie-parser-2.7.0.tgz", "integrity": "sha512-lXLOiqpkUumhRdFF3k1osNXCy9akgx/dyPZ5p8qAg9seJzXr5ZrlqZuWIMuY6ejOsVLE6flJ5/h3lsn57fQ/PQ==" }, + "node_modules/shared": { + "resolved": "../shared", + "link": true + }, "node_modules/shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", diff --git a/server/package.json b/server/package.json index ebb381e..a2d18e2 100644 --- a/server/package.json +++ b/server/package.json @@ -23,6 +23,7 @@ "@fastify/type-provider-typebox": "^5.0.0", "fastify": "^5.0.0", "pino": "^9.4.0", - "pino-pretty": "^11.2.2" + "pino-pretty": "^11.2.2", + "shared": "../shared" } } diff --git a/server/src/routes/add/root.ts b/server/src/routes/add/root.ts index 7027ca9..ae74ac2 100644 --- a/server/src/routes/add/root.ts +++ b/server/src/routes/add/root.ts @@ -1,4 +1,5 @@ -import { Type, TypeBoxTypeProvider, FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox' +import { TypeBoxTypeProvider, FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox' +import { AddResponseSchema, AddRequestSchema } from 'shared' // maybe use a generic argument for FastifyPluginAsync if we use options with fastify instance const add: FastifyPluginAsyncTypebox = async (fastifyApp, { }): Promise => { @@ -6,12 +7,9 @@ const add: FastifyPluginAsyncTypebox = async (fastifyApp, { }): Promise => fastify.get('/', { schema: { - querystring: Type.Object({ - a: Type.Number(), - b: Type.Number() - }), + querystring: AddRequestSchema, response: { - 200: Type.Object({result: Type.Number()}) + 200: AddResponseSchema } } }, async (request, reply) => { diff --git a/shared/package-lock.json b/shared/package-lock.json index 2b88fe1..264f362 100644 --- a/shared/package-lock.json +++ b/shared/package-lock.json @@ -12,6 +12,7 @@ "@fastify/cors": "^8.3.0", "@fastify/helmet": "^10.1.1", "@fastify/sensible": "^5.2.0", + "@sinclair/typebox": "^0.33.12", "fastify": "^4.18.0", "fastify-cli": "^5.7.1", "fastify-plugin": "^4.5.0", @@ -681,6 +682,11 @@ "string-argv": "~0.3.1" } }, + "node_modules/@sinclair/typebox": { + "version": "0.33.12", + "resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.33.12.tgz", + "integrity": "sha512-d5KrXIdPolLp8VpGpZAQvEz8ioVtFlUQSyCIS2sEBi7FKhceIB7nj9BlNfqqvp5wmOfg8v8bP1rAvYYkjz21/Q==" + }, "node_modules/@types/argparse": { "version": "1.0.38", "resolved": "https://registry.npmjs.org/@types/argparse/-/argparse-1.0.38.tgz", diff --git a/shared/package.json b/shared/package.json index b9ea054..472509c 100644 --- a/shared/package.json +++ b/shared/package.json @@ -16,6 +16,7 @@ "@fastify/cors": "^8.3.0", "@fastify/helmet": "^10.1.1", "@fastify/sensible": "^5.2.0", + "@sinclair/typebox": "^0.33.12", "fastify": "^4.18.0", "fastify-cli": "^5.7.1", "fastify-plugin": "^4.5.0", @@ -26,4 +27,4 @@ "typescript": "^5.0.2", "vite": "^4.4.5" } -} \ No newline at end of file +} diff --git a/shared/src/index.ts b/shared/src/index.ts new file mode 100644 index 0000000..f9f1287 --- /dev/null +++ b/shared/src/index.ts @@ -0,0 +1,2 @@ + // Add +export { AddResponseSchema, AddRequestSchema, type AddRequest, type AddResponse } from './model/add.model' diff --git a/shared/src/model/add.model.ts b/shared/src/model/add.model.ts new file mode 100644 index 0000000..7d26694 --- /dev/null +++ b/shared/src/model/add.model.ts @@ -0,0 +1,13 @@ +import { Static, Type } from '@sinclair/typebox' + +export const AddResponseSchema = Type.Object({ + result: Type.Number() +}) +export type AddResponse = Static + +export const AddRequestSchema = Type.Object({ + a: Type.Number(), + b: Type.Number() +}) +export type AddRequest = Static + diff --git a/web/package-lock.json b/web/package-lock.json index ac9350d..c35ba07 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -58,6 +58,7 @@ "@fastify/cors": "^8.3.0", "@fastify/helmet": "^10.1.1", "@fastify/sensible": "^5.2.0", + "@sinclair/typebox": "^0.33.12", "fastify": "^4.18.0", "fastify-cli": "^5.7.1", "fastify-plugin": "^4.5.0", diff --git a/web/package.json b/web/package.json index 3419778..aa84f4a 100644 --- a/web/package.json +++ b/web/package.json @@ -24,7 +24,7 @@ "web-vitals": "^2.1.4" }, "scripts": { - "start": "vite", + "dev": "vite", "build": "tsc && vite build", "serve": "vite preview", "reload": "npm run build && npm run serve", diff --git a/web/src/client/add.ts b/web/src/client/add.ts index a227ed3..be7d9d6 100644 --- a/web/src/client/add.ts +++ b/web/src/client/add.ts @@ -1,8 +1,6 @@ import {Client} from "./client"; +import { AddRequest } from 'shared' -export async function add(client: Client, a: number, b: number): Promise { - return await client.get('/add', { - a, - b - }) -} \ No newline at end of file +export async function add(client: Client, params: AddRequest): Promise { + return await client.get('/add', params) +} diff --git a/web/src/components/main.tsx b/web/src/components/main.tsx index f0c64d7..52aa124 100644 --- a/web/src/components/main.tsx +++ b/web/src/components/main.tsx @@ -17,7 +17,7 @@ export const Main = memo(() => { if (!client) { return } - const response = await add(client, 1, 2); + const response = await add(client, {a:1, b:2}); return response; });