1
1
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox' ;
2
- import { buildFastifyApiServer } from '@hirosystems/api-toolkit' ;
2
+ import FastifyCors from '@fastify/cors' ;
3
+ import Fastify , { FastifyInstance } from 'fastify' ;
4
+ import FastifyMetrics , { IFastifyMetrics } from 'fastify-metrics' ;
3
5
import { FastifyPluginAsync } from 'fastify' ;
4
6
import { Server } from 'http' ;
5
7
import { PgStore } from '../pg/pg-store' ;
@@ -8,6 +10,7 @@ import { AddressRoutes } from './routes/addresses';
8
10
import { TransactionRoutes } from './routes/transactions' ;
9
11
import { BlockRoutes } from './routes/blocks' ;
10
12
import { StatusRoutes } from './routes/status' ;
13
+ import { PINO_LOGGER_CONFIG , isProdEnv } from '@hirosystems/api-toolkit' ;
11
14
12
15
export const Api : FastifyPluginAsync <
13
16
Record < never , never > ,
@@ -22,11 +25,35 @@ export const Api: FastifyPluginAsync<
22
25
} ;
23
26
24
27
export async function buildApiServer ( args : { db : PgStore } ) {
25
- const fastify = await buildFastifyApiServer ( ) ;
26
-
28
+ const fastify = Fastify ( {
29
+ trustProxy : true ,
30
+ logger : PINO_LOGGER_CONFIG ,
31
+ } ) . withTypeProvider < TypeBoxTypeProvider > ( ) ;
32
+ if ( isProdEnv ) {
33
+ await fastify . register ( FastifyMetrics , { endpoint : null } ) ;
34
+ }
35
+ await fastify . register ( FastifyCors ) ;
27
36
fastify . decorate ( 'db' , args . db ) ;
28
37
await fastify . register ( Api , { prefix : '/runes/v1' } ) ;
29
38
await fastify . register ( Api , { prefix : '/runes' } ) ;
30
39
31
40
return fastify ;
32
41
}
42
+
43
+ export async function buildPrometheusServer ( args : {
44
+ metrics : IFastifyMetrics ;
45
+ } ) : Promise < FastifyInstance > {
46
+ const promServer = Fastify ( {
47
+ trustProxy : true ,
48
+ logger : PINO_LOGGER_CONFIG ,
49
+ } ) ;
50
+ promServer . route ( {
51
+ url : '/metrics' ,
52
+ method : 'GET' ,
53
+ logLevel : 'info' ,
54
+ handler : async ( _ , reply ) => {
55
+ await reply . type ( 'text/plain' ) . send ( await args . metrics . client . register . metrics ( ) ) ;
56
+ } ,
57
+ } ) ;
58
+ return promServer ;
59
+ }
0 commit comments