Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release to main #4

Merged
merged 14 commits into from
Jul 1, 2024
79 changes: 79 additions & 0 deletions .github/workflows/vercel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Vercel

env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}

on:
push:
branches:
- main
- beta
- develop
pull_request:
release:
types:
- published
workflow_dispatch:

jobs:
vercel:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./api

environment:
name: ${{ github.ref_name == 'main' && 'Production' || 'Preview' }}
url: ${{ github.ref_name == 'main' && 'https://runes-api.vercel.app/' || 'https://runehook-pbcblockstack-hirosystems.vercel.app/' }}

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version-file: 'api/.nvmrc'

- name: Cache node modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: |
~/.npm
**/node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install deps
run: npm ci --audit=false

- name: Install Vercel CLI
run: npm install --global vercel@latest

- name: Pull Vercel environment information
run: vercel pull --yes --environment=${{ github.ref_name == 'main' && 'production' || 'preview' }} --token=${{ secrets.VERCEL_TOKEN }}

- name: Build project artifacts
run: vercel build ${{ github.ref_name == 'main' && '--prod' || '' }} --token=${{ secrets.VERCEL_TOKEN }} --cwd=./

- name: Deploy project artifacts to Vercel
id: deploy
run: vercel ${{ github.ref_name == 'main' && '--prod' || 'deploy' }} --prebuilt --token=${{ secrets.VERCEL_TOKEN }} | awk '{print "deployment_url="$1}' >> $GITHUB_OUTPUT

- name: Trigger docs.hiro.so deployment
if: github.ref_name == 'main'
run: curl -X POST ${{ secrets.VERCEL_DOCS_DEPLOY_HOOK_URL }}

- name: Add comment with Vercel deployment URL
if: ${{ github.event_name == 'pull_request' }}
uses: thollander/actions-comment-pull-request@v2
with:
comment_tag: vercel
message: |
Vercel deployment URL: ${{ steps.deploy.outputs.deployment_url }} :rocket:
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Runehook.toml
.DS_Store
12 changes: 6 additions & 6 deletions api/package-lock.json

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

5 changes: 4 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"start": "node dist/src/index.js",
"start-ts": "ts-node ./src/index.ts",
"test": "jest --runInBand",
"generate:openapi": "rimraf ./tmp && node -r ts-node/register ./util/openapi-generator.ts",
"generate:docs": "redoc-cli build --output ./tmp/index.html ./tmp/openapi.yaml",
"generate:git-info": "rimraf .git-info && node_modules/.bin/api-toolkit-git-info",
"generate:vercel": "npm run generate:git-info && npm run generate:openapi && npm run generate:docs",
"lint:eslint": "eslint . --ext .ts,.tsx -f unix",
"lint:prettier": "prettier --check src/**/*.ts tests/**/*.ts",
"lint:unused-exports": "ts-unused-exports tsconfig.json --showLineNumber --excludePathsFromReport=util/*"
Expand Down Expand Up @@ -48,7 +51,7 @@
"@fastify/multipart": "^7.1.0",
"@fastify/swagger": "^8.3.1",
"@fastify/type-provider-typebox": "3.2.0",
"@hirosystems/api-toolkit": "^1.5.0",
"@hirosystems/api-toolkit": "^1.6.0",
"@types/node": "^18.13.0",
"bignumber.js": "^9.1.2",
"env-schema": "^5.2.1",
Expand Down
41 changes: 11 additions & 30 deletions api/src/api/init.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,32 @@
import FastifyCors from '@fastify/cors';
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import { PINO_LOGGER_CONFIG, isProdEnv } from '@hirosystems/api-toolkit';
import Fastify, { FastifyPluginAsync } from 'fastify';
import FastifyMetrics, { IFastifyMetrics } from 'fastify-metrics';
import { buildFastifyApiServer } from '@hirosystems/api-toolkit';
import { FastifyPluginAsync } from 'fastify';

Check warning on line 3 in api/src/api/init.ts

View check run for this annotation

Codecov / codecov/patch

api/src/api/init.ts#L2-L3

Added lines #L2 - L3 were not covered by tests
import { Server } from 'http';
import { PgStore } from '../pg/pg-store';
import { EtchingRoutes } from './routes/etchings';
import { AddressRoutes } from './routes/addresses';
import { TransactionRoutes } from './routes/transactions';
import { BlockRoutes } from './routes/blocks';
import { StatusRoutes } from './routes/status';

Check warning on line 10 in api/src/api/init.ts

View check run for this annotation

Codecov / codecov/patch

api/src/api/init.ts#L7-L10

Added lines #L7 - L10 were not covered by tests

export const Api: FastifyPluginAsync<
Record<never, never>,
Server,
TypeBoxTypeProvider
> = async fastify => {
await fastify.register(StatusRoutes);

Check warning on line 17 in api/src/api/init.ts

View check run for this annotation

Codecov / codecov/patch

api/src/api/init.ts#L17

Added line #L17 was not covered by tests
await fastify.register(EtchingRoutes);
await fastify.register(AddressRoutes);
await fastify.register(TransactionRoutes);
await fastify.register(BlockRoutes);

Check warning on line 21 in api/src/api/init.ts

View check run for this annotation

Codecov / codecov/patch

api/src/api/init.ts#L19-L21

Added lines #L19 - L21 were not covered by tests
};

