Skip to content

Commit

Permalink
feat: return git info in /extended/v1/status
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rafaelcr authored Aug 27, 2021
1 parent 439d4f4 commit 0538ae2
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 5 deletions.
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

0 comments on commit 0538ae2

Please sign in to comment.