import fastify , { FastifyInstance , } from 'fastify' ; import fastifyPlugin from 'fastify-plugin' ; import { ServerInstance } from '../types' ; import { storeToFs } from '../entity/storeToFs' ; import { ModelStore } from '../entity/ModelStore' ; import { verifyCapabilityInvocation } from '../server/zcap/verifyCapabilityInvocation' ; import routes from './routes' ; export const getFastify = ( ) : ServerInstance => { const opts = { } ; const server = fastify ( opts ) ; async function servicesConnector ( server ) { const baseUrl = process . env . EDV_BASE_URL || 'http://localhost:9876' ; server . decorate ( 'baseUrl' , baseUrl ) ; const connectionString = process . env . MONGO_DB_CONNECTION_STRING || 'mongodb://localhost:27017/test' ; const connection = await ModelStore . getConnection ( connectionString ) ; const store = new ModelStore ( connection , true ) ; server . decorate ( 'connectionString' , connectionString ) ; server . decorate ( 'store' , store ) ; server . decorate ( 'verifyCapabilityInvocation' , verifyCapabilityInvocation ) ; server . decorate ( 'storeToFs' , storeToFs ) ; } server . register ( fastifyPlugin ( servicesConnector ) ) ; server . get ( '/' , async ( _request , reply ) => { return reply . status ( 200 ) . send ( { hello : '💾  data vault.' , baseUrl : ( server as any ) . baseUrl } ) ; } ) ; server . register ( routes , { prefix : '/edvs' } ) ; server . addHook ( 'onClose' , async ( instance , done ) => { await ( instance as ServerInstance ) . store . close ( ) ; done ( ) ; } ) ; return server as ServerInstance ; } ;