import { ConfigDI, container } from '@jig-software/sdk';
import { TRestClient } from '@jig-software/trest-client';
import { AppConfig } from './config';
import { healthCheckContract } from './shared';

async function healthCheck() {
  const appConfig = await container.create(
    ConfigDI.config(AppConfig, {
      configPath: './volumes/config.json',
    }),
  );
  const { port, apiPrefix } = appConfig;
  const baseUrl = apiPrefix
    ? `http://localhost:${port}/${apiPrefix.replace(/^\//, '')}`
    : `http://localhost:${port}/`;
  const healthCheckClient = new TRestClient(healthCheckContract, baseUrl);
  await healthCheckClient.actions.checkHealth().unwrap();
  console.info('OK');
}

healthCheck().catch((err) => {
  console.error(err);
  process.exit(1);
});
