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
2 changes: 2 additions & 0 deletions src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Router from '@koa/router'
import Koa from 'koa'

import { Config } from '@/types'
import { version } from '@/version'

import { accountRouter } from './account'
import { setUpDocs } from './docs'
Expand Down Expand Up @@ -31,6 +32,7 @@ export const setUpRouter = async (
ctx.status = 200
ctx.body = {
status: 'ok',
version,
timestamp: new Date().toISOString(),
}
})
Expand Down
3 changes: 3 additions & 0 deletions src/server/routes/indexer/getStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { DefaultContext, DefaultState } from 'koa'
import { State } from '@/db'
import { SerializedBlock } from '@/types'
import { serializeBlock } from '@/utils'
import { version } from '@/version'

type GetStatusResponse =
| {
version: string
chainId: string
latestBlock: SerializedBlock
lastStakingBlockHeightExported: string | null
Expand Down Expand Up @@ -35,6 +37,7 @@ export const getStatus: Router.Middleware<

ctx.status = 200
ctx.body = {
version,
chainId: state.chainId,
latestBlock: serializeBlock(state.latestBlock),
lastStakingBlockHeightExported:
Expand Down
5 changes: 5 additions & 0 deletions src/server/routes/indexer/up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DefaultContext, DefaultState } from 'koa'
import { ConfigManager } from '@/config'
import { State } from '@/db'
import { getStargateClient } from '@/utils'
import { version } from '@/version'

type UpBlock = {
height: number
Expand All @@ -13,6 +14,7 @@ type UpBlock = {

type UpResponse =
| {
version: string
chainId: string
remoteBlock: UpBlock
localBlock: UpBlock | { error: string } | null
Expand All @@ -25,6 +27,7 @@ type UpResponse =
}
}
| {
version: string
error: string
}

Expand Down Expand Up @@ -120,6 +123,7 @@ export const up: Router.Middleware<
} catch (err) {
ctx.status = 500
ctx.body = {
version,
error: err instanceof Error ? err.message : `${err}`,
}
return
Expand Down Expand Up @@ -156,6 +160,7 @@ export const up: Router.Middleware<

ctx.status = caughtUp ? 200 : 412
ctx.body = {
version,
chainId: state.chainId,
remoteBlock,
localBlock,
Expand Down
2 changes: 2 additions & 0 deletions src/server/test/indexer/getStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest'

import { State } from '@/db'
import { serializeBlock } from '@/utils'
import { version } from '@/version'

import { app } from './app'

Expand All @@ -16,6 +17,7 @@ describe('GET /status', () => {
.expect(200)
.expect('Content-Type', /json/)
.expect({
version,
chainId: state.chainId,
latestBlock: serializeBlock(state.latestBlock),
lastStakingBlockHeightExported:
Expand Down
1 change: 1 addition & 0 deletions src/server/test/indexer/health.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('health', () => {
.expect(200)
.expect((res) => {
expect(res.body.status).toBe('ok')
expect(res.body.version).toBeTypeOf('string')
expect(res.body.timestamp).toBeTypeOf('string')
})
})
Expand Down
26 changes: 26 additions & 0 deletions src/server/test/indexer/version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import request from 'supertest'
import { describe, expect, it } from 'vitest'

import { version } from '@/version'

import { app } from './app'

describe('version in responses', () => {
it('/health includes version', async () => {
await request(app.callback())
.get('/health')
.expect(200)
.expect((res) => {
expect(res.body.version).toBe(version)
})
})

it('/status includes version', async () => {
await request(app.callback())
.get('/status')
.expect(200)
.expect((res) => {
expect(res.body.version).toBe(version)
})
})
})
10 changes: 10 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { readFileSync } from 'fs'
import { join } from 'path'

// Read version from package.json at startup. Using readFileSync since this
// runs once at import time, not per-request.
const pkg = JSON.parse(
readFileSync(join(__dirname, '../package.json'), 'utf-8')
)

export const version: string = pkg.version
Loading