From aaebe63e6de7520d138d8f94b070f6f1d1d19a27 Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Fri, 13 Nov 2020 20:52:24 +0300 Subject: [PATCH] docker thing --- .devcontainer/Dockerfile | 10 +++--- .devcontainer/devcontainer.json | 10 ++++-- .devcontainer/docker-compose.yml | 10 +++--- .dockerignore | 5 +++ Dockerfile | 43 +++++++++++++++++++++++++ dockercomposeexample/db.env | 11 +++++++ dockercomposeexample/docker-compose.yml | 23 +++++++++++++ dockerentry.sh | 5 +++ frontend/src/env.ts | 14 ++++++-- ormconfig.dev.json | 2 +- ormconfig.example.json | 4 +-- package.json | 6 ++-- src/migration/1605289418438-init.ts | 31 ++++++++++++++++++ 13 files changed, 155 insertions(+), 19 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 dockercomposeexample/db.env create mode 100644 dockercomposeexample/docker-compose.yml create mode 100644 dockerentry.sh create mode 100644 src/migration/1605289418438-init.ts diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index a3f52cd..bcdf44a 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -2,14 +2,16 @@ ARG VARIANT="14-buster" FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:0-${VARIANT} +ENV DOCKERDEV=true + # Update args in docker-compose.yaml to set the UID/GID of the "node" user. ARG USER_UID=1000 ARG USER_GID=$USER_UID RUN if [ "$USER_GID" != "1000" ] || [ "$USER_UID" != "1000" ]; then \ - groupmod --gid $USER_GID node \ - && usermod --uid $USER_UID --gid $USER_GID node \ - && chmod -R $USER_UID:$USER_GID /home/node \ - && chmod -R $USER_UID:root /usr/local/share/nvm /usr/local/share/npm-global; \ + groupmod --gid $USER_GID node \ + && usermod --uid $USER_UID --gid $USER_GID node \ + && chmod -R $USER_UID:$USER_GID /home/node \ + && chmod -R $USER_UID:root /usr/local/share/nvm /usr/local/share/npm-global; \ fi RUN sudo -u node npm config set unsafe-perm=true diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 2977e0d..5e85b8c 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -39,12 +39,16 @@ "dbaeumer.vscode-eslint", "mtxr.sqltools", "mtxr.sqltools-driver-mysql", - "ms-vscode.vscode-typescript-tslint-plugin" + "visualstudioexptteam.vscodeintellicode", + "rvest.vs-code-prettier-eslint", ], // Use 'forwardPorts' to make a list of ports inside the container available locally. - "forwardPorts": [1234, 3000], + "forwardPorts": [ + 1234, + 3000 + ], // Use 'postCreateCommand' to run commands after the container is created. "postCreateCommand": "npm config set unsafe-perm=true && npm i && cd frontend && npm i", // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root. - // "remoteUser": "node" + "remoteUser": "node" } \ No newline at end of file diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml index 552566c..c1cb228 100644 --- a/.devcontainer/docker-compose.yml +++ b/.devcontainer/docker-compose.yml @@ -22,7 +22,7 @@ services: network_mode: service:db # Uncomment the next line to use a non-root user for all processes. - # user: node + user: node # Use "forwardPorts" in **devcontainer.json** to forward an app port locally. # (Adding the "ports" property to this file will not forward from a Codespace.) @@ -31,7 +31,7 @@ services: image: mariadb:latest restart: unless-stopped volumes: - - mariadb-data:/var/lib/mysql + - writermariadb-data:/var/lib/mysql environment: MYSQL_DATABASE: writer MYSQL_USER: writer @@ -42,7 +42,7 @@ services: image: mariadb:latest restart: unless-stopped volumes: - - mariadbtest-data:/var/lib/mysql + - writermariadbtest-data:/var/lib/mysql environment: MYSQL_DATABASE: writer_test MYSQL_USER: writer @@ -53,5 +53,5 @@ services: # (Adding the "ports" property to this file will not forward from a Codespace.) volumes: - mariadb-data: - mariadbtest-data: \ No newline at end of file + writermariadb-data: + writermariadbtest-data: \ No newline at end of file diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..76ddc81 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +node_modules +npm-debug.log + +frontend/node_modules +frontend/npm-debug.log diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7d66e27 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,43 @@ +FROM node:14 + +WORKDIR /usr/src/app +COPY package*.json ./ +RUN npm ci --only=production + +RUN mkdir frontend +WORKDIR frontend +COPY ./frontend/package*.json ./ +RUN npm ci --only=production +WORKDIR ../ + +COPY . . + +WORKDIR frontend +RUN npm run build +WORKDIR ../ + +ENV PORT=8080 +ENV DATA_DIR=data + +ENV TYPEORM_CONNECTION=mariadb +#ENV TYPEORM_HOST=localhost +#ENV TYPEORM_USERNAME=root +#ENV TYPEORM_PASSWORD=admin +#ENV TYPEORM_DATABASE=test +#ENV TYPEORM_PORT=3000 +ENV TYPEORM_SYNCHRONIZE=false +ENV TYPEORM_LOGGING=false +ENV TYPEORM_ENTITIES=src/entity/**/*.ts +ENV TYPEORM_ENTITIES_DIR=src/entity +ENV TYPEORM_MIGRATIONS=src/migration/**/*.ts +ENV TYPEORM_MIGRATIONS_DIR=src/migration +ENV TYPEORM_SUBSCRIBERS=src/subscriber/**/*.ts +ENV TYPEORM_SUBSCRIBERS_DIR=src/subscriber +ENV TYPEORM_DRIVER_EXTRA='{"charset": "utf8mb4"}' +ENV NODE_ENV=production + +EXPOSE 8080 + +RUN ["chmod", "+x", "dockerentry.sh"] + +CMD [ "./dockerentry.sh" ] diff --git a/dockercomposeexample/db.env b/dockercomposeexample/db.env new file mode 100644 index 0000000..4d7e0e6 --- /dev/null +++ b/dockercomposeexample/db.env @@ -0,0 +1,11 @@ +TYPEORM_HOST = db +TYPEORM_USERNAME = writer +TYPEORM_PASSWORD = writer +TYPEORM_DATABASE = writer +TYPEORM_PORT = 3306 +MYSQL_DATABASE = writer +MYSQL_USER = writer +MYSQL_PASSWORD = writer +MYSQL_ROOT_PASSWORD = writer + + diff --git a/dockercomposeexample/docker-compose.yml b/dockercomposeexample/docker-compose.yml new file mode 100644 index 0000000..7831792 --- /dev/null +++ b/dockercomposeexample/docker-compose.yml @@ -0,0 +1,23 @@ +version: "3.8" +services: + writerapp: + image: stepanusatiuk/writer:latest + restart: always + ports: + - "8080:8080" + volumes: + - ./data:/usr/src/app/data + environment: + - JWT_SECRET=huegrhuigerhupoiervw + env_file: + - db.env + depends_on: + - db + db: + image: mariadb + restart: always + volumes: + - ./dbdata:/var/lib/mysql + env_file: + - db.env + diff --git a/dockerentry.sh b/dockerentry.sh new file mode 100644 index 0000000..eb7e055 --- /dev/null +++ b/dockerentry.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +npm run typeorm -- migration:run + +npm start \ No newline at end of file diff --git a/frontend/src/env.ts b/frontend/src/env.ts index 995525f..d9bd20a 100644 --- a/frontend/src/env.ts +++ b/frontend/src/env.ts @@ -1,2 +1,12 @@ -export const apiRoot = process.env.API_ROOT || "http://localhost:3000"; -export const webRoot = process.env.WEB_ROOT || "http://localhost:1234"; +export const apiRoot = + process.env.API_ROOT || + process.env.NODE_ENV === "production" || + !process.env.NODE_ENV + ? window.location.origin + : "http://localhost:3000"; +export const webRoot = + process.env.WEB_ROOT || + process.env.NODE_ENV === "production" || + !process.env.NODE_ENV + ? window.location.origin + : "http://localhost:1234"; diff --git a/ormconfig.dev.json b/ormconfig.dev.json index bfda3e7..45b5366 100644 --- a/ormconfig.dev.json +++ b/ormconfig.dev.json @@ -5,7 +5,7 @@ "username": "writer", "password": "writer", "database": "writer", - "synchronize": true, + "synchronize": false, "logging": false, "entities": [ "src/entity/**/*.ts" diff --git a/ormconfig.example.json b/ormconfig.example.json index 26b8c9e..45b5366 100644 --- a/ormconfig.example.json +++ b/ormconfig.example.json @@ -4,8 +4,8 @@ "port": 3306, "username": "writer", "password": "writer", - "database": "writer_test", - "synchronize": true, + "database": "writer", + "synchronize": false, "logging": false, "entities": [ "src/entity/**/*.ts" diff --git a/package.json b/package.json index dc53904..e693754 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,9 @@ "frontend": "cd frontend && npm start", "dev": "cross-env NODE_ENV=development concurrently npm:ts-node-dev npm:frontend -c 'blue,green'", "test": "cross-env NODE_ENV=test mocha --timeout 15000 -r ts-node/register -r tsconfig-paths/register 'src/tests/**/*.ts' ", - "lint": "eslint ./src/** ./frontend/src/** --ext .js,.jsx,.ts,.tsx" + "lint": "eslint ./src/** ./frontend/src/** --ext .js,.jsx,.ts,.tsx", + "typeorm-dev": "cross-env NODE_ENV=development ts-node -T -r tsconfig-paths/register ./node_modules/typeorm/cli.js", + "typeorm": "cross-env NODE_ENV=production ts-node -T -r tsconfig-paths/register ./node_modules/typeorm/cli.js" }, "husky": { "hooks": { @@ -82,4 +84,4 @@ "engines": { "node": "14.x" } -} +} \ No newline at end of file diff --git a/src/migration/1605289418438-init.ts b/src/migration/1605289418438-init.ts new file mode 100644 index 0000000..aef338f --- /dev/null +++ b/src/migration/1605289418438-init.ts @@ -0,0 +1,31 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class init1605289418438 implements MigrationInterface { + name = "init1605289418438"; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + "CREATE TABLE `user` (`id` int NOT NULL AUTO_INCREMENT, `username` varchar(190) NOT NULL, `email` varchar(190) NOT NULL, `passwordHash` varchar(190) NOT NULL, UNIQUE INDEX `IDX_78a916df40e02a9deb1c4b75ed` (`username`), UNIQUE INDEX `IDX_e12875dfb3b1d92d7d7c5377e2` (`email`), PRIMARY KEY (`id`)) ENGINE=InnoDB", + ); + await queryRunner.query( + "CREATE TABLE `document` (`id` int NOT NULL AUTO_INCREMENT, `name` varchar(190) NOT NULL, `content` text NOT NULL DEFAULT '', `createdAt` timestamp NULL DEFAULT NULL, `editedAt` timestamp NULL DEFAULT NULL, `shared` tinyint NOT NULL DEFAULT 0, `userId` int NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB", + ); + await queryRunner.query( + "ALTER TABLE `document` ADD CONSTRAINT `FK_7424ddcbdf1e9b067669eb0d3fd` FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION", + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + "ALTER TABLE `document` DROP FOREIGN KEY `FK_7424ddcbdf1e9b067669eb0d3fd`", + ); + await queryRunner.query("DROP TABLE `document`"); + await queryRunner.query( + "DROP INDEX `IDX_e12875dfb3b1d92d7d7c5377e2` ON `user`", + ); + await queryRunner.query( + "DROP INDEX `IDX_78a916df40e02a9deb1c4b75ed` ON `user`", + ); + await queryRunner.query("DROP TABLE `user`"); + } +}