-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Env var derivation + running application (#20)
- Loading branch information
1 parent
2b82064
commit abc0736
Showing
5 changed files
with
91 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters