diff --git a/api/src/app.ts b/api/src/app.ts index c371d69a..2ab40bdf 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -67,5 +67,8 @@ export const createApp = (): { app: Express, router: express.Router } => { } ); + const basePath = process.env.BASE_PATH || '/'; + app.use(basePath, router); + return { app, router }; }; diff --git a/api/src/graphql/server/index.ts b/api/src/graphql/server/index.ts index 784a7b4f..51cbe4d7 100644 --- a/api/src/graphql/server/index.ts +++ b/api/src/graphql/server/index.ts @@ -1,22 +1,14 @@ -import type { Server } from 'http'; -import { createServer as createHttpServer } from 'http'; + import type express from 'express'; import { ApolloServerPlugin, ApolloServer } from '@apollo/server'; import { expressMiddleware } from '@apollo/server/express4'; import bodyParser from 'body-parser'; import cookierParser from 'cookie-parser'; import cors from 'cors'; -import { PORT } from '../../env'; import { schemaWithMiddleware as indexSchema } from './index/schema'; import { schemaWithMiddleware as driveSchema } from './drive/schema'; import { Context, Context as IndexContext, createContext as createIndexContext } from './index/context'; import { Context as DriveContext, createContext as createDriveContext } from './drive/context'; -import { getChildLogger } from '../../logger'; -import "express-async-errors"; -import { errorHandler } from '../../middleware/errors'; -import * as Sentry from "@sentry/node"; -import { initRedis } from '../../redis'; -const logger = getChildLogger({ msgPrefix: 'SERVER' }); function loggerPlugin(): ApolloServerPlugin { return { @@ -44,24 +36,15 @@ const createApolloDriveServer = (): ApolloServer => new ApolloServ plugins: [loggerPlugin()], }); -export const startServer = async ( - app: express.Application, +export const addGraphqlRoutes = async ( router: express.Router, -): Promise => { - logger.debug('Starting server'); - - if (process.env.REDIS_TLS_URL) { - await initRedis(); - } - +) => { const apolloIndex = createApolloIndexServer(); const apolloDrive = createApolloDriveServer(); await apolloIndex.start(); await apolloDrive.start(); - - router.use( '/drives', cors(), @@ -81,16 +64,4 @@ export const startServer = async ( context: async (params) => createDriveContext(params), }), ); - - const basePath = process.env.BASE_PATH || '/'; - app.use(basePath, router); - app.use(errorHandler); - if (process.env.SENTRY_DSN) { - app.use(Sentry.Handlers.errorHandler()); - } - - const httpServer = createHttpServer(app); - return httpServer.listen({ port: PORT }, () => { - logger.info(`Running on ${PORT}`); - }); }; diff --git a/api/src/index.ts b/api/src/index.ts index 29f6bdb5..52967ef0 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -1,12 +1,47 @@ +import type express from 'express'; +import * as Sentry from "@sentry/node"; import { createApp } from './app'; -import { startServer } from './graphql/server'; +import { addGraphqlRoutes } from './graphql/server'; import { getChildLogger } from './logger'; import { closeRedis } from './redis'; +import { Server, createServer as createHttpServer } from 'http'; +import { PORT } from './env'; +import { errorHandler } from './middleware/errors'; +import { initRedis } from './redis'; +import "express-async-errors"; const logger = getChildLogger({ msgPrefix: 'SERVER' }); const { app, router } = createApp(); +async function startServer( + app: express.Application, + router: express.Router, +): Promise { + await addGraphqlRoutes(router); + + logger.debug('Starting server'); + + if (process.env.REDIS_TLS_URL) { + await initRedis(); + } + + const basePath = process.env.BASE_PATH || '/'; + app.use(basePath, router); + + if (process.env.SENTRY_DSN) { + app.use(Sentry.Handlers.errorHandler()); + } + + app.use(errorHandler); + + const httpServer = createHttpServer(app); + return httpServer.listen({ port: PORT }, () => { + logger.info(`Running on ${PORT}`); + }); +} + + /* istanbul ignore next @preserve */ startServer(app, router) .then((e) => { diff --git a/api/src/logger.ts b/api/src/logger.ts index fddedda2..c0c87a6b 100644 --- a/api/src/logger.ts +++ b/api/src/logger.ts @@ -53,6 +53,7 @@ const transport = process.env.SENTRY_DSN ? { options: { sentry: { dsn: process.env.SENTRY_DSN, + environment: process.env.SENTRY_ENV ?? 'dev', ignoreErrors: [/Transmitter .+ not found/, /^Failed to fetch strands$/, /Drive with id .+ not found/], // additional options for sentry }, diff --git a/api/src/middleware/errors.ts b/api/src/middleware/errors.ts index f789b493..bdef4ddc 100644 --- a/api/src/middleware/errors.ts +++ b/api/src/middleware/errors.ts @@ -1,14 +1,11 @@ import { NextFunction, Request, Response } from "express"; import { getChildLogger } from "../logger"; -import { CustomError } from "../errors/CustomError"; const logger = getChildLogger({ msgPrefix: 'Generic Error Handler', }); export const errorHandler = (err: Error, req: Request, res: Response, next: NextFunction) => { + const errorId = (res as { sentry?: string }).sentry; err.cause = err.cause || 'Unknown'; - logger.error({ - msg: err.message, - }); - - res.status(500).send({ errors: err.message }); + logger.error(err, err.message, { errorId }); + res.status(500).send({ errors: err.message, errorId }); };