feat: return git info in /extended/v1/status

* feat: add build script to write git info to usable ts file

* fix: run git-info before tests

* fix: fetch all git history for running tests

* fix: try moving git-info run to workflow

* fix: add some debug info to setup

* fix: try using the module as a relative route

* chore: remove test commands now that tests pass

* chore: create status endpoint openapi schema and example

* feat: create a plain text file instead

* chore: undo github workflow changes since they are no longer necessary

* fix: make import paths relative
This commit is contained in:
Rafael Cárdenas
2021-08-27 14:24:26 -05:00
committed by GitHub
parent 439d4f46cd
commit 0538ae297f
9 changed files with 82 additions and 5 deletions

3
.gitignore vendored
View File

@@ -123,3 +123,6 @@ openapitools.json
.env.local
yarn.lock
# Git info output
.git-info

View File

@@ -0,0 +1,4 @@
{
"server_version": "stacks-blockchain-api v0.64.1 (master:439d4f46)",
"status": "ready"
}

View File

@@ -0,0 +1,20 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "GET blockchain API status",
"title": "ServerStatusResponse",
"type": "object",
"additionalProperties": false,
"required": [
"status"
],
"properties": {
"server_version": {
"type": "string",
"description": "the server version that is currently running"
},
"status": {
"type": "string",
"description": "the current server status"
}
}
}

14
docs/generated.d.ts vendored
View File

@@ -39,6 +39,7 @@ export type SchemaMergeRootStub =
| RunFaucetResponse
| NetworkBlockTimeResponse
| NetworkBlockTimesResponse
| ServerStatusResponse
| GetStxCirculatingSupplyPlainResponse
| GetStxSupplyLegacyFormatResponse
| GetStxTotalSupplyPlainResponse
@@ -1531,6 +1532,19 @@ export interface NetworkBlockTimesResponse {
target_block_time: number;
};
}
/**
* GET blockchain API status
*/
export interface ServerStatusResponse {
/**
* the server version that is currently running
*/
server_version?: string;
/**
* the current server status
*/
status: string;
}
/**
* GET request that returns network target block times
*/

View File

@@ -1399,9 +1399,10 @@ paths:
description: Success
content:
application/json:
examples:
success:
value: { status: 'ready' }
schema:
$ref: ./api/info/get-status.schema.json
example:
$ref: ./api/info/get-status.example.json
/extended/v1/info/network_block_times:
get:

View File

@@ -16,7 +16,8 @@
"test:integration:rosetta": "npm run devenv:deploy -- -d && cross-env NODE_ENV=development jest --config ./jest.config.rosetta.js --coverage --no-cache --runInBand; npm run devenv:stop",
"test:integration:bns": "npm run devenv:deploy -- -d && cross-env NODE_ENV=development jest --config ./jest.config.bns.js --coverage --no-cache --runInBand; npm run devenv:stop",
"test:integration:microblocks": "npm run devenv:deploy:pg -- -d && cross-env NODE_ENV=development jest --config ./jest.config.microblocks.js --coverage --no-cache --runInBand; npm run devenv:stop:pg",
"build": "rimraf ./lib && tsc -p tsconfig.build.json",
"git-info": "echo \"$(git rev-parse --abbrev-ref HEAD)\n$(git log -1 --pretty=format:%h)\n$(git describe --tags --abbrev=0)\" > ./.git-info",
"build": "npm run git-info && rimraf ./lib && tsc -p tsconfig.build.json",
"build:tests": "tsc -p tsconfig.json",
"build:docs": "npm i --prefix client && npm run generate:docs --prefix client && npm i --prefix docs && npm run generate:docs --prefix docs",
"start": "node ./lib/index.js",

View File

@@ -40,6 +40,7 @@ import * as pathToRegex from 'path-to-regexp';
import * as expressListEndpoints from 'express-list-endpoints';
import { createMiddleware as createPrometheusMiddleware } from '@promster/express';
import { createMicroblockRouter } from './routes/microblock';
import { createStatusRouter } from './routes/status';
export interface ApiServer {
expressApp: ExpressWithAsync;
@@ -149,7 +150,7 @@ export async function startApiServer(opts: {
router.use('/info', createInfoRouter(datastore));
router.use('/stx_supply', createStxSupplyRouter(datastore));
router.use('/debug', createDebugRouter(datastore));
router.use('/status', (req, res) => res.status(200).json({ status: 'ready' }));
router.use('/status', createStatusRouter(datastore));
router.use('/faucets', createFaucetRouter(datastore));
return router;
})()

29
src/api/routes/status.ts Normal file
View File

@@ -0,0 +1,29 @@
import * as express from 'express';
import * as fs from 'fs';
import { addAsync, RouterWithAsync } from '@awaitjs/express';
import { DataStore } from '../../datastore/common';
import { ServerStatusResponse } from '@stacks/stacks-blockchain-api-types';
import { logger } from '../../helpers';
export function createStatusRouter(_: DataStore): RouterWithAsync {
const router = addAsync(express.Router());
router.get('/', (_, res) => {
try {
const [branch, commit, tag] = fs.readFileSync('.git-info', 'utf-8').split('\n');
const response: ServerStatusResponse = {
server_version: `stacks-blockchain-api ${tag} (${branch}:${commit})`,
status: 'ready',
};
res.json(response);
} catch (error) {
logger.error(`Unable to read git info`, error);
const response: ServerStatusResponse = {
status: 'ready',
};
res.json(response);
}
});
return router;
}

View File

@@ -137,6 +137,10 @@ async function init(): Promise<void> {
});
}
if (isProdEnv && !fs.existsSync('.git-info')) {
throw new Error(`Git info file not found`);
}
const apiServer = await startApiServer({ datastore: db, chainId: getConfiguredChainID() });
logger.info(`API server listening on: http://${apiServer.address}`);
registerShutdownConfig({