@cardstack/hub

0.28.7 • Public • Published

@cardstack/hub

The Cardstack Hub is the API server for the Cardstack project. For more information, see the project-wide README.

Architecture

The Hub consists of API endpoints and a postgres database.

The app uses a Postgresql-based background task queue built on graphile/worker

Configuration

Below is a list of the most common environment variables that the Hub accepts:

  • SERVER_SECRET (required) - to generate one for your machine, run node --eval="console.log(crypto.randomBytes(32).toString('base64'))"
  • FIXER_API_KEY (required for /api/exchange-rates) - API key for currency exchange rates, we use https://fixer.io/
  • EXCHANGE_RATES_ALLOWED_DOMAINS - domains from which a request to /api/exchange-rates is allowed
  • HUB_AWS_ACCESS_KEY_ID
  • HUB_AWS_SECRET_ACCESS_KEY
  • HUB_AWS_REGION
  • AWS_PROFILE - if none of the HUBAWS* variables are defined, no credentials or region will be passed to the aws-sdk. This will make the aws-sdk's default behavior take effect, which includes using an AWS_PROFILE env var if it is set
  • DATABASE_URL - defaults in development to postgres://postgres:postgres@localhost:5432/hub_development
  • LOG_LEVELS - defaults to *=info

Search the mono-repo for process.env and check the config directory to see these variables referenced.

To use the variables, create a file named .env in the hub's folder, and put in the variables you want to use.

For example:

SERVER_SECRET=7TmgY1xFo/WrYTnAFSvAemZtFB8wQVMd8IkoeQKBboE=
AWS_PROFILE=cardstack

Setting up Discord

Some of the packages in this mono repo support the operation of a discord bot that uses the hub's DI system. We leverage CordeJS for our unit tests. CordeJS uses a discord bot running in discord to test the bot functionality by emulating a discord user and sending commands to the bot under test. In order to run the cordejs tests, you'll need to setup a discord server and install the cordebot with full permissions as well as the cardbot into the discord server.

The instructions for setting up the cordebot (and cardbot) are here: https://cordejs.org/docs/creatingdiscordbot. In these instructions you need to setup 2 bots (the instructions outline how to setup a single bot). One bot that you setup will be the cardbot, the other bot that you setup will be the cordebot (used to test the cardbot).

At the time of this writing the cardbot requires the following OAuth scopes:

Once the cardbot and cordebot bots are setup, you can then configure environment variables for running the bot tests. Specifically:

  • CORDE_BOT_ID: The bot ID for the cordebot
  • CORDE_BOT_TOKEN: The bot token for the cordebot
  • CARDBOT_ID: The bot ID for the cardbot
  • CARDBOT_TOKEN: The bot token for the cardbot
  • CARDBOT_ALLOWED_GUILDS: A comma separated list of the discord server IDs that the cardbot is allowed to communicate on. This should be the server(s) that you added the cardbot to.
  • CARDBOT_ALLOWED_CHANNELS: A comma separated list of channel IDs the cardbot is allowed to communicate in.

(Note: to easily obtain server and channel ID's enable User Settings -> Advanced -> Enable Developer Mode in Discord. This will reveal a "Copy ID" item in the settings panel for servers and channels to retrieve these ID's.)

Getting Started

You need to build the hub via yarn build, or start watching for live rebuilds with yarn rebuild.

The following command will create a hub_development database on your locally running postgres server

    createdb hub_development
    yarn db:migrate up
    dist/hub.js db seed
    dist/hub.js db dump
    NODE_ENV=test dist/hub.js db init

Running the hub

# Starts the server on port 3000
dist/hub.js server

# Starts the worker process
dist/hub.js worker

# Starts the discord bot
dist/hub.js bot

# If you want to run both in the same terminal you can run
yarn start

Database migrations

# Run available migrations
yarn db:migrate up

#To reverse the last migration:
yarn db:migrate down

#To redo the last migration (i.e. down + up):
yarn db:migrate redo

