Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
},
"dependencies": {
"@prisma/client": "^6.4.1",
"fastify": "^5.2.1",
"fastify": "^5.3.2",
"dotenv": "^16.5.0",
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Since dotenv is primarily for local development, consider moving it to devDependencies if your production environments inject variables through the environment directly.

Copilot uses AI. Check for mistakes.
"zod": "^3.24.2"
}
}
28 changes: 28 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fastify, { type FastifyReply } from 'fastify'
import { ZodError } from 'zod'
import { env } from './env'

export const app = fastify()

app.get('/', (_, reply: FastifyReply) => {
return reply.status(200).send({
message: 'Hello, World!',
})
})

app.setErrorHandler((error, _, reply) => {
if (error instanceof ZodError) {
return reply
.status(400)
.send({ message: 'Validation error.', issues: error.format() })
}

if (env.NODE_ENV !== 'prod') {
console.error(error)
} else {
console.error(error)
Comment on lines +20 to +23
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The console.error call is duplicated in both branches; consider logging the error once outside the condition and then adding any external logging only in the 'prod' branch to reduce duplication.

Suggested change
if (env.NODE_ENV !== 'prod') {
console.error(error)
} else {
console.error(error)
console.error(error)
if (env.NODE_ENV === 'prod') {

Copilot uses AI. Check for mistakes.
// TODO: Here we should log to an external tool like DataDog/NewRelic/Sentry
}

return reply.status(500).send({ message: 'Internal server error.' })
})
17 changes: 17 additions & 0 deletions src/env/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'dotenv/config'
import { z } from 'zod'

const envSchema = z.object({
PORT: z.coerce.number().min(1000).max(9999).default(3333),
NODE_ENV: z.enum(['dev', 'test', 'prod']).default('dev'),
})

const _env = envSchema.safeParse(process.env)

if (_env.success === false) {
console.error('Invalid environment variables', _env.error.format())

throw new Error('Invalid environment variables.')
}

export const env = _env.data
7 changes: 7 additions & 0 deletions src/lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { env } from '@/env'
import { PrismaClient } from '@prisma/client'

export const prisma = new PrismaClient({
log: env.NODE_ENV === 'dev' ? ['error', 'info', 'query', 'warn'] : [],
errorFormat: 'pretty',
})
15 changes: 4 additions & 11 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import fastify, { type FastifyReply } from 'fastify'

const app = fastify()

app.get('/', (_, reply: FastifyReply) => {
return reply.status(200).send({
message: 'Hello, World!',
})
})
import { app } from './app'
import { env } from './env'

app
.listen({
host: '0.0.0.0',
port: 3333,
port: env.PORT,
})
.then(() => {
console.log('Server Running on 3333!')
console.log(`Server Running on ${env.PORT}!`)
})