Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/api/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export interface ApiServer {
forceKill: () => Promise<void>;
}

/** API version as given by .git-info */
export const API_VERSION: { branch?: string; commit?: string; tag?: string } = {};

export async function startApiServer(opts: {
datastore: PgStore;
writeDatastore?: PgWriteStore;
Expand All @@ -70,6 +73,15 @@ export async function startApiServer(opts: {
}): Promise<ApiServer> {
const { datastore, writeDatastore, chainId, serverHost, serverPort, httpLogLevel } = opts;

try {
const [branch, commit, tag] = fs.readFileSync('.git-info', 'utf-8').split('\n');
API_VERSION.branch = branch;
API_VERSION.commit = commit;
API_VERSION.tag = tag;
} catch (error) {
logger.error(`Unable to read API version from .git-info`, error);
}

const app = express();
const apiHost = serverHost ?? process.env['STACKS_BLOCKCHAIN_API_HOST'];
const apiPort = serverPort ?? parseInt(process.env['STACKS_BLOCKCHAIN_API_PORT'] ?? '');
Expand Down Expand Up @@ -128,6 +140,15 @@ export async function startApiServer(opts: {
});
app.use(promMiddleware);
}
// Add API version to header
app.use((_, res, next) => {
res.setHeader(
'X-API-Version',
`${API_VERSION.tag} (${API_VERSION.branch}:${API_VERSION.commit})`
);
res.append('Access-Control-Expose-Headers', 'X-API-Version');
next();
});
// Setup request logging
app.use(
expressWinston.logger({
Expand Down
5 changes: 2 additions & 3 deletions src/api/routes/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { ServerStatusResponse } from '@stacks/stacks-blockchain-api-types';
import { logger } from '../../helpers';
import { getETagCacheHandler, setETagCacheHeaders } from '../controllers/cache-controller';
import { PgStore } from '../../datastore/pg-store';
import { API_VERSION } from '../init';

export function createStatusRouter(db: PgStore): express.Router {
const router = express.Router();
const cacheHandler = getETagCacheHandler(db);
const statusHandler = async (_: Request, res: any) => {
try {
const [branch, commit, tag] = fs.readFileSync('.git-info', 'utf-8').split('\n');
const response: ServerStatusResponse = {
server_version: `stacks-blockchain-api ${tag} (${branch}:${commit})`,
server_version: `stacks-blockchain-api ${API_VERSION.tag} (${API_VERSION.branch}:${API_VERSION.commit})`,
status: 'ready',
};
const chainTip = await db.getUnanchoredChainTip();
Expand All @@ -28,7 +28,6 @@ export function createStatusRouter(db: PgStore): express.Router {
setETagCacheHeaders(res);
res.json(response);
} catch (error) {
logger.error(`Unable to read git info`, error);
const response: ServerStatusResponse = {
status: 'ready',
};
Expand Down