From dfa292a3c10559148ff5771b984a41dc22ab5636 Mon Sep 17 00:00:00 2001 From: Dmitry Sviridenko Date: Sun, 16 Jul 2023 15:58:25 +0300 Subject: [PATCH 1/3] feat: Improve README --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index a169cf84..ae0b5ba8 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,29 @@ npm run dev 2. Run `nvm use` 3. That's it! +### Guide to setup postgres DB and Redis from scratch + +If you can access your user and you know how to create a DB, **you can ignore that section**. + +If you don't know how to access your postgres user or DB: +1. Install Postgres.app for all existing postgres version [here](https://postgresapp.com/downloads.html) (it will install all needed Posgres versions). +2. Open the app, in the interface click on the "+" on the bottom left and add a new V11 server. +3. Click "Initialize" button from the interface. +4. If you have error "Post already in use", try to close apps that are using that port. Otherwise click on the server V11 in the interface and change port to 5433 +5. Connect to default DB user using "psql -h localhost -p 5432 -U postgres -d postgres". If you changed port on the previos step, update the port +6. Now run following commands to setup a user (update dumb values with you own): + - "CREATE USER `myuser` WITH PASSWORD `'secretpassword'`;" + - ALTER ROLE `myuser` SUPERUSER; + - CREATE DATABASE `db-name`; (db-name will be used for the app) + - \d (to close the connection) + - psql -h localhost -p 5432 -U `myuser` -d `db-name` +7. That's it. + +To install Redis (if you don't have one): +1. Install Redis via `brew install redis` +2. Then `brew services start redis` +3. You're done :) + ### Current DB schema ![budget-tracker-schema](https://user-images.githubusercontent.com/12257282/147393125-de1c8815-023e-49d4-b337-20cfaea06552.png) From b6f94825cf5fafd8b39fc555ece8911c6980d05b Mon Sep 17 00:00:00 2001 From: Dmitry Sviridenko Date: Sun, 16 Jul 2023 16:30:37 +0300 Subject: [PATCH 2/3] feat: Improve configs storing --- .env.template | 11 +++++++++++ .eslintignore | 2 +- .gitignore | 2 ++ .sequelizerc | 2 +- config/db/config.js | 1 + config/default.js | 26 ++++++++++++++++++++++++++ config/default.json | 8 -------- config/development.js | 5 +++++ config/development.json | 21 --------------------- config/production.js | 5 +++++ config/production.json | 21 --------------------- config/test.js | 5 +++++ config/test.json | 21 --------------------- jest.config.ts | 2 +- src/app.ts | 2 +- src/models/index.ts | 2 +- 16 files changed, 60 insertions(+), 76 deletions(-) create mode 100644 config/default.js delete mode 100644 config/default.json create mode 100644 config/development.js delete mode 100644 config/development.json create mode 100644 config/production.js delete mode 100644 config/production.json create mode 100644 config/test.js delete mode 100644 config/test.json diff --git a/.env.template b/.env.template index e3645f22..13b756ba 100644 --- a/.env.template +++ b/.env.template @@ -1,2 +1,13 @@ # used in migrations to fetch actual currencies rates API_LAYER_API_KEY= + +APPLICATION_HOST= +APPLICATION_PORT= +APPLICATION_JWT_SECRET= +APPLICATION_DB_HOST= +APPLICATION_DB_USERNAME= +APPLICATION_DB_PASSWORD= +APPLICATION_DB_DATABASE= +APPLICATION_DB_PORT= +APPLICATION_DB_DIALECT= +APPLICATION_REDIS_HOST= diff --git a/.eslintignore b/.eslintignore index fa9bd52d..24c38ec9 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,4 @@ src/seeders/**/*.js src/migrations/**/*.js .eslintrc.js -config/db/config.js +config/** diff --git a/.gitignore b/.gitignore index c3154412..f02f89e0 100644 --- a/.gitignore +++ b/.gitignore @@ -77,6 +77,8 @@ typings/ # dotenv environment variables file .env .env.test +.env.development +.env.production # parcel-bundler cache (https://parceljs.org/) .cache diff --git a/.sequelizerc b/.sequelizerc index f39a36bd..7376f30d 100644 --- a/.sequelizerc +++ b/.sequelizerc @@ -1,5 +1,5 @@ const path = require('path'); -require('dotenv').config(); +require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }) module.exports = { "config": path.resolve('./config/db', 'config.js'), diff --git a/config/db/config.js b/config/db/config.js index c718271a..706a53bf 100644 --- a/config/db/config.js +++ b/config/db/config.js @@ -1,3 +1,4 @@ +require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` }); const config = require('config').get('db'); const options = { diff --git a/config/default.js b/config/default.js new file mode 100644 index 00000000..eea66cd5 --- /dev/null +++ b/config/default.js @@ -0,0 +1,26 @@ +// Default file that being merged with others env-related files + +module.exports = { + name: 'budget-tracker-be', + host: process.env.APPLICATION_HOST, + port: process.env.APPLICATION_PORT, + apiPrefix: '/api/v1', + jwtSecret: process.env.APPLICATION_JWT_SECRET, + db: { + host: process.env.APPLICATION_DB_HOST, + username: process.env.APPLICATION_DB_USERNAME, + password: process.env.APPLICATION_DB_PASSWORD, + database: process.env.APPLICATION_DB_DATABASE, + port: process.env.APPLICATION_DB_PORT, + dialect: process.env.APPLICATION_DB_DIALECT, + logging: true, + }, + redis: { + host: process.env.APPLICATION_REDIS_HOST, + }, + bankIntegrations: { + monobank: { + apiEndpoint: 'https://api.monobank.ua' + } + }, +} diff --git a/config/default.json b/config/default.json deleted file mode 100644 index f222d7cb..00000000 --- a/config/default.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "budget-tracker-be", - "bankIntegrations": { - "monobank": { - "apiEndpoint": "https://api.monobank.ua" - } - } -} diff --git a/config/development.js b/config/development.js new file mode 100644 index 00000000..238eb4e1 --- /dev/null +++ b/config/development.js @@ -0,0 +1,5 @@ +module.exports = { + env: 'development', + envShort: 'dev', + hostWebhooksCallback: 'http://d8d75e719def.ngrok.io', +} diff --git a/config/development.json b/config/development.json deleted file mode 100644 index 0a1d6054..00000000 --- a/config/development.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "env": "development", - "envShort": "dev", - "host": "127.0.0.1", - "port": "8081", - "apiPrefix": "/api/v1", - "jwtSecret": "dev-jwt", - "hostWebhooksCallback": "http://d8d75e719def.ngrok.io", - "db": { - "host": "127.0.0.1", - "username": "letehaha", - "password": "password", - "database": "budget-tracker", - "port": "5432", - "dialect": "postgres", - "logging": true - }, - "redis": { - "host": "127.0.0.1" - } -} diff --git a/config/production.js b/config/production.js new file mode 100644 index 00000000..f7eeede3 --- /dev/null +++ b/config/production.js @@ -0,0 +1,5 @@ +module.exports = { + env: 'production', + envShort: 'prod', + hostWebhooksCallback: 'http://d8d75e719def.ngrok.io', +} diff --git a/config/production.json b/config/production.json deleted file mode 100644 index e3558d94..00000000 --- a/config/production.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "env": "production", - "envShort": "prod", - "host": "127.0.0.1", - "port": "8081", - "apiPrefix": "/api/v1", - "jwtSecret": "prod-jwt", - "hostWebhooksCallback": "http://d8d75e719def.ngrok.io", - "db": { - "host": "db", - "username": "letehaha", - "password": "password", - "database": "budget-tracker", - "port": "5432", - "dialect": "postgres", - "logging": false - }, - "redis": { - "host": "redis" - } -} diff --git a/config/test.js b/config/test.js new file mode 100644 index 00000000..52d4add1 --- /dev/null +++ b/config/test.js @@ -0,0 +1,5 @@ +module.exports = { + env: 'test', + envShort: 'test', + hostWebhooksCallback: 'http://d8d75e719def.ngrok.io', +} diff --git a/config/test.json b/config/test.json deleted file mode 100644 index 36ead198..00000000 --- a/config/test.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "env": "development", - "envShort": "dev", - "host": "127.0.0.1", - "port": "8081", - "apiPrefix": "/api/v1", - "jwtSecret": "dev-jwt", - "hostWebhooksCallback": "http://d8d75e719def.ngrok.io", - "db": { - "host": "127.0.0.1", - "username": "letehaha", - "password": "password", - "database": "budget-tracker_test", - "port": "5432", - "dialect": "postgres", - "logging": false - }, - "redis": { - "host": "127.0.0.1" - } -} diff --git a/jest.config.ts b/jest.config.ts index 20cb5d9c..6ae872f7 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -3,7 +3,7 @@ export default { preset: 'ts-jest', testEnvironment: 'node', - testMatch: ['**/?(*.)+(spec|test|e2e).[jt]s?(x)'], + testMatch: ['**/?(*.)+(unit|spec|e2e).[jt]s?(x)'], globals: { 'ts-jest': { tsconfig: 'tsconfig.json', diff --git a/src/app.ts b/src/app.ts index 240cfec5..93205b25 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,7 +1,7 @@ import dotenv from "dotenv"; import 'module-alias/register'; -dotenv.config(); +dotenv.config({ path: `.env.${process.env.NODE_ENV}` }); import config from 'config'; import express, { Request } from 'express'; import cors from 'cors'; diff --git a/src/models/index.ts b/src/models/index.ts index 59cb8f0d..62ca313d 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -15,7 +15,7 @@ const sequelize = new Sequelize({ models: [__dirname + '/**/*.model.ts'], }); -if (process.env.NODE_ENV === 'development') { +if (['development', 'test'].includes(process.env.NODE_ENV)) { console.log('DBConfig', DBConfig); } From 8a9edaf0d4d9d131739c06e9a047189a81144e86 Mon Sep 17 00:00:00 2001 From: Dmitry Sviridenko Date: Sun, 16 Jul 2023 17:48:30 +0300 Subject: [PATCH 3/3] feat: Update CI for secrets --- .github/workflows/check-source-code.yml | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/.github/workflows/check-source-code.yml b/.github/workflows/check-source-code.yml index aa9649bf..577bf2b8 100644 --- a/.github/workflows/check-source-code.yml +++ b/.github/workflows/check-source-code.yml @@ -32,6 +32,7 @@ jobs: name: Unit and e2e testing needs: prepare-dependencies runs-on: ubuntu-latest + environment: test services: postgres: image: postgres:11.12-stretch @@ -54,14 +55,45 @@ jobs: steps: - uses: actions/checkout@v2 - uses: ./.github/actions/prepare-local-env + - name: Make envfile + uses: SpicyPizza/create-envfile@v1 + with: + envkey_APPLICATION_HOST: ${{ secrets.APPLICATION_HOST }} + envkey_APPLICATION_PORT: ${{ secrets.APPLICATION_PORT }} + envkey_APPLICATION_JWT_SECRET: ${{ secrets.APPLICATION_JWT_SECRET }} + envkey_APPLICATION_DB_HOST: ${{ secrets.APPLICATION_DB_HOST }} + envkey_APPLICATION_DB_USERNAME: ${{ secrets.APPLICATION_DB_USERNAME }} + envkey_APPLICATION_DB_PASSWORD: ${{ secrets.APPLICATION_DB_PASSWORD }} + envkey_APPLICATION_DB_DATABASE: ${{ secrets.APPLICATION_DB_DATABASE }} + envkey_APPLICATION_DB_PORT: ${{ secrets.APPLICATION_DB_PORT }} + envkey_APPLICATION_DB_DIALECT: ${{ secrets.APPLICATION_DB_DIALECT }} + envkey_APPLICATION_REDIS_HOST: ${{ secrets.APPLICATION_REDIS_HOST }} + directory: ./ + file_name: .env.test - name: Unit and e2e testing run: npm run test docker-build: name: Build source code using Docker runs-on: ubuntu-latest + environment: production steps: - uses: actions/checkout@v2 + - name: Make envfile + uses: SpicyPizza/create-envfile@v1 + with: + envkey_APPLICATION_HOST: ${{ secrets.APPLICATION_HOST }} + envkey_APPLICATION_PORT: ${{ secrets.APPLICATION_PORT }} + envkey_APPLICATION_JWT_SECRET: ${{ secrets.APPLICATION_JWT_SECRET }} + envkey_APPLICATION_DB_HOST: ${{ secrets.APPLICATION_DB_HOST }} + envkey_APPLICATION_DB_USERNAME: ${{ secrets.APPLICATION_DB_USERNAME }} + envkey_APPLICATION_DB_PASSWORD: ${{ secrets.APPLICATION_DB_PASSWORD }} + envkey_APPLICATION_DB_DATABASE: ${{ secrets.APPLICATION_DB_DATABASE }} + envkey_APPLICATION_DB_PORT: ${{ secrets.APPLICATION_DB_PORT }} + envkey_APPLICATION_DB_DIALECT: ${{ secrets.APPLICATION_DB_DIALECT }} + envkey_APPLICATION_REDIS_HOST: ${{ secrets.APPLICATION_REDIS_HOST }} + directory: ./ + file_name: .env.production - uses: ./.github/actions/docker-build with: docker-hub-username: ${{ secrets.DOCKER_HUB_USERNAME }}