Skip to content

Commit

Permalink
refactor: dockerfile deploy (#187)
Browse files Browse the repository at this point in the history
* refactor: dockerfile deploy

* fix: missing .prisma client

---------

Co-authored-by: pengap <penganpingprivte@gmail.com>
  • Loading branch information
Pengap and Pengap committed Oct 11, 2023
1 parent 013f2a7 commit b0317f8
Show file tree
Hide file tree
Showing 26 changed files with 863 additions and 5,996 deletions.
7 changes: 4 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
node_modules
**/node_modules
dist
**/.next

# All secrets
**/.env.local
Expand Down Expand Up @@ -35,9 +36,9 @@ tsconfig.tsbuildinfo
# Docker related
.dockerignore
Dockerfile
dockers/docker-compose.*.yml
dockers/docker-compose.yml
docker
docker-compose.yml
docker-compose.*.yml
dockers/**

# Log files
logs
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
matrix:
node-version: [18.x]
database-type: [postgres, sqlite]
env:
CI: 1

steps:
- uses: actions/checkout@v4
Expand Down
44 changes: 33 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,20 @@ DOCKER_COMPOSE_ARGS := DOCKER_UID=$(shell id -u) \
NETWORK_MODE=$(NETWORK_MODE)


define print_db_options
define print_db_mode_options
@echo -e "\nSelect a database to start."
@echo -e "\n\tsqlite Lightweight embedded, ideal for mobile and embedded systems, simple, resource-efficient, "
@echo -e "\t\t\t\teasy integration (default database)"
@echo -e "\tpostges(pg) Powerful and scalable, suitable for complex enterprise needs, highly customizable, rich community support\n"
endef

.PHONY: db-mode sqlite-mode postgres-mode
define print_db_push_options
@echo -e "The 'db pull' command connects to your database and adds Prisma models to your Prisma schema that reflect the current database schema.\n"
@echo -e "0) sqlite"
@echo -e "1) postges(pg)\n"
endef

.PHONY: db-mode sqlite-mode postgres-mode gen-prisma-schema gen-sqlite-prisma-schema gen-postgres-prisma-schema
.DEFAULT_GOAL := help

docker.create.network:
Expand Down Expand Up @@ -170,20 +176,36 @@ postgres.integration.test: docker.create.network
make postgres-mode && \
yarn workspace @teable-group/backend test:e2e'

gen-sqlite-prisma-schema: ## Generate the 'sqlite' version of the 'schema.prisma' file(alias 'gspc')
gen-sqlite-prisma-schema:
@cd ./packages/db-main-prisma; \
echo '{ "PRISMA_PROVIDER": "sqlite" }' | yarn mustache - ./prisma/schema.mustache > ./prisma/sqlite/schema.prisma
echo '{ "PRISMA_PROVIDER": "sqlite" }' | yarn mustache - ./prisma/template.prisma > ./prisma/sqlite/schema.prisma
@echo 'generate【 prisma/sqlite/schema.prisma 】success.'

gen-postgres-prisma-schema: ## Generate the 'postgres' version of the 'schema.prisma' file(alias 'gpps')
gen-postgres-prisma-schema:
@cd ./packages/db-main-prisma; \
echo '{ "PRISMA_PROVIDER": "postgres" }' | yarn mustache - ./prisma/schema.mustache > ./prisma/postgres/schema.prisma
echo '{ "PRISMA_PROVIDER": "postgres" }' | yarn mustache - ./prisma/template.prisma > ./prisma/postgres/schema.prisma
@echo 'generate【 prisma/postgres/schema.prisma 】success.'

gen-prisma-schema: gen-sqlite-prisma-schema gen-postgres-prisma-schema ## Generate 'schema.prisma' files for all versions of the system
gspc: gen-sqlite-prisma-schema
gpps: gen-postgres-prisma-schema
gen-prisma-schema: gen-sqlite-prisma-schema gen-postgres-prisma-schema ## Generate 'schema.prisma' files for all versions of the system

sqlite-db-push: ## db-push by sqlite
@cd ./packages/db-main-prisma; \
yarn prisma-db-push --schema ./prisma/sqlite/schema.prisma

postgres-db-push: ## db-push by postgres
@cd ./packages/db-main-prisma; \
yarn prisma-db-push --schema ./prisma/postgres/schema.prisma

db-push: ## connects to your database and adds Prisma models to your Prisma schema that reflect the current database schema.
$(print_db_push_options)
@read -p "Enter a command: " command; \
if [ "$$command" = "0" ] || [ "$$command" = "sqlite" ]; then \
make gen-sqlite-prisma-schema; \
make sqlite-db-push; \
elif [ "$$command" = "1" ] || [ "$$command" = "postges" ] || [ "$$command" = "pg" ]; then \
make gen-postgres-prisma-schema; \
make postgres-db-push; \
else echo "Unknown command."; fi

sqlite-mode: ## sqlite-mode
@make gen-sqlite-prisma-schema
Expand All @@ -198,7 +220,7 @@ postgres-mode: ## postgres-mode
yarn prisma-migrate deploy --schema ./prisma/postgres/schema.prisma

db-mode: ## db-mode
$(print_db_options)
$(print_db_mode_options)
@read -p "Enter a command: " command; \
if [ "$$command" = "sqlite" ]; then make sqlite-mode; \
elif [ "$$command" = "postges" ] || [ "$$command" = "pg" ]; then \
Expand All @@ -208,4 +230,4 @@ db-mode: ## db-mode
else echo "Unknown command."; fi

help: ## show this help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
63 changes: 31 additions & 32 deletions apps/nestjs-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@
"flamegraph-home": "npx 0x --output-dir './.debug/flamegraph/{pid}.0x' --on-port 'autocannon http://localhost:$PORT --duration 20' -- node ../../node_modules/.bin/next start"
},
"devDependencies": {
"@faker-js/faker": "8.0.2",
"@nestjs/cli": "10.1.16",
"@nestjs/testing": "10.2.2",
"@faker-js/faker": "8.1.0",
"@nestjs/cli": "10.1.18",
"@nestjs/testing": "10.2.7",
"@teable-group/eslint-config-bases": "workspace:^",
"@types/bcrypt": "5.0.0",
"@types/cookie": "0.5.2",
"@types/cookie-parser": "1.4.3",
"@types/cors": "2.8.12",
"@types/express": "4.17.17",
"@types/cookie-parser": "1.4.4",
"@types/cors": "2.8.14",
"@types/express": "4.17.18",
"@types/jest": "29.5.5",
"@types/markdown-it": "13.0.1",
"@types/mime-types": "2.1.1",
"@types/multer": "1.4.7",
"@types/markdown-it": "13.0.2",
"@types/mime-types": "2.1.2",
"@types/multer": "1.4.8",
"@types/node": "18.11.7",
"@types/nodemailer": "6.4.9",
"@types/passport-jwt": "3.0.9",
"@types/passport-local": "1.0.35",
"@types/sharedb": "3.3.2",
"@types/supertest": "2.0.12",
"@types/nodemailer": "6.4.11",
"@types/passport-jwt": "3.0.10",
"@types/passport-local": "1.0.36",
"@types/sharedb": "3.3.3",
"@types/supertest": "2.0.14",
"@types/ws": "8.5.6",
"cross-env": "7.0.3",
"dotenv-flow": "3.3.0",
Expand All @@ -80,12 +80,12 @@
"get-tsconfig": "4.2.0",
"is-ci": "3.0.1",
"jest": "29.7.0",
"jest-mock-extended": "2.0.4",
"jest-mock-extended": "3.0.5",
"npm-run-all": "4.1.5",
"prettier": "2.7.1",
"rimraf": "5.0.1",
"symlink-dir": "5.0.1",
"sync-directory": "5.1.9",
"rimraf": "5.0.5",
"symlink-dir": "5.2.0",
"sync-directory": "6.0.4",
"ts-jest": "29.1.1",
"ts-node": "10.9.1",
"ts-patch": "2.0.2",
Expand All @@ -94,19 +94,17 @@
},
"dependencies": {
"@babel/core": "7.19.6",
"@juicyllama/nestjs-redoc": "2.3.11",
"@nestjs/common": "10.2.2",
"@nestjs/config": "3.0.0",
"@nestjs/core": "10.2.2",
"@nestjs/common": "10.2.7",
"@nestjs/config": "3.1.1",
"@nestjs/core": "10.2.7",
"@nestjs/devtools-integration": "0.1.5",
"@nestjs/event-emitter": "2.0.2",
"@nestjs/jwt": "10.1.0",
"@nestjs/passport": "10.0.1",
"@nestjs/platform-express": "10.2.2",
"@nestjs/platform-ws": "10.2.2",
"@nestjs/swagger": "7.1.10",
"@nestjs/websockets": "10.2.2",
"@soluble/dsn-parser": "1.9.2",
"@nestjs/jwt": "10.1.1",
"@nestjs/passport": "10.0.2",
"@nestjs/platform-express": "10.2.7",
"@nestjs/platform-ws": "10.2.7",
"@nestjs/swagger": "7.1.13",
"@nestjs/websockets": "10.2.7",
"@teable-group/common-i18n": "workspace:^",
"@teable-group/core": "workspace:^",
"@teable-group/db-main-prisma": "workspace:^",
Expand All @@ -125,19 +123,20 @@
"fs-extra": "11.1.1",
"helmet": "7.0.0",
"is-port-reachable": "3.1.0",
"joi": "17.10.2",
"joi": "17.11.0",
"json-rules-engine": "6.1.2",
"knex": "2.5.1",
"lodash": "4.17.21",
"markdown-it": "13.0.1",
"markdown-it": "13.0.2",
"markdown-it-sanitizer": "0.4.3",
"mime-types": "2.1.35",
"nanoid": "3.3.6",
"nest-knexjs": "0.0.18",
"nestjs-cls": "3.5.1",
"nestjs-pino": "3.5.0",
"nestjs-redoc": "2.2.2",
"next": "13.0.2",
"nodemailer": "6.9.5",
"nodemailer": "6.9.6",
"passport": "0.6.0",
"passport-jwt": "4.0.1",
"passport-local": "1.0.0",
Expand Down
4 changes: 2 additions & 2 deletions apps/nestjs-backend/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'dayjs/plugin/timezone';
import 'dayjs/plugin/utc';
import fs from 'fs';
import path from 'path';
import type { RedocOptions } from '@juicyllama/nestjs-redoc';
import { RedocModule } from '@juicyllama/nestjs-redoc';
import type { INestApplication } from '@nestjs/common';
import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
Expand All @@ -16,6 +14,8 @@ import { json, urlencoded } from 'express';
import helmet from 'helmet';
import isPortReachable from 'is-port-reachable';
import { Logger } from 'nestjs-pino';
import { RedocModule } from 'nestjs-redoc';
import type { RedocOptions } from 'nestjs-redoc';
import { AppModule } from './app.module';
import type { ISecurityWebConfig, ISwaggerConfig } from './configs/bootstrap.config';
import { GlobalExceptionFilter } from './filter/global-exception.filter';
Expand Down
2 changes: 1 addition & 1 deletion apps/nestjs-backend/src/global/knex/knex.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { DynamicModule } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { parseDsn } from '@teable-group/core';
import { KnexModule } from 'nest-knexjs';
import { parseDsn } from '../../utils/parse-dsn';

@Module({})
export class TeableKnexModule {
Expand Down
2 changes: 1 addition & 1 deletion apps/nestjs-backend/src/ws/ws.gateway.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class DevWsGateway implements OnModuleInit, OnModuleDestroy {
const port = this.configService.get<number>('SOCKET_PORT');

this.server = new Server({ port, path: '/socket' });
this.logger.log('DevWsGateway afterInit');
this.logger.log(`DevWsGateway afterInit, Port:${port}`);

this.server.on('connection', this.handleConnection);

Expand Down
24 changes: 17 additions & 7 deletions apps/nextjs-app/.env
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
# This is the main file for environment
# https://nextjs.org/docs/basic-features/environment-variables
# @link https://nextjs.org/docs/basic-features/environment-variables

#######################################################################################
# 1. BUILD ENVIRONMENT - Consumed by next.config.mjs during build and development #
#######################################################################################
NEXT_BUILD_ENV_OUTPUT=classic
NEXT_BUILD_ENV_SOURCEMAPS=false
#NEXT_BUILD_ENV_LINT=
#NEXT_BUILD_ENV_TYPECHECK=
#NEXT_BUILD_ENV_CSP=
#NEXT_BUILD_ENV_SENTRY_ENABLED=
#NEXT_BUILD_ENV_SENTRY_UPLOAD_DRY_RUN=
#NEXT_BUILD_ENV_SENTRY_DEBUG=
#NEXT_BUILD_ENV_SENTRY_TRACING=
#######################################################################################

# __NEXT_TEST_MODE = 'jest'
# DATABASE_URL
# When deploying on serveless/lambdas "?connection_limit=" should be 1
# @see https://www.prisma.io/docs/concepts/components/prisma-client/deployment#recommended-connection-limit
PRISMA_DATABASE_URL=

PRISMA_DATABASE_URL=postgresql://teable:teable@127.0.0.1:5432/teable?schema=public

# Sentry related
# Sourcemap upload to sentry is disabled by default
Expand All @@ -20,12 +31,11 @@ SENTRY_PROJECT=monorepo-web-app
NEXT_PUBLIC_SENTRY_RELEASE=
NEXT_PUBLIC_SENTRY_DSN=


# See https://github.com/soluble-io/cache-interop
APP_CACHE_DSN=

LOG_LEVEL=info

NEXTJS_DIR=../nextjs-app
PORT=3000
SOCKET_PORT=${PORT}
SOCKET_PORT=3000
20 changes: 20 additions & 0 deletions apps/nextjs-app/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
#######################################################################################
# 1. BUILD ENVIRONMENT - Consumed by next.config.mjs during build and development #
#######################################################################################
NEXT_BUILD_ENV_OUTPUT=classic
NEXT_BUILD_ENV_SOURCEMAPS=false
NEXT_BUILD_ENV_LINT=false
NEXT_BUILD_ENV_TYPECHECK=false
NEXT_BUILD_ENV_CSP=true
NEXT_BUILD_ENV_SENTRY_ENABLED=false
NEXT_BUILD_ENV_SENTRY_UPLOAD_DRY_RUN=true
NEXT_BUILD_ENV_SENTRY_DEBUG=false
NEXT_BUILD_ENV_SENTRY_TRACING=false

# ↓↓↓↓↓↓↓↓ frontend(nextjs) env ↓↓↓↓↓↓↓↓
NEXTJS_DISABLE_SENTRY=true
NEXTJS_SENTRY_UPLOAD_DRY_RUN=true
Expand All @@ -8,6 +21,13 @@ LOG_LEVEL=info
PORT=3000
SOCKET_PORT=${PORT}

BACKEND_MAIL_SERVICE=<you mail service: example@gmail.com>
BACKEND_MAIL_HOST=<you mail host: example@gmail.com>
BACKEND_MAIL_PORT=<you mail port: 465>
BACKEND_MAIL_SECURE=<you mail secure: true>
BACKEND_MAIL_AUTH_USER=<you mail user: username>
BACKEND_MAIL_AUTH_PASS=<you mail pass: usertoken>

# DATABASE_URL
# @see https://www.prisma.io/docs/reference/database-reference/connection-urls#examples
PRISMA_DATABASE_URL=file:../../db/main.db
Expand Down
Loading

0 comments on commit b0317f8

Please sign in to comment.