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

return git info in /extended/v1/status #724

Merged
merged 11 commits into from
Aug 27, 2021
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,6 @@ openapitools.json

.env.local
yarn.lock

# Git info output
.git-info
4 changes: 4 additions & 0 deletions docs/api/info/get-status.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"server_version": "stacks-blockchain-api v0.64.1 (master:439d4f46)",
"status": "ready"
}
20 changes: 20 additions & 0 deletions docs/api/info/get-status.schema.json
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions docs/generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type SchemaMergeRootStub =
| RunFaucetResponse
| NetworkBlockTimeResponse
| NetworkBlockTimesResponse
| ServerStatusResponse
| GetStxCirculatingSupplyPlainResponse
| GetStxSupplyLegacyFormatResponse
| GetStxTotalSupplyPlainResponse
Expand Down Expand Up @@ -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
*/
Expand Down
7 changes: 4 additions & 3 deletions docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/api/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
})()
Expand Down
29 changes: 29 additions & 0 deletions src/api/routes/status.ts
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down