@cardstack/hub

中文文档
0.28.7发布于 2021/12/24MIT近 30 天下载 3027

Stock API server for the Cardstack tech stack.

原始使用文档
文档目录21 节
  1. Architecture
  2. Configuration
  3. Setting up Discord
  4. Getting Started
  5. Running the hub
  6. Database migrations
  7. Application console
  8. Make a DB query (call installed modules)
  9. Call a service (call application modules)
  10. Connecting to the database staging|production database on A…
  11. Setup AWS Session Manager ssh config
  12. Provided APIs
  13. GET /api/exchange-rates
  14. GET /api/prepaid-card-patterns
  15. GET /api/prepaid-card-color-schemes
  16. POST /api/prepaid-card-customizations
  17. POST /api/merchant-infos
  18. GET /api/merchant-infos/validate-slug/:slug
  19. The Hub CLI
  20. The Card Compiler
  21. Contributing

@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.

版本与发布记录

@cardstack/hub 共发布 429 个版本,首次发布于 2017/03/29;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.28.7latest2021/12/244 年前
0.28.22021/12/174 年前
0.28.12021/12/164 年前
0.28.02021/12/164 年前
0.27.442021/12/154 年前
0.27.432021/12/154 年前

其他发布通道:alpha → 0.2.2-alpha.0,container-alpha → 0.2.11-container-alpha.3,dotbc → 0.2.12-dotbc.0。

关于 @cardstack/hub 的常见问题

@cardstack/hub 是什么?

Stock API server for the Cardstack tech stack.

如何安装 @cardstack/hub?

在项目目录运行 npm install @cardstack/hub,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 cardstack-hub,可以用 npx cardstack-hub 直接运行。

@cardstack/hub 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cardstack__hub 是否存在。

@cardstack/hub 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^14.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cardstack/hub 可以商用吗?

@cardstack/hub 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cardstack/hub 的下载量有多少?

根据 npm 官方下载统计,@cardstack/hub 在 2026/10/09 – 2026/11/07 这 30 天内被下载 3,027 次,日均约 101 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cardstack/hub 还在维护吗?

最新版本 0.28.7 发布于 2021/12/24(4 年前);至今共发布 429 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/03/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。