Skip to content

Commit 1e998db

Browse files
feat: add API version in response header (#1216)
* feat: added stacks version in header * fix: resolved error on reading from git-info * fix: refactored file reading to read only once * fix: append cors header, read .git-info only once Co-authored-by: Rafael Cárdenas <[email protected]>
1 parent 9482238 commit 1e998db

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/api/init.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export interface ApiServer {
5858
forceKill: () => Promise<void>;
5959
}
6060

61+
/** API version as given by .git-info */
62+
export const API_VERSION: { branch?: string; commit?: string; tag?: string } = {};
63+
6164
export async function startApiServer(opts: {
6265
datastore: PgStore;
6366
writeDatastore?: PgWriteStore;
@@ -70,6 +73,15 @@ export async function startApiServer(opts: {
7073
}): Promise<ApiServer> {
7174
const { datastore, writeDatastore, chainId, serverHost, serverPort, httpLogLevel } = opts;
7275

76+
try {
77+
const [branch, commit, tag] = fs.readFileSync('.git-info', 'utf-8').split('\n');
78+
API_VERSION.branch = branch;
79+
API_VERSION.commit = commit;
80+
API_VERSION.tag = tag;
81+
} catch (error) {
82+
logger.error(`Unable to read API version from .git-info`, error);
83+
}
84+
7385
const app = express();
7486
const apiHost = serverHost ?? process.env['STACKS_BLOCKCHAIN_API_HOST'];
7587
const apiPort = serverPort ?? parseInt(process.env['STACKS_BLOCKCHAIN_API_PORT'] ?? '');
@@ -128,6 +140,15 @@ export async function startApiServer(opts: {
128140
});
129141
app.use(promMiddleware);
130142
}
143+
// Add API version to header
144+
app.use((_, res, next) => {
145+
res.setHeader(
146+
'X-API-Version',
147+
`${API_VERSION.tag} (${API_VERSION.branch}:${API_VERSION.commit})`
148+
);
149+
res.append('Access-Control-Expose-Headers', 'X-API-Version');
150+
next();
151+
});
131152
// Setup request logging
132153
app.use(
133154
expressWinston.logger({

src/api/routes/status.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import { ServerStatusResponse } from '@stacks/stacks-blockchain-api-types';
44
import { logger } from '../../helpers';
55
import { getETagCacheHandler, setETagCacheHeaders } from '../controllers/cache-controller';
66
import { PgStore } from '../../datastore/pg-store';
7+
import { API_VERSION } from '../init';
78

89
export function createStatusRouter(db: PgStore): express.Router {
910
const router = express.Router();
1011
const cacheHandler = getETagCacheHandler(db);
1112
const statusHandler = async (_: Request, res: any) => {
1213
try {
13-
const [branch, commit, tag] = fs.readFileSync('.git-info', 'utf-8').split('\n');
1414
const response: ServerStatusResponse = {
15-
server_version: `stacks-blockchain-api ${tag} (${branch}:${commit})`,
15+
server_version: `stacks-blockchain-api ${API_VERSION.tag} (${API_VERSION.branch}:${API_VERSION.commit})`,
1616
status: 'ready',
1717
};
1818
const chainTip = await db.getUnanchoredChainTip();
@@ -28,7 +28,6 @@ export function createStatusRouter(db: PgStore): express.Router {
2828
setETagCacheHeaders(res);
2929
res.json(response);
3030
} catch (error) {
31-
logger.error(`Unable to read git info`, error);
3231
const response: ServerStatusResponse = {
3332
status: 'ready',
3433
};

0 commit comments

Comments
 (0)