From e5d7df3e7b6ce5d6cbbc595ed5bfe846bf863e89 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Fri, 16 Apr 2021 21:06:45 +0200 Subject: [PATCH] Simplify project setup (#3) * Simplify project setup * Setup docker * Add seed * Feature/migrate links to router link (#2) * Migrate links to RouterLink * Add i18n * Simplify project setup * Setup docker * Add seed --- .env.sample => .env | 2 +- .gitignore | 5 -- CHANGELOG.md | 1 + Dockerfile | 9 -- README.md | 13 +-- .../docker-compose.yml | 17 ---- package.json | 4 + prisma/seed.ts | 53 +++++++++++ scripts/dockerize.ts | 89 ------------------- 9 files changed, 66 insertions(+), 127 deletions(-) rename .env.sample => .env (95%) delete mode 100644 Dockerfile rename docker-compose.yml => docker/docker-compose.yml (53%) create mode 100644 prisma/seed.ts delete mode 100644 scripts/dockerize.ts diff --git a/.env.sample b/.env similarity index 95% rename from .env.sample rename to .env index 13c6f5e5d..9c4f9789a 100644 --- a/.env.sample +++ b/.env @@ -3,7 +3,7 @@ COMPOSE_PROJECT_NAME=ghostfolio-development # CACHE CACHE_TTL=1 MAX_ITEM_IN_CACHE=9999 -REDIS_HOST=redis +REDIS_HOST=localhost REDIS_PORT=6379 # POSTGRES diff --git a/.gitignore b/.gitignore index b19ecb9c9..301ed078d 100644 --- a/.gitignore +++ b/.gitignore @@ -40,8 +40,3 @@ testem.log # System Files .DS_Store Thumbs.db - -# Local -/backups -.env -ec2-nano-ssh.pem diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cfec22d3..56154676b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Changed the routing to `routerLink` for an improved navigation experience +- Simplified the initial project setup ## 0.85.0 - 12.04.2021 diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index af9ed9ac7..000000000 --- a/Dockerfile +++ /dev/null @@ -1,9 +0,0 @@ -FROM node:14 - -# Create app directory -WORKDIR /app - -COPY . . - -EXPOSE 3333 -CMD [ "npm", "run", "start:prod" ] diff --git a/README.md b/README.md index a49cad0dd..c389af912 100644 --- a/README.md +++ b/README.md @@ -23,18 +23,19 @@ ### Setup 1. Run `yarn install` -2. Run `yarn docker:dockerize` -3. Copy `.env.sample` to `docker/.env` -4. Run `cd docker/` -5. Run `docker-compose build` -6. Run `docker-compose up -d` +2. Run `cd docker` +3. Run `docker compose build` +4. Run `docker compose up -d` +5. Run `yarn setup:database` +6. Start server and client (see _Development_) +7. Login as _Admin_ with the `Security Token`: `ae76872ae8f3419c6d6f64bf51888ecbcc703927a342d815fafe486acdb938da07d0cf44fca211a0be74a423238f535362d390a41e81e633a9ce668a6e31cdf9` ## Development ### Start server -- Serve: Run `yarn start:server` - Debug: Run `yarn watch:server` and click "Launch Program" in _Visual Studio Code_ +- Serve: Run `yarn start:server` ### Start client diff --git a/docker-compose.yml b/docker/docker-compose.yml similarity index 53% rename from docker-compose.yml rename to docker/docker-compose.yml index 1530c0da0..8e5a8ec07 100644 --- a/docker-compose.yml +++ b/docker/docker-compose.yml @@ -1,20 +1,5 @@ version: '3.7' services: - ghostfolio: - container_name: ghostfolio - image: ghostfolio:{{VERSION}} - restart: unless-stopped - build: - context: . - dockerfile: Dockerfile - depends_on: - - postgres - - redis - env_file: - - ../.env - environment: - - NODE_ENV=production - postgres: image: postgres:12 container_name: postgres @@ -34,6 +19,4 @@ services: - 6379:6379 volumes: - portainer_data: postgres: - name: ghostfolio-db diff --git a/package.json b/package.json index 0aa1403ba..6d0fa2a24 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,8 @@ "angular": "node --max_old_space_size=32768 ./node_modules/@angular/cli/bin/ng", "build:all": "ng build --prod api && ng build --prod client && yarn run replace-placeholders-in-build", "clean": "rimraf dist", + "database:push": "yarn prisma db push --preview-feature", + "database:seed": "prisma db seed --preview-feature", "dep-graph": "nx dep-graph", "docker:build": "cd docker/1.0.1 && docker-compose build", "docker:dockerize": "./node_modules/.bin/ts-node ./scripts/dockerize.ts", @@ -29,11 +31,13 @@ "nx": "nx", "postinstall": "yarn prisma generate", "replace-placeholders-in-build": "node ./replace.build.js", + "setup:database": "yarn database:push && yarn database:seed", "start": "node dist/apps/api/main", "start:client": "ng serve client --hmr -o", "start:prod": "node apps/api/main", "start:server": "yarn run nx serve api", "test": "ng test", + "ts-node": "ts-node --compiler-options '{\"module\":\"CommonJS\"}'", "update": "nx migrate latest", "watch:server": "yarn run nx build api --watch", "workspace-generator": "nx workspace-generator" diff --git a/prisma/seed.ts b/prisma/seed.ts new file mode 100644 index 000000000..b63df349d --- /dev/null +++ b/prisma/seed.ts @@ -0,0 +1,53 @@ +import { Currency, PrismaClient, Role, Type } from '@prisma/client'; +const prisma = new PrismaClient(); + +async function main() { + const admin = await prisma.user.upsert({ + create: { + accessToken: + 'c689bcc894e4a420cb609ee34271f3e07f200594f7d199c50d75add7102889eb60061a04cd2792ebc853c54e37308271271e7bf588657c9e0c37faacbc28c3c6', + alias: 'Admin', + id: '4e1af723-95f6-44f8-92a7-464df17f6ec3', + role: Role.ADMIN + }, + update: {}, + where: { id: '4e1af723-95f6-44f8-92a7-464df17f6ec3' } + }); + + const demo = await prisma.user.upsert({ + create: { + accessToken: + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjliMTEyYjRkLTNiN2QtNGJhZC05YmRkLTNiMGY3YjRkYWMyZiIsImlhdCI6MTYxODUxMjAxNCwiZXhwIjoxNjIxMTA0MDE0fQ.l3WUxpI0hxuQtdPrD0kd7sem6S2kx_7CrdNvkmlKuWw', + alias: 'Demo', + id: '9b112b4d-3b7d-4bad-9bdd-3b0f7b4dac2f', + role: Role.DEMO, + Order: { + create: [ + { + currency: Currency.USD, + date: new Date(Date.UTC(2017, 0, 3, 0, 0, 0)), + fee: 30, + id: 'cf7c0418-8535-4089-ae3d-5dbfa0aec2e1', + quantity: 50, + symbol: 'TSLA', + type: Type.BUY, + unitPrice: 42.97 + } + ] + } + }, + update: {}, + where: { id: '9b112b4d-3b7d-4bad-9bdd-3b0f7b4dac2f' } + }); + + console.log({ admin, demo }); +} + +main() + .catch((e) => { + console.error(e); + process.exit(1); + }) + .finally(async () => { + await prisma.$disconnect(); + }); diff --git a/scripts/dockerize.ts b/scripts/dockerize.ts deleted file mode 100644 index 4931cfd57..000000000 --- a/scripts/dockerize.ts +++ /dev/null @@ -1,89 +0,0 @@ -import * as dotenv from 'dotenv'; -import * as path from 'path'; - -const replace = require('replace-in-file'); -const version = require(path.resolve(__dirname, '..', 'package.json')).version; - -/** - * Creates a docker image - */ -class Dockerizer { - /** - * @constructor - */ - constructor() { - dotenv.config({ - path: path.resolve(__dirname, '..', '.env') - }); - } - - public async dockerize(aVersion: string) { - console.log('Dockerizing...'); - - console.log('Version:', aVersion); - - await this.executeShellCommand('rm -rf ./dist'); - - console.log(`Building source...`); - - await this.executeShellCommand('ng build --prod api'); - - await this.executeShellCommand('ng build --prod client'); - - await this.executeShellCommand('yarn run replace-placeholders-in-build'); - - console.log(`Copying files...`); - - await this.executeShellCommand(`mkdir -p ./docker/${aVersion}`); - await this.executeShellCommand(`cp -r ./dist/ ./docker/${aVersion}`); - - await this.executeShellCommand(`mkdir ./docker/${aVersion}/prisma`); - await this.executeShellCommand( - `cp -r ./prisma/schema.prisma ./docker/${aVersion}/prisma/schema.prisma` - ); - - await this.executeShellCommand( - `cp ./docker-compose.yml ./docker/${aVersion}` - ); - - try { - replace.sync({ - allowEmptyPaths: false, - files: `./docker/${aVersion}/docker-compose.yml`, - from: /{{VERSION}}/g, - to: aVersion - }); - } catch (error) { - console.error('Error while replacing docker-compose.yml:', error); - } - - await this.executeShellCommand(`cp ./Dockerfile ./docker/${aVersion}`); - await this.executeShellCommand(`cp ./package.json ./docker/${aVersion}`); - await this.executeShellCommand(`cp ./yarn.lock ./docker/${aVersion}`); - - await this.executeShellCommand( - `cd docker/${aVersion} && yarn install --production` - ); - - console.log('Dockerizing has been completed successfully.'); - } - - /** - * Executes a shell command and return it as a promise - */ - private executeShellCommand(cmd: string) { - const exec = require('child_process').exec; - return new Promise((resolve, reject) => { - // Maximum buffer size increased to 1000 * 1000KB - exec(cmd, { maxBuffer: 1000 * 1024 * 1000 }, (error, stdout, stderr) => { - if (error) { - console.warn(error); - } - resolve(stdout ? stdout : stderr); - }); - }); - } -} - -const dockerizer = new Dockerizer(); -dockerizer.dockerize(version);