Skip to content

Commit

Permalink
Add cors
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBroadbent committed Dec 4, 2023
1 parent e6efd35 commit e41f21a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"max-len": ["error", { "code": 120, "tabWidth": 2 }],
"no-extra-semi": "error",
"no-unexpected-multiline": "error",
"semi": ["off", "never"],
"operator-linebreak": ["error", "before"]
"semi": ["off", "never"]
},
"overrides": [
{
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
"openapi:generate-internal": "redocly bundle internal -o ./src/config/openapi-internal.bundle.json",
"openapi:generate-types": "openapi-typescript ./src/config/openapi-internal.bundle.json --output ./src/types/generated/openapi-schema.ts",
"openapi:validate": "redocly lint --lint-config error",
"docs:preview-public": "redocly preview-docs -p 3000 src/config/openapi.bundle.json",
"docs:preview-internal": "redocly preview-docs -p 3000 src/config/openapi-internal.bundle.json",
"docs:preview": "concurrently \"http-server docs/public\" \"open http://127.0.0.1:8080\""
},
"repository": {
Expand Down Expand Up @@ -56,6 +54,7 @@
"typescript": "5.2.2"
},
"dependencies": {
"@fastify/cors": "^8.4.1",
"dotenv-flow": "3.3.0",
"fastify": "4.23.2",
"fastify-openapi-glue": "2.6.7",
Expand Down
39 changes: 36 additions & 3 deletions src/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@
import fastify, { FastifyInstance, FastifyServerOptions } from "fastify"
import cors from "@fastify/cors"
import fastify, { FastifyInstance, FastifyReply, FastifyRequest, FastifyServerOptions } from "fastify"

import { errorHandler } from "./error-handler"
import { registerAPIRoutes } from "./routes"

export const getServer = async (options: FastifyServerOptions = {}): Promise<FastifyInstance> => {
const server = fastify(options).setErrorHandler(errorHandler)
const logRequests = (server: FastifyInstance) => {
server.addHook("preHandler", (req: FastifyRequest, rep: FastifyReply, done) => {
if (req.body) req.log.info({ body: req.body }, "request body")
done()
})
}

const logResponses = (server: FastifyInstance) => {
server.addHook("onSend", (req: FastifyRequest, rep: FastifyReply, payload: string, done) => {
const contentType = rep.getHeader("Content-Type")
if (
payload &&
(typeof contentType === "string" || Array.isArray(contentType)) &&
contentType.includes("application/json")
) {
req.log.info({ payload: JSON.parse(payload) }, "response body")
}

done()
})
}

const logFatalError = (server: FastifyInstance) => {
process.on("uncaughtExceptionMonitor", (error, origin) => {
server.log.fatal(error, origin)
})
}

export const getServer = async (options: FastifyServerOptions = {}): Promise<FastifyInstance> => {
const server = fastify(options).setErrorHandler(errorHandler)

server.register(cors, {
origin: "*",
})

logRequests(server)
logResponses(server)
logFatalError(server)

await registerAPIRoutes(server)

Expand Down

0 comments on commit e41f21a

Please sign in to comment.