-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
158 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'; | ||
import { FastifyPluginCallback } from 'fastify'; | ||
import { Server } from 'http'; | ||
import { SERVER_VERSION } from '../../server-version'; | ||
import { ApiStatusResponse } from '../schemas'; | ||
|
||
export const StatusRoutes: FastifyPluginCallback< | ||
Record<never, never>, | ||
Server, | ||
TypeBoxTypeProvider | ||
> = (fastify, options, done) => { | ||
fastify.get( | ||
'/', | ||
{ | ||
schema: { | ||
summary: 'API Status', | ||
description: 'Displays the status of the API', | ||
tags: ['Status'], | ||
response: { | ||
200: ApiStatusResponse, | ||
}, | ||
}, | ||
}, | ||
async (request, reply) => { | ||
const result = await fastify.db.sqlTransaction(async sql => { | ||
const block_height = await fastify.db.getChainTipBlockHeight(); | ||
const max_inscription_number = await fastify.db.getMaxInscriptionNumber(); | ||
return { | ||
server_version: `ordinals-api ${SERVER_VERSION.tag} (${SERVER_VERSION.branch}:${SERVER_VERSION.commit})`, | ||
status: 'ready', | ||
block_height, | ||
max_inscription_number, | ||
}; | ||
}); | ||
await reply.send(result); | ||
} | ||
); | ||
done(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { readFileSync } from 'fs'; | ||
|
||
interface ServerVersion { | ||
branch: string; | ||
commit: string; | ||
tag: string; | ||
} | ||
|
||
function getServerVersion(): ServerVersion { | ||
if (process.env.NODE_ENV === 'test') { | ||
return { | ||
branch: 'test', | ||
commit: '123456', | ||
tag: 'v0.0.1', | ||
}; | ||
} | ||
const [branch, commit, tag] = readFileSync('.git-info', 'utf-8').split('\n'); | ||
return { branch, commit, tag }; | ||
} | ||
|
||
export const SERVER_VERSION = getServerVersion(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { buildApiServer } from '../src/api/init'; | ||
import { cycleMigrations } from '../src/pg/migrations'; | ||
import { PgStore } from '../src/pg/pg-store'; | ||
import { TestFastifyServer } from './helpers'; | ||
|
||
describe('Status', () => { | ||
let db: PgStore; | ||
let fastify: TestFastifyServer; | ||
|
||
beforeEach(async () => { | ||
db = await PgStore.connect({ skipMigrations: true }); | ||
fastify = await buildApiServer({ db }); | ||
await cycleMigrations(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await fastify.close(); | ||
await db.close(); | ||
}); | ||
|
||
test('returns status when db is empty', async () => { | ||
const response = await fastify.inject({ method: 'GET', url: '/ordinals/v1/' }); | ||
const json = response.json(); | ||
expect(json).toStrictEqual({ | ||
server_version: 'ordinals-api v0.0.1 (test:123456)', | ||
status: 'ready', | ||
block_height: 0, | ||
}); | ||
const noVersionResponse = await fastify.inject({ method: 'GET', url: '/ordinals/' }); | ||
expect(response.statusCode).toEqual(noVersionResponse.statusCode); | ||
expect(json).toStrictEqual(noVersionResponse.json()); | ||
}); | ||
|
||
test('returns inscriptions total', async () => { | ||
await db.insertInscriptionGenesis({ | ||
inscription: { | ||
genesis_id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dci0', | ||
mime_type: 'image/png', | ||
content_type: 'image/png', | ||
content_length: 5, | ||
number: 7, | ||
content: '0x48656C6C6F', | ||
fee: 2805n, | ||
}, | ||
location: { | ||
inscription_id: 0, | ||
block_height: 775617, | ||
block_hash: '00000000000000000002a90330a99f67e3f01eb2ce070b45930581e82fb7a91d', | ||
tx_id: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc', | ||
address: 'bc1p3cyx5e2hgh53w7kpxcvm8s4kkega9gv5wfw7c4qxsvxl0u8x834qf0u2td', | ||
output: '38c46a8bf7ec90bc7f6b797e7dc84baa97f4e5fd4286b92fe1b50176d03b18dc:0', | ||
offset: 0n, | ||
value: 10000n, | ||
timestamp: 1676913207, | ||
sat_ordinal: 257418248345364n, | ||
sat_rarity: 'common', | ||
sat_coinbase_height: 650000, | ||
genesis: true, | ||
current: true, | ||
}, | ||
}); | ||
|
||
const response = await fastify.inject({ method: 'GET', url: '/ordinals/v1/' }); | ||
const json = response.json(); | ||
expect(json).toStrictEqual({ | ||
server_version: 'ordinals-api v0.0.1 (test:123456)', | ||
status: 'ready', | ||
block_height: 775617, | ||
max_inscription_number: 7, | ||
}); | ||
}); | ||
}); |