-
Notifications
You must be signed in to change notification settings - Fork 0
Atualizar branch com versão mais atual da main #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5dace59
63f8ec5
8cc88c1
e42c382
e689611
c578904
6608d8c
096269e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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
|
||||||||||||||
| if (env.NODE_ENV !== 'prod') { | |
| console.error(error) | |
| } else { | |
| console.error(error) | |
| console.error(error) | |
| if (env.NODE_ENV === 'prod') { |
| 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 |
| 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', | ||
| }) |
| 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}!`) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Since
dotenvis primarily for local development, consider moving it todevDependenciesif your production environments inject variables through the environment directly.