Skip to content
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

Feat(api): Fastify api (#175) #176

Merged
merged 2 commits into from
Nov 24, 2023
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
104 changes: 104 additions & 0 deletions apps/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
56 changes: 56 additions & 0 deletions apps/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# FROM node:16

# ENV port=8090
# COPY . .

# RUN yarn
# RUN yarn build

# EXPOSE 8090
# CMD [ "node", "./dist/main.js" ]

FROM node:18 AS base

# The web Dockerfile is copy-pasted into our main docs at /docs/handbook/deploying-with-docker.
# Make sure you update this Dockerfile, the Dockerfile in the web workspace and copy that over to Dockerfile in the docs.

FROM base AS builder

# Set working directory
WORKDIR /app
RUN yarn global add turbo
COPY . .
RUN turbo prune --scope=api --docker


# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
WORKDIR /app

# First install dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/yarn.lock ./yarn.lock
RUN yarn install


# Build the project and its dependencies
COPY --from=builder /app/out/full/ .
RUN ls
COPY turbo.json /../../turbo.json

# RUN rm -f apps/api/.env

RUN yarn turbo run build --filter=database

FROM base AS runner
WORKDIR /app

# Don't run production as root
# RUN addgroup --system --gid 1001 prod
# RUN adduser --system --uid 1001 prod
# USER prod
COPY --from=installer /app .

EXPOSE 8090
CMD ["node", "apps/api/dist/"]
21 changes: 21 additions & 0 deletions apps/api/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Satish Babariya

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions apps/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# api

Clean Architecture for node.js projects using fastify and prisma

```
src
│ main.ts # Application entry point\
└───controllers # route controllers for all the endpoints of the app
└───middlewares # route middleware
└───prisma # here lies prisma schema and migrations
└───types # Type declaration files (d.ts) for Typescript


```
38 changes: 38 additions & 0 deletions apps/api/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: "3.3"
services:
pg:
image: "postgres:11"
restart: unless-stopped
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres
container_name: "pg"
ports:
- '5432:5432'
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- internal

api:
build: .
container_name: api
restart: always
depends_on:
- pg
ports:
- "8090:8090"
environment:
- DATABASE_URL=postgres://postgres:postgres@pg:5432/postgres
- JWT_SECRET=secret
- PORT=8090
networks:
- internal

volumes:
postgres_data:

networks:
internal:
driver: bridge
46 changes: 46 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "api",
"version": "1.0.0",
"main": "dist/main.js",
"repository": "",
"author": "Jack Andrews",
"license": "MIT",
"scripts": {
"postinstall": "prisma generate",
"db:push": "prisma db push --accept-data-loss",
"db:migrate": "prisma migrate dev",
"build": "tsc",
"dev": "ts-node-dev --respawn src/main.ts"
},
"devDependencies": {
"@types/bcrypt": "^5.0.0",
"@types/jsonwebtoken": "^8.5.8",
"@types/node": "^17.0.23",
"@types/passport-local": "^1.0.35",
"prisma": "5.2.0",
"ts-node": "^10.7.0",
"ts-node-dev": "^2.0.0",
"typescript": "^5.3.2"
},
"dependencies": {
"@fastify/cookie": "^9.0.4",
"@fastify/cors": "^8.3.0",
"@fastify/session": "^10.4.0",
"@fastify/swagger": "^8.10.0",
"@prisma/client": "^5.2.0",
"axios": "^1.5.0",
"bcrypt": "^5.0.1",
"dotenv": "^16.0.0",
"fastify": "4.22.2",
"got": "^13.0.0",
"i": "^0.3.7",
"imap": "^0.8.19",
"jsonwebtoken": "9.0.2",
"mailparser": "^3.6.5",
"prisma": "^5.6.0"
},
"prisma": {
"schema": "./src/prisma/schema.prisma",
"seed": "node ./src/prisma/seed.js"
}
}
Loading