Skip to content

Commit

Permalink
minor change
Browse files Browse the repository at this point in the history
  • Loading branch information
tinchoz49 committed Oct 9, 2024
1 parent 30c0dd4 commit 5b079b9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ DATABASE_IN_MEMORY=true
# DATABASE_PORT=5432
# DATABASE_USER=your_username
# DATABASE_PASSWORD=your_password
# DATABASE_DB=your_database_name
# DATABASE_NAME=your_database_name
# DATABASE_PATH=/optional/path
# DATABASE_SSL=false
# DATABASE_MAX=10
Expand Down
23 changes: 14 additions & 9 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import type { Server } from 'node:http'
import type { Server as HttpServer } from 'node:http'
import type { Server as HttpsServer } from 'node:https'

import path from 'node:path'

import type { Static } from '@sinclair/typebox'

import autoload from '@fastify/autoload'
import fastifyCors from '@fastify/cors'
import fastifyHelmet from '@fastify/helmet'
import { type TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
import { serverFactory } from '@geut/fastify-uws'
import fastify, { type FastifyHttpOptions } from 'fastify'
import errorPlugin, { type BetterErrorPlugin } from 'fastify-better-error'
import fastify, { type FastifyHttpOptions, type FastifyHttpsOptions } from 'fastify'
import { type BetterErrorPlugin } from 'fastify-better-error'
import { type DeepPartial, parseEnv } from 'typebox-env'

import EnvSchema from './env.js'
import * as errors from './errors.js'

export interface AppOptions extends FastifyHttpOptions<Server> {
export interface AppOptions extends FastifyHttpOptions<HttpServer> {
https?: FastifyHttpsOptions<HttpsServer>['https']
env?: DeepPartial<Static<typeof EnvSchema>>
}

export const createApp = (options: AppOptions = {}) => {
const hrstart = process.hrtime()

const { env: envOptions = {}, ...fastifyOptions } = options

const env = parseEnv(EnvSchema, {
Expand Down Expand Up @@ -49,9 +51,9 @@ export const createApp = (options: AppOptions = {}) => {
app.decorate('env', env)

// fastify plugins
app.register(errorPlugin, { errors })
app.register(fastifyCors)
app.register(fastifyHelmet)
app.register(import('fastify-better-error'), { errors })
app.register(import('@fastify/cors'))
app.register(import('@fastify/helmet'))

// user plugins
app.register(autoload, {
Expand Down Expand Up @@ -81,6 +83,9 @@ export const createApp = (options: AppOptions = {}) => {
if (env.DATABASE.RUN_SEED) {
await app.drizzle.seed()
}

const hrend = process.hrtime(hrstart)
app.log.info('Execution time (hr): %ds %dms', hrend[0], hrend[1] / 1000000)
})

return app
Expand Down
2 changes: 1 addition & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default Type.Object({
PORT: Type.Number(),
USER: Type.String(),
PASSWORD: Type.String(),
DB: Type.String(),
NAME: Type.String(),
PATH: Type.Optional(Type.String()),
SSL: Type.Optional(Type.Boolean()),
MAX: Type.Optional(Type.Number()),
Expand Down
7 changes: 2 additions & 5 deletions src/plugins/auth.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type { FastifyRequest } from 'fastify'

import fastifyAuth from '@fastify/auth'
import fastifyJwt from '@fastify/jwt'
import fp from 'fastify-plugin'

import type { App } from '~/app.js'

async function authPlugin(app: App) {
app.register(fastifyAuth)

app.register(fastifyJwt, {
app.register(import('@fastify/auth'))
app.register(import('@fastify/jwt'), {
secret: app.env.JWT.SECRET,
verify: {
cache: app.env.JWT.CACHE,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/drizzle.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function drizzlePlugin(app: App) {
port: env.PORT,
user: env.USER,
password: env.PASSWORD,
database: env.DB,
database: env.NAME,
path: env.PATH,
ssl: env.SSL,
max: env.MAX,
Expand Down

0 comments on commit 5b079b9

Please sign in to comment.