Skip to content

Commit

Permalink
fix(ci): Update source for correct deploy with postgres database
Browse files Browse the repository at this point in the history
  • Loading branch information
EndyKaufman committed Aug 29, 2018
1 parent 6854385 commit af58791
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 37 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ A simple application demonstrating the basic usage of permissions with NestJS (J
- copy `develop._env` to `develop.env` and set environments for use (on Windows copy with IDE)
- run `npm install` to install project dependencies
- run `npm build` to install project dependencies
- run `npm run start` to fire up prod server (`npm run start:dev` - dev server)
- run `npm run start:prod` to fire up prod server (`npm run start:dev` - dev server)
- Open browser to [`http://localhost:5000/swagger`](http://localhost:5000/swagger)

### Users
Expand All @@ -35,7 +35,8 @@ A simple application demonstrating the basic usage of permissions with NestJS (J
### Swagger
- local: [`http://localhost:5000/swagger`](http://localhost:5000/swagger)
- online on heroku: [`https://rucken-core-nestjs.herokuapp.com/swagger`](https://rucken-core-nestjs.herokuapp.com/swagger)
- online on now: [`https://rucken-core-nestjs.now.sh/swagger`](https://rucken-core-nestjs.now.sh/swagger)
- online on now with db sqlite: [`https://rucken-core-nestjs.now.sh/swagger`](https://rucken-core-nestjs.now.sh/swagger)
- online on now with db postgres: [`https://rucken-core-nestjs-postgres.now.sh/swagger`](https://rucken-core-nestjs.now.sh/swagger)
- apiKey template: ```JWT <token_generated_on_login>```

### Docs
Expand Down
3 changes: 1 addition & 2 deletions README_DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ npm run migrate:run

## Run with custom env file
```bash
./node_modules/.bin/cross-env NODE_ENV=postgres npm run clean
./node_modules/.bin/cross-env NODE_ENV=postgres npm run migrate
./node_modules/.bin/cross-env NODE_ENV=postgres npm run build
./node_modules/.bin/cross-env NODE_ENV=postgres npm run start:dev
./node_modules/.bin/cross-env NODE_ENV=postgres npm run start
```
7 changes: 6 additions & 1 deletion now.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
"alias": [
"rucken-core-nestjs.now.sh"
"rucken-nestjs.now.sh",
"rucken-core-nestjs.now.sh",
"rucken-core-nestjs-mongo.now.sh",
"rucken-todo-nestjs.now.sh"
],
"env": {
"NODE_ENV": "production",
"NPM_CONFIG_PRODUCTION": "false",
"DEBUG": "@debug",
"DEMO": "true",
"DOMAIN": "@domain",
Expand Down
20 changes: 9 additions & 11 deletions ormconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ const load = require('dotenv').load;
const path = require('path');
const fs = require('fs');
const NODE_ENV = process.env.NODE_ENV || 'develop';
const DB_SOURCE_EXT = process.env.DB_SOURCE_EXT || 'js';
// todo: wait resolve https://github.com/typeorm/typeorm/issues/2358
// const dbSourceExt = NODE_ENV === 'develop' ? 'ts' : 'js';
// const sourceRootKey = NODE_ENV === 'develop' ? 'sourceRoot' : 'outputPath';
const dbSourceExt = 'ts';
const sourceRootKey = 'sourceRoot';
const sourceRootKey = (DB_SOURCE_EXT === 'ts' || NODE_ENV === 'develop') ? 'sourceRoot' : 'outputPath';
const nestCliConfig = JSON.parse(fs.readFileSync('.nestcli.json'));
try {
fs.accessSync(`${NODE_ENV}.env`);
Expand All @@ -33,15 +31,15 @@ const defaultApp = nestCliConfig.projects[defaultProject];

if (connectionString.protocol === 'sqlite') {
const databaseUrl =
(dbSourceExt === 'ts' ? './' : '../') +
(DB_SOURCE_EXT === 'ts' ? './' : '../') +
connectionString.hosts[0].name +
(connectionString.path.length ? '/' + connectionString.path[0] : '');
module.exports = {
type: 'sqlite',
database: databaseUrl,
entities: [...libs, ...apps].map(lib => `${lib[sourceRootKey]}/**/entities/**/*.entity.${dbSourceExt}`),
migrations: [...libs, ...apps].map(lib => `${lib[sourceRootKey]}/**/migrations/**/*.${dbSourceExt}`),
subscribers: [...libs, ...apps].map(lib => `${lib[sourceRootKey]}/**/subscribers/**/*.${dbSourceExt}`),
entities: [...libs, ...apps].map(lib => `${lib[sourceRootKey]}/**/entities/**/*.entity.${DB_SOURCE_EXT}`),
migrations: [...libs, ...apps].map(lib => `${lib[sourceRootKey]}/**/migrations/**/*.${DB_SOURCE_EXT}`),
subscribers: [...libs, ...apps].map(lib => `${lib[sourceRootKey]}/**/subscribers/**/*.${DB_SOURCE_EXT}`),
logging: 'all',
synchronize: false,
cli: {
Expand All @@ -56,9 +54,9 @@ if (connectionString.protocol === 'sqlite') {
username: connectionString.user,
password: connectionString.password,
database: connectionString.path[0],
entities: [...libs, ...apps].map(lib => `${lib[sourceRootKey]}/**/entities/**/*.entity.${dbSourceExt}`),
migrations: [...libs, ...apps].map(lib => `${lib[sourceRootKey]}/**/migrations/**/*.${dbSourceExt}`),
subscribers: [...libs, ...apps].map(lib => `${lib[sourceRootKey]}/**/subscribers/**/*.${dbSourceExt}`),
entities: [...libs, ...apps].map(lib => `${lib[sourceRootKey]}/**/entities/**/*.entity.${DB_SOURCE_EXT}`),
migrations: [...libs, ...apps].map(lib => `${lib[sourceRootKey]}/**/migrations/**/*.${DB_SOURCE_EXT}`),
subscribers: [...libs, ...apps].map(lib => `${lib[sourceRootKey]}/**/subscribers/**/*.${DB_SOURCE_EXT}`),
logging: 'all',
synchronize: false,
cli: {
Expand Down
28 changes: 21 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"start:dev": "nodemon",
"start:prod": "cross-env NODE_ENV=production ./node_modules/.bin/pm2 start dist/main.js --name demo",
"start:hmr": "cross-env NODE_ENV=production node dist/main.hmr.js",
"start": "./node_modules/.bin/pm2 start dist/main.js --name demo",
"start": "node dist/main.js",
"stop": "./node_modules/.bin/pm2 kill",
"logs": "./node_modules/.bin/pm2 logs",
"logs:flush": "./node_modules/.bin/pm2 flush",
Expand All @@ -57,11 +57,11 @@
"webpack": "webpack --config webpack.config.js",
"migrate:create": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:create",
"migrate:generate": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:generate",
"migrate": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:run",
"migrate": "cross-env DB_SOURCE_EXT=ts ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js migration:run",
"docs": "./node_modules/.bin/typedoc --ignoreCompilerErrors --excludeExternals --out www/docs src",
"md-to-html": "showdown makehtml -i README.md -o frontend/index.html",
"frontend": "run-s md-to-html",
"build": "run-s clean format docs migrate tsc frontend",
"build": "run-s clean format docs tsc migrate frontend",
"heroku-postbuild": "npm run build",
"prepare:core": "rucken make-ts-list ./src/libs/core",
"prepare:auth": "rucken make-ts-list ./src/libs/auth",
Expand Down
23 changes: 23 additions & 0 deletions postgres-now.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"alias": [
"rucken-core-nestjs-postgres.now.sh"
],
"env": {
"NODE_ENV": "production",
"NPM_CONFIG_PRODUCTION": "false",
"DEBUG": "@debug",
"DEMO": "true",
"DOMAIN": "@domain",
"PROTOCOL": "https",
"JWT_SECRET_KEY": "@jwt_secret_key",
"JWT_EXPIRATION_DELTA": "7 days",
"JWT_AUTH_HEADER_PREFIX": "JWT",
"FACEBOOK_CLIENT_ID": "@facebook_client_id",
"FACEBOOK_CLIENT_SECRET": "@facebook_client_secret",
"FACEBOOK_OAUTH_REDIRECT_URI": "@facebook_oauth_redirect_uri",
"GOOGLE_CLIENT_ID": "@google_client_id",
"GOOGLE_CLIENT_SECRET": "@google_client_secret",
"GOOGLE_OAUTH_REDIRECT_URI": "@google_oauth_redirect_uri",
"DATABASE_URL": "@postgres_database_url"
}
}
29 changes: 29 additions & 0 deletions postgres._env
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
DEBUG=true
DEMO=false
NODE_MODULES_CACHE=false
NPM_CONFIG_PRODUCTION=false
DOMAIN=localhost
PORT=5000
EXTERNAL_PORT=5000
JWT_SECRET_KEY=secret_key
JWT_EXPIRATION_DELTA=7 days
JWT_AUTH_HEADER_PREFIX=JWT

# https://developers.facebook.com/apps/<FACEBOOK_CLIENT_ID>/settings/basic/
FACEBOOK_CLIENT_ID=
FACEBOOK_CLIENT_SECRET=
FACEBOOK_OAUTH_REDIRECT_URI={host}/auth/facebook/

# https://console.developers.google.com/apis/credentials/oauthclient/
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_OAUTH_REDIRECT_URI={host}/auth/google-plus/

#DATABASE_URL=sqlite://database/sqlitedb.db
DATABASE_URL=postgres://USER:PASSWORD@HOST:1105/NAME
#DATABASE_URL=sqlite://db.sqlite3
#DATABASE_URL=oracle://USER:PASSWORD@HOST:1105/NAME
#DATABASE_URL=mysql://USER:PASSWORD@HOST:1105/NAME
#DATABASE_URL=redis://USER:PASSWORD@HOST:1105
#DATABASE_URL=memcached://USER:PASSWORD@HOST:1105
#DATABASE_URL=ocmem://HOST:1105/PATH
29 changes: 29 additions & 0 deletions production._env
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
DEBUG=true
DEMO=false
NODE_MODULES_CACHE=false
NPM_CONFIG_PRODUCTION=false
DOMAIN=localhost
PORT=5000
EXTERNAL_PORT=5000
JWT_SECRET_KEY=secret_key
JWT_EXPIRATION_DELTA=7 days
JWT_AUTH_HEADER_PREFIX=JWT

# https://developers.facebook.com/apps/<FACEBOOK_CLIENT_ID>/settings/basic/
FACEBOOK_CLIENT_ID=
FACEBOOK_CLIENT_SECRET=
FACEBOOK_OAUTH_REDIRECT_URI={host}/auth/facebook/

# https://console.developers.google.com/apis/credentials/oauthclient/
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_OAUTH_REDIRECT_URI={host}/auth/google-plus/

DATABASE_URL=sqlite://database/sqlitedb.db
#DATABASE_URL=postgres://USER:PASSWORD@HOST:1105/NAME
#DATABASE_URL=sqlite://db.sqlite3
#DATABASE_URL=oracle://USER:PASSWORD@HOST:1105/NAME
#DATABASE_URL=mysql://USER:PASSWORD@HOST:1105/NAME
#DATABASE_URL=redis://USER:PASSWORD@HOST:1105
#DATABASE_URL=memcached://USER:PASSWORD@HOST:1105
#DATABASE_URL=ocmem://HOST:1105/PATH
4 changes: 4 additions & 0 deletions scripts/now-deploy.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#!/bin/bash
now rm rucken-core-nestjs -y
now --public
now alias
now --local-config postgres-now.json --public
now --local-config postgres-now.json alias
read -p "Press any key to continue... " -n1 -s
1 change: 1 addition & 0 deletions scripts/version-bump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ bump=`node_modules/.bin/conventional-recommended-bump -p angular`
echo ${1:-$bump}
npm --no-git-tag-version version ${1:-$bump} &>/dev/null
node_modules/.bin/conventional-changelog -i CHANGELOG.md -s -p ${2:-$preset}
read -p "Press any key to continue... " -n1 -s
16 changes: 10 additions & 6 deletions src/main.hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,21 @@ async function bootstrap() {
path.resolve(__dirname, '..', 'www'),
path.resolve(__dirname, '..', 'frontend')
];

Logger.log(NODE_ENV);
const envFile = path.resolve(__dirname, '..', `${NODE_ENV}.env`);
try {
accessSync(`../${NODE_ENV}.env`);
load({ path: `../${NODE_ENV}.env` });
Logger.log(`env file: ${NODE_ENV}.env`, 'Main');
accessSync(envFile);
load({ path: envFile });
Logger.log(`env file: ${envFile}`, 'Main');
} catch (error) {
Logger.log(`error on get env file: ${envFile}`, 'Main');
try {
accessSync(`.env`);
load();
Logger.log(`env file: .env`, 'Main');
} catch (error) {}
} catch (error) {
Logger.log(`error on get env file: .env`, 'Main');
}
}
const connectionString = new ConnectionString(process.env.DATABASE_URL);
if (connectionString.protocol === 'sqlite') {
Expand All @@ -67,7 +71,7 @@ async function bootstrap() {
(connectionString.path.length ? '/' + connectionString.path[0] : '');
try {
chmod(dbFile, 777);
} catch (error) {}
} catch (error) { }
}
const coreConfig: ICoreConfig = {
...defaultCoreConfig,
Expand Down
14 changes: 9 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,21 @@ async function bootstrap() {
path.resolve(__dirname, '..', 'www'),
path.resolve(__dirname, '..', 'frontend')
];

Logger.log(NODE_ENV);
const envFile = path.resolve(__dirname, '..', `${NODE_ENV}.env`);
try {
accessSync(`../${NODE_ENV}.env`);
load({ path: `../${NODE_ENV}.env` });
Logger.log(`env file: ${NODE_ENV}.env`, 'Main');
accessSync(envFile);
load({ path: envFile });
Logger.log(`env file: ${envFile}`, 'Main');
} catch (error) {
Logger.log(`error on get env file: ${envFile}`, 'Main');
try {
accessSync(`.env`);
load();
Logger.log(`env file: .env`, 'Main');
} catch (error) {}
} catch (error) {
Logger.log(`error on get env file: .env`, 'Main');
}
}
const connectionString = new ConnectionString(process.env.DATABASE_URL);
if (connectionString.protocol === 'sqlite') {
Expand Down

0 comments on commit af58791

Please sign in to comment.