Skip to content

Commit

Permalink
Feature: Env var derivation + running application (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomenezes authored Dec 15, 2023
1 parent 2b82064 commit abc0736
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
web: GQL_PORT=$PORT sqd serve:prod
worker: sqd process:prod
web: GQL_PORT=$PORT npm run serve:prod
worker: npm run process:prod
9 changes: 9 additions & 0 deletions deploy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Deploy Helpers

Folder holding scripts to support the API deployment process.

### Run script

Currently support deriving the database_url environment variable into expected ones for the processor and graphQL-server (e.g. DB_HOST, DB_PASS), then it starts the target applications with this new vars available.

> Check the script [here](./run) for more details.
65 changes: 65 additions & 0 deletions deploy/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env node

/**
* @file Manage to derive the DATABASE_URL env var
* into several ones, followed by starting the correct npm script.
* It will set the new vars prepending its declaration prior
* calling the npm-script defined by the caller.
* The main intent is to solve the problem when the cloud provider changes
* the DATABASE_URL due to maintenance that leads to manual derivation
* of such values.
*
* @author Bruno Menezes <[email protected]>
*/

/**
* @typedef {Object} Config
* @property {string} DB_HOST - Database host
* @property {string} DB_NAME - Database name
* @property {string} DB_USER - Database user
* @property {string} DB_PASS - Database password
* @property {string} DB_PORT - Database port
*/

const url = require('node:url');
const shell = require('shelljs');
const yargs = require('yargs');
const { hideBin } = require('yargs/helpers');

const argv = yargs(hideBin(process.argv)).argv;

if (!argv.npmScriptName) {
console.error('Npm script name is required!\n');
process.exitCode = 1;
}

/**
* @type Config
*/
const config = {};

const scriptName = argv.npmScriptName;
const DATABASE_URL = process.env.DATABASE_URL;
const db = url.parse(DATABASE_URL);
const auth = db.auth.split(':');
config.DB_HOST = db.hostname;
config.DB_NAME = db.pathname.substring(1);
config.DB_USER = auth[0];
config.DB_PASS = auth[1];
config.DB_PORT = db.port;

/**
*
* @param {string} scriptName Existing npm-script name.
* @param {Config} config Database environment variables to be injected.
*/
function runScript(scriptName, config) {
const vars = [];
for (const key in config) {
vars.push(`${key}=${config[key]}`);
}

shell.exec(`${vars.join(' ')} npm run ${scriptName}`);
}

runScript(scriptName, config);
32 changes: 7 additions & 25 deletions package-lock.json

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

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
"sqd:down": "sqd down",
"sqd:migration": "sqd migration",
"sqd:process": "sqd process",
"sqd:process:prod": "sqd process:prod",
"sqd:graphql": "sqd serve",
"sqd:graphql:prod": "sqd serve:prod",
"sqd:auth": "sqd auth -k $SQD_API_KEY",
"sqd:deploy": "sqd deploy -o cartesi -m",
"sqd:deploy:mainnet": "run-s \"sqd:deploy -- {@} .\" -- \"squid-mainnet.yaml\"",
"sqd:deploy:sepolia": "run-s \"sqd:deploy -- {@} .\" -- \"squid-sepolia.yaml\"",
"tsc": "tsc"
"tsc": "tsc",
"process:prod": "node deploy/run --npmScriptName=sqd:process:prod",
"serve:prod": "node deploy/run --npmScriptName=sqd:graphql:prod"
},
"dependencies": {
"@cartesi/rollups": "1.0.0",
Expand All @@ -34,8 +38,10 @@
"dotenv": "^16.1.4",
"ethers": "^6.5.1",
"pg": "^8.11.0",
"shelljs": "^0.8.5",
"type-graphql": "^1.2.0-rc.1",
"typeorm": "^0.3.16"
"typeorm": "^0.3.16",
"yargs": "^17.7.2"
},
"devDependencies": {
"@subsquid/cli": "latest",
Expand Down

0 comments on commit abc0736

Please sign in to comment.