Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): support loading config from dynamodb #2119

Merged
merged 2 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/server/bin/basemaps-server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

if (process.env.AWS_REGION == null) process.env.AWS_REGION = process.env.AWS_DEFAULT_REGION;

import { BasemapsServerCommand } from '../build/cli.js';
import Errors from '@oclif/errors/handle.js';

Expand Down
50 changes: 34 additions & 16 deletions packages/server/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Configure the logging before importing everything
import { ConfigPrefix, ConfigProvider, ConfigProviderMemory, parseRgba } from '@basemaps/config';
import { ConfigPrefix, ConfigProvider, ConfigProviderDynamo, ConfigProviderMemory, parseRgba } from '@basemaps/config';
import { TileSetLocal } from '@basemaps/lambda-tiler/build/cli/tile.set.local.js';
import { TileSets } from '@basemaps/lambda-tiler/build/tile.set.cache.js';
import { Config, Env, LogConfig } from '@basemaps/shared';
Expand All @@ -24,26 +24,21 @@ const BaseProvider: ConfigProvider = {

export class BasemapsServerCommand extends Command {
static description = 'Create a WMTS/XYZ Tile server for basemaps config';
static flags = { verbose: flags.boolean(), port: flags.integer({ default: 5000 }) };
static flags = {
verbose: flags.boolean(),
port: flags.integer({ default: 5000 }),
dynamo: flags.string({ description: 'Dynamodb table', required: false }),
};

static args = [{ name: 'configPath', required: true }];

async run(): Promise<void> {
const { args, flags } = this.parse(BasemapsServerCommand);
if (flags.verbose) logger.level = 'debug';

const ServerUrl = `http://localhost:${flags.port}`;
// Force a default url base so WMTS requests know their relative url
process.env[Env.PublicUrlBase] = process.env[Env.PublicUrlBase] ?? `http://localhost:${flags.port}`;

logger.info({ path: args.configPath }, 'Starting Server');
static args = [{ name: 'configPath', required: false }];

async loadFromPath(configPath: string, serverUrl: string): Promise<void> {
const config = new ConfigProviderMemory();
Config.setConfigProvider(config);

const tifSets = new Map<string, TileSetLocal>();

for await (const file of fsa.listDetails(args.configPath)) {
for await (const file of fsa.listDetails(configPath)) {
const lowerPath = file.path.toLowerCase();
if (lowerPath.endsWith('.tiff') || lowerPath.endsWith('.tif')) {
const tiffPath = dirname(file.path);
Expand All @@ -55,7 +50,7 @@ export class BasemapsServerCommand extends Command {
await tsl.load();
TileSets.add(tsl, new Date('3000-01-01').getTime());

const wmtsUrl = `${ServerUrl}/v1/tiles/${tileSet}/WMTSCapabilities.xml`;
const wmtsUrl = `${serverUrl}/v1/tiles/${tileSet}/WMTSCapabilities.xml`;
logger.info({ tileSetId: tileSet, wmtsUrl }, 'TileSet:Loaded');
if (!config.objects.has('pv_linz')) config.put(BaseProvider);
}
Expand All @@ -78,9 +73,32 @@ export class BasemapsServerCommand extends Command {

const tileSet = Config.unprefix(ConfigPrefix.TileSet, jsonData.id);
if (jsonData.name == null) jsonData.name = tileSet;
const wmtsUrl = `${ServerUrl}/v1/tiles/${tileSet}/WMTSCapabilities.xml`;
const wmtsUrl = `${serverUrl}/v1/tiles/${tileSet}/WMTSCapabilities.xml`;
logger.info({ tileSetId: jsonData.id, wmtsUrl }, 'TileSet:Loaded');
}
}

async run(): Promise<void> {
const { args, flags } = this.parse(BasemapsServerCommand);
if (flags.verbose) logger.level = 'debug';

if (args.configPath == null && flags.dynamo == null) {
}

const ServerUrl = `http://localhost:${flags.port}`;

if (args.configPath != null) {
logger.info({ path: args.configPath }, 'Starting Server');

await this.loadFromPath(args.configPath, ServerUrl);
} else if (flags.dynamo != null) {
logger.info({ dynamo: flags.dynamo }, 'Starting Server');
Config.setConfigProvider(new ConfigProviderDynamo(flags.dynamo));
} else {
throw new Error('Either a configuration path or dynamodb table name must be supplied');
}
// Force a default url base so WMTS requests know their relative url
process.env[Env.PublicUrlBase] = process.env[Env.PublicUrlBase] ?? `http://localhost:${flags.port}`;

BasemapsServer.listen(flags.port, () => {
logger.info({ url: ServerUrl }, 'ServerStarted');
Expand Down