Skip to content

Commit

Permalink
Make an aync function to start sever.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentao-Kuang committed Jun 13, 2022
1 parent 31a0f37 commit 735ab70
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/config-cli/src/cli/screenshot/screenshot.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { fsa, LogConfig, LogType } from '@basemaps/shared';
import { Config, fsa, LogConfig, LogType } from '@basemaps/shared';
import { mkdir } from 'fs/promises';
import { Browser, chromium } from 'playwright';
import { CommandLineAction, CommandLineStringParameter } from '@rushstack/ts-command-line';
import { z } from 'zod';
import getPort, { portNumbers } from 'get-port';
import { createServer } from '@basemaps/server';
import { FastifyInstance } from 'fastify/types/instance';
import { ConfigProviderMemory } from '@basemaps/config';
import { ConfigBundled } from '@basemaps/config/src/memory/memory.config';

export const DefaultTestTiles = './test-tiles/default.test.tiles.json';
export const DefaultHost = 'basemaps.linz.govt.nz';
Expand Down Expand Up @@ -76,16 +78,7 @@ export class CommandScreenShot extends CommandLineAction {
if (config != null) {
const port = await getPort({ port: portNumbers(10000, 11000) });
host = `http://localhost:${port}`;
const server = createServer(logger);

if (server == null) throw new Error('Failed to Create server with the config File');
await new Promise<void>((resolve) =>
server.listen(port, '0.0.0.0', () => {
logger.info({ url: host }, 'ServerStarted');
resolve();
}),
);
BasemapsServer = server;
BasemapsServer = await startServer(host, port, config, logger);
}

logger.info('Page:Launch');
Expand Down Expand Up @@ -134,3 +127,17 @@ export async function takeScreenshots(host: string, tiles: string, chrome: Brows
await page.close();
}
}

async function startServer(host: string, port: number, config: string, logger: LogType): Promise<FastifyInstance> {
// Bundle Config
const configJson = await fsa.readJson<ConfigBundled>(config);
const mem = ConfigProviderMemory.fromJson(configJson);
Config.setConfigProvider(mem);

// Start server
const server = createServer(logger);
server.listen(port, '0.0.0.0', () => {
logger.info({ url: host }, 'ServerStarted');
});
return server;
}

0 comments on commit 735ab70

Please sign in to comment.