export async function buildApiServer(args: { db: PgStore }) {
const fastify = Fastify({
trustProxy: true,
logger: PINO_LOGGER_CONFIG,
}).withTypeProvider<TypeBoxTypeProvider>();
const fastify = await buildFastifyApiServer();

Check warning on line 25 in api/src/api/init.ts

View check run for this annotation

Codecov / codecov/patch

api/src/api/init.ts#L25

Added line #L25 was not covered by tests

fastify.decorate('db', args.db);
if (isProdEnv) {
await fastify.register(FastifyMetrics, { endpoint: null });
}
await fastify.register(FastifyCors);
await fastify.register(Api, { prefix: '/runes/v1' });
await fastify.register(Api, { prefix: '/runes' });

return fastify;
}

export async function buildPromServer(args: { metrics: IFastifyMetrics }) {
const promServer = Fastify({
trustProxy: true,
logger: PINO_LOGGER_CONFIG,
});

promServer.route({
url: '/metrics',
method: 'GET',
logLevel: 'info',
handler: async (_, reply) => {
await reply.type('text/plain').send(await args.metrics.client.register.metrics());
},
});

return promServer;
}
51 changes: 51 additions & 0 deletions api/src/api/routes/addresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import { Type } from '@sinclair/typebox';
import { FastifyPluginCallback } from 'fastify';
import { Server } from 'http';
import { AddressSchema, LimitSchema, OffsetSchema, BalanceResponseSchema } from '../schemas';
import { parseBalanceResponse } from '../util/helpers';
import { Optional, PaginatedResponse } from '@hirosystems/api-toolkit';
import { handleCache } from '../util/cache';

export const AddressRoutes: FastifyPluginCallback<
Record<never, never>,
Server,
TypeBoxTypeProvider
> = (fastify, options, done) => {
fastify.addHook('preHandler', handleCache);

fastify.get(
'/addresses/:address/balances',
{
schema: {
operationId: 'getAddressBalances',
summary: 'Address balances',
description: 'Retrieves a paginated list of address balances',
tags: ['Balances'],
params: Type.Object({
address: AddressSchema,
}),
querystring: Type.Object({
offset: Optional(OffsetSchema),
limit: Optional(LimitSchema),
}),
response: {
200: PaginatedResponse(BalanceResponseSchema, 'Paginated balances response'),
},
},
},
async (request, reply) => {
const offset = request.query.offset ?? 0;
const limit = request.query.limit ?? 20;
const results = await fastify.db.getAddressBalances(request.params.address, offset, limit);
await reply.send({
limit,
offset,
total: results.total,
results: results.results.map(r => parseBalanceResponse(r)),
});
}
);

done();
};

Check warning on line 51 in api/src/api/routes/addresses.ts

View check run for this annotation

Codecov / codecov/patch

api/src/api/routes/addresses.ts#L2-L51

Added lines #L2 - L51 were not covered by tests
51 changes: 51 additions & 0 deletions api/src/api/routes/blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import { Type } from '@sinclair/typebox';
import { FastifyPluginCallback } from 'fastify';
import { Server } from 'http';
import { LimitSchema, OffsetSchema, ActivityResponseSchema, BlockSchema } from '../schemas';
import { parseActivityResponse } from '../util/helpers';
import { Optional, PaginatedResponse } from '@hirosystems/api-toolkit';
import { handleCache } from '../util/cache';

export const BlockRoutes: FastifyPluginCallback<
Record<never, never>,
Server,
TypeBoxTypeProvider
> = (fastify, options, done) => {
fastify.addHook('preHandler', handleCache);

fastify.get(
'/blocks/:block/activity',
{
schema: {
operationId: 'getBlockActivity',
summary: 'Block activity',
description: 'Retrieves a paginated list of rune activity for a block',
tags: ['Activities'],
params: Type.Object({
block: BlockSchema,
}),
querystring: Type.Object({
offset: Optional(OffsetSchema),
limit: Optional(LimitSchema),
}),
response: {
200: PaginatedResponse(ActivityResponseSchema, 'Paginated activity response'),
},
},
},
async (request, reply) => {
const offset = request.query.offset ?? 0;
const limit = request.query.limit ?? 20;
const results = await fastify.db.getBlockActivity(request.params.block, offset, limit);
await reply.send({
limit,
offset,
total: results.total,
results: results.results.map(r => parseActivityResponse(r)),
});
}
);

done();
};

Check warning on line 51 in api/src/api/routes/blocks.ts

View check run for this annotation

Codecov / codecov/patch

api/src/api/routes/blocks.ts#L2-L51

Added lines #L2 - L51 were not covered by tests
Loading
Loading