## Creating database migrations
yarn db:migrate create <migration-name>`

Documentation on how to create migration scripts is available at https://salsita.github.io/node-pg-migrate/#/migrations

After you have completed running your new DB migration script create a pg_dump of the DB in the config/structure.sql file using:

dist/hub.js db dump

Application console

To test, debug and call isolated parts of the application within its context.

yarn console starts the application console.

Examples:

Make a DB query (call installed modules)

Hub > const { Client } = require('pg');
Hub > const config = require('config');
Hub > const client = new Client(config.db.url);
Hub > await client.connect();
Hub > await client.query('SELECT * FROM merchant_infos');

Call a service (call application modules)

Hub > const workerClient = await container.lookup('worker-client');
Hub > await workerClient.addJob('persist-off-chain-merchant-info', { id: 1 });

Connecting to the database staging|production database on AWS

Setup AWS Session Manager ssh config

Add the following to your ~/.ssh/config file:

# SSH over Session Manager
host i-* mi-*
    ProxyCommand sh -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"

Lookup the tunneling command and database password:

cd [PROJECTS]/cardstack/infra/configs/hub/[staging|production]
AWS_PROFILE=cardstack terraform output | grep tunnel_to_database
AWS_PROFILE=cardstack terraform output | grep postgres_password

Run the command, open a postgres client, and connect to localhost, port 55432 with username cardstack, password as looked up in previous step.

Provided APIs

APIs conform to the JSON API specification.

GET /api/exchange-rates

GET /api/prepaid-card-patterns

GET /api/prepaid-card-color-schemes

POST /api/prepaid-card-customizations

POST /api/merchant-infos

GET /api/merchant-infos/validate-slug/:slug

The Hub CLI

The hub CLI can be invoked from within the hub package

dist/hub.js

💡 Tip: Add export PATH="./bin:$PATH" to your .zshenv or .bash_profile to be to invoke hub directly (without the bin/)

The files that support the CLI are in the cli/ directory. You can add your own by following these instructions. The full yargs api can be found here.

The Card Compiler

All compiler functionality is currently hidden behind the COMPILER feature flag. So to start the server with card compiling and related routes, use that flag.

COMPILER=true dist/hub.js server

Contributing

Note that this package is written in TypeScript, so be sure to run a TypesScript compiler as you work. See the project-wide README for information about running the Hub and its tests locally.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.28.71latest
0.2.2-alpha.00alpha
0.2.11-container-alpha.30container-alpha
0.2.12-dotbc.01dotbc

Version History

VersionDownloads (Last 7 Days)Published
0.28.71
0.28.21
0.28.11
0.28.01
0.27.441
0.27.431
0.27.422
0.27.411
0.27.401
0.27.381
0.27.371
0.27.361
0.27.351
0.27.341
0.27.331
0.27.321
0.27.311
0.27.301
0.27.291
0.27.281
0.27.261
0.27.251
0.27.241
0.27.231
0.27.221
0.27.201
0.27.191
0.27.181
0.27.171
0.27.161
0.27.151
0.27.141
0.27.131
0.27.111
0.27.101
0.27.61
0.27.51
0.27.41
0.27.31
0.27.21
0.27.11
0.27.01
0.26.101
0.26.91
0.26.81
0.26.51
0.26.41
0.26.31
0.26.21
0.26.11
0.26.01
0.25.22
0.25.11
0.25.01
0.24.51
0.24.42
0.24.31
0.24.21
0.24.11
0.23.01
0.22.11
0.22.01
0.21.52
0.21.31
0.21.22
0.21.11
0.21.01
0.19.501
0.19.492
0.19.481
0.19.472
0.19.461
0.19.451
0.19.441
0.19.431
0.19.422
0.19.411
0.19.401
0.19.391
0.19.381
0.19.371
0.19.361
0.19.341
0.19.332
0.19.322
0.19.301
0.19.291
0.19.261
0.19.252
0.19.241
0.19.231
0.19.221
0.19.211
0.19.192
0.19.181
0.19.171
0.19.161
0.19.151
0.19.142
0.19.131
0.19.121
0.19.112
0.19.101
0.19.91
0.19.81
0.19.71
0.19.61
0.19.51
0.19.41
0.19.31
0.19.22
0.19.11
0.19.01
0.14.38-patch.00
0.18.171
0.14.24-patch.10
0.18.161
0.18.151
0.18.141
0.18.132
0.18.121
0.18.111
0.18.101
0.18.91
0.18.82
0.18.41
0.18.31
0.18.21
0.18.11
0.17.02
0.16.321
0.16.312
0.16.301
0.16.291
0.16.281
0.16.271
0.16.261
0.16.251
0.16.241
0.16.231
0.16.221
0.16.211
0.16.201
0.16.191
0.16.181
0.16.171
0.16.161
0.16.151
0.16.141
0.16.131
0.16.121
0.16.111
0.16.101
0.16.91
0.16.81
0.16.71
0.16.61
0.16.51
0.16.41
0.16.31
0.16.21
0.16.11
0.16.01
0.15.01
0.14.502
0.14.491
0.14.481
0.14.471
0.14.461
0.14.451
0.14.431
0.14.421
0.14.412
0.14.401
0.14.391
0.14.381
0.14.371
0.14.361
0.14.351
0.14.341
0.14.331
0.14.321
0.14.311
0.14.301
0.14.291
0.14.282
0.14.272
0.14.261
0.14.251
0.14.241
0.14.24-alpha.00
0.14.231
0.14.221
0.14.211
0.14.202
0.14.181
0.14.171
0.14.161
0.14.102
0.14.51
0.14.02
0.13.711
0.13.701
0.13.691
0.13.681
0.13.671
0.13.661
0.13.651
0.13.631
0.13.621
0.13.611
0.13.601
0.13.591
0.13.581
0.13.571
0.13.561
0.13.541
0.13.531
0.13.521
0.13.511
0.13.501
0.13.481
0.13.392
0.13.371
0.13.362
0.13.351
0.13.341
0.13.331
0.13.321
0.13.311
0.13.301
0.13.292
0.13.281
0.13.272
0.13.261
0.13.251
0.13.241
0.13.231
0.13.221
0.13.211
0.13.201
0.13.191
0.13.181
0.13.171
0.13.161
0.13.152
0.13.141
0.13.131
0.13.121
0.13.112
0.13.101
0.13.91
0.13.61
0.13.52
0.13.41
0.13.21
0.13.11
0.13.01
0.12.211
0.12.201
0.12.191
0.12.181
0.12.171
0.12.161
0.12.152
0.12.141
0.12.132
0.12.121
0.12.111
0.12.102
0.12.92
0.12.81
0.12.71
0.12.7-alpha.10
0.12.62
0.12.51
0.12.42
0.12.31
0.12.21
0.12.11
0.12.02
0.11.101
0.11.92
0.11.82
0.11.72
0.11.61
0.11.52
0.11.42
0.11.31
0.11.3-alpha.00
0.11.22
0.11.11
0.11.01
0.10.25-alpha.21
0.10.251
0.10.242
0.10.231
0.10.221
0.10.211
0.10.201
0.10.181
0.10.171
0.10.161
0.10.152
0.10.141
0.10.131
0.10.121
0.10.111
0.10.102
0.10.91
0.10.82
0.10.71
0.10.61
0.10.51
0.10.41
0.10.31
0.10.21
0.10.11
0.10.01
0.9.162
0.9.151
0.9.141
0.9.131
0.9.111
0.9.91
0.9.81
0.9.61
0.9.51
0.9.41
0.9.21
0.9.01
0.8.31
0.8.01
0.7.121
0.7.101
0.7.12
0.7.01
0.6.191
0.6.181
0.6.162
0.6.141
0.6.131
0.6.121
0.6.111
0.6.92
0.6.81
0.6.71
0.6.62
0.6.52
0.6.32
0.6.21
0.6.01
0.5.131
0.5.111
0.5.101
0.5.92
0.5.82
0.5.71
0.5.61
0.5.51
0.5.41
0.5.31
0.5.21
0.5.02
0.4.131
0.4.121
0.4.61
0.4.52
0.4.41
0.4.11
0.4.01
0.3.41
0.3.21
0.3.11
0.3.01
0.2.12-dotbc.01
0.2.121
0.2.11-dotbc.20
0.2.11-dotbc.10
0.2.11-dotbc.01
0.2.11-container-alpha.30
0.2.111
0.2.11-alpha.00
0.2.10-alpha.20
0.2.101
0.2.10-alpha.10
0.2.10-alpha.00
0.3.0-container-alpha.20
0.3.0-container-alpha.10
0.3.0-container-alpha.00
0.2.81
0.2.71
0.2.61
0.2.51
0.2.4-alpha.10
0.2.41
0.2.4-alpha.00
0.2.31
0.2.21
0.2.2-alpha.20
0.2.2-alpha.10
0.2.2-alpha.00
0.2.11
0.2.01
0.0.20-alpha.40
0.1.31
0.0.20-alpha.30
0.0.20-alpha.20
0.0.20-alpha.10
0.0.20-alpha.00
0.1.3-alpha.10
0.1.3-alpha.00
0.1.21
0.1.11
0.1.01
0.0.161
0.0.151
0.0.15-alpha.20
0.0.15-alpha.10
0.0.141
0.0.131
0.0.121
0.0.111
0.0.101
0.0.91
0.0.81
0.0.71
0.0.61
0.0.21

Package Sidebar

Install

npm i @cardstack/hub

Weekly Downloads

461

Version

0.28.7

License

MIT

Unpacked Size

131 MB

Total Files

1770

Last publish

Collaborators

  • tintinthong
  • backspace
  • pcjun97
  • jurgen
  • burcunoyan
  • ef4
  • habdelra
  • lukemelia