Skip to content

Commit

Permalink
feat(server): use the lambda handler directly (#1870)
Browse files Browse the repository at this point in the history
* feat(server): use the lambda handler directly

This gives a much better logging output by default

This also enforces pretty printing of logs when run from the console

* refactor: apply lint fix

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
blacha and kodiakhq[bot] authored Sep 22, 2021
1 parent 6c7bdc9 commit 408ff56
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 78 deletions.
6 changes: 0 additions & 6 deletions packages/cli/src/cli/base.cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { GitTag } from '@basemaps/shared/build/cli/git.tag.js';
import { CommandLineParser } from '@rushstack/ts-command-line';
import { readFileSync } from 'fs';
import path from 'path';
import { PrettyTransform } from 'pretty-json-log';
import 'source-map-support/register.js';
import * as ulid from 'ulid';
import url from 'url';
Expand Down Expand Up @@ -35,11 +34,6 @@ export abstract class BaseCommandLine extends CommandLineParser {
});

protected onExecute(): Promise<void> {
// If the console is a tty pretty print the output
if (process.stdout.isTTY) {
LogConfig.setOutputStream(PrettyTransform.stream());
}

if (this.verbose.value) {
LogConfig.get().level = 'debug';
} else if (this.extraVerbose.value) {
Expand Down
6 changes: 0 additions & 6 deletions packages/cli/src/cli/basemaps/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env node
import { LogConfig } from '@basemaps/shared';
import { PrettyTransform } from 'pretty-json-log';
import 'source-map-support/register.js';
import { BaseCommandLine } from '../base.cli.js';
import { TileSetInvalidateAction } from './action.invalidate.js';
Expand All @@ -14,10 +12,6 @@ export class BasemapsCommandLine extends BaseCommandLine {
});
this.addAction(new TileSetInfoAction());
this.addAction(new TileSetInvalidateAction());

if (process.stdout.isTTY) {
LogConfig.setOutputStream(PrettyTransform.stream());
}
}
}

Expand Down
9 changes: 3 additions & 6 deletions packages/lambda-tiler/src/cli/dump.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { Nztm2000Tms } from '@basemaps/geo';
import { LambdaAlbRequest } from '@linzjs/lambda';
import { LogConfig } from '@basemaps/shared';
import { ImageFormat } from '@basemaps/tiler';
import { LambdaAlbRequest } from '@linzjs/lambda';
import { Context } from 'aws-lambda';
import { promises as fs } from 'fs';
import { PrettyTransform } from 'pretty-json-log';
import { TileRoute } from '../routes/tile.js';
import { TileSet } from '../tile.set.js';
import { TileSets } from '../tile.set.cache.js';
import { TileSet } from '../tile.set.js';
import { TileSetLocal } from './tile.set.local.js';
import { Context } from 'aws-lambda';

if (process.stdout.isTTY) LogConfig.setOutputStream(PrettyTransform.stream());

const xyz = { x: 0, y: 0, z: 0 };
const tileMatrix = Nztm2000Tms;
Expand Down
3 changes: 0 additions & 3 deletions packages/lambda-tiler/src/cli/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { HttpHeader, LambdaAlbRequest, LambdaHttpRequest } from '@linzjs/lambda'
import { Context } from 'aws-lambda';
import express from 'express';
import path from 'path';
import { PrettyTransform } from 'pretty-json-log';
import 'source-map-support/register.js';
import * as ulid from 'ulid';
import url from 'url';
Expand All @@ -19,8 +18,6 @@ const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const app = express();
const port = Env.getNumber('PORT', 5050);

if (process.stdout.isTTY) LogConfig.setOutputStream(PrettyTransform.stream());

async function handleRequest(
ctx: LambdaHttpRequest,
res: express.Response<any>,
Expand Down
3 changes: 2 additions & 1 deletion packages/server/bin/basemaps-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import { BasemapsServerCommand } from '../build/cli.js';
import Errors from '@oclif/errors/handle.js';
BasemapsServerCommand.run().catch((error) => {

BasemapsServerCommand.run(void 0, import.meta.url).catch((error) => {
if (error.oclif) return Errors(error);
console.log(error);
});
7 changes: 2 additions & 5 deletions packages/server/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { ConfigProviderMemory, parseRgba } from '@basemaps/config';
// Configure the logging before importing everything
import { ConfigPrefix, ConfigProviderMemory, parseRgba } from '@basemaps/config';
import { Config, Env, LogConfig } from '@basemaps/shared';
import { fsa } from '@linzjs/s3fs';
import { Command, flags } from '@oclif/command';
import { PrettyTransform } from 'pretty-json-log';
import { ConfigPrefix } from '@basemaps/config';
import { BasemapsServer } from './server.js';

if (process.stdout.isTTY) LogConfig.setOutputStream(PrettyTransform.stream());

const logger = LogConfig.get();

export class BasemapsServerCommand extends Command {
Expand Down
70 changes: 30 additions & 40 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,40 @@
import { HttpHeader, HttpHeaderRequestId, LambdaAlbRequest } from '@linzjs/lambda';
import { handleRequest } from '@basemaps/lambda-tiler';
import * as ulid from 'ulid';
import { handler } from '@basemaps/lambda-tiler';
import { ALBEvent, ALBResult, APIGatewayProxyResultV2, CloudFrontRequestResult, Context } from 'aws-lambda';
import ulid from 'ulid';
import express from 'express';
import { LogConfig } from '@basemaps/shared';
import { Context } from 'aws-lambda';
import { lf } from '@linzjs/lambda';

export const BasemapsServer = express();

BasemapsServer.get('/v1/*', async (req: express.Request, res: express.Response) => {
const startTime = Date.now();
const requestId = ulid.ulid();
const logger = LogConfig.get().child({ id: requestId });
const ctx = new LambdaAlbRequest(
{
httpMethod: 'get',
path: req.path,
queryStringParameters: req.query,
} as any,
{} as Context,
logger,
);
function isAlbResult(r: ALBResult | CloudFrontRequestResult | APIGatewayProxyResultV2): r is ALBResult {
if (typeof r !== 'object') return false;
if (r == null) return false;
return 'statusCode' in r;
}

const instanceId = ulid.ulid();

if (ctx.header(HttpHeaderRequestId.RequestId) == null) {
ctx.headers.set(HttpHeaderRequestId.RequestId, 'c' + requestId);
}
const ifNoneMatch = req.header(HttpHeader.IfNoneMatch);
if (ifNoneMatch != null && !Array.isArray(ifNoneMatch)) {
ctx.headers.set(HttpHeader.IfNoneMatch.toLowerCase(), ifNoneMatch);
}
BasemapsServer.get('/v1/*', async (req: express.Request, res: express.Response) => {
const event: ALBEvent = {
httpMethod: 'GET',
requestContext: { elb: { targetGroupArn: 'arn:fake' } },
path: req.path,
headers: req.headers as Record<string, string>,
queryStringParameters: req.query as Record<string, string>,
body: null,
isBase64Encoded: false,
};
if (req.query['api'] == null) req.query['api'] = 'c' + instanceId;

try {
const data = await handleRequest(ctx);
res.status(data.status);
if (data.headers) {
for (const [header, value] of data.headers) {
res.header(header, String(value));
}
handler(event, {} as Context, (err, r) => {
if (err || !isAlbResult(r)) {
lf.Logger.fatal({ err }, 'RequestFailed');
return res.end(err);
}
if (data.status < 299 && data.status > 199) res.end(data.body);
else res.end();

const duration = Date.now() - startTime;
logger.info({ ...ctx.logContext, metrics: ctx.timer.metrics, status: data.status, duration }, 'Request:Done');
} catch (e) {
logger.fatal({ ...ctx.logContext, err: e }, 'Request:Failed');
res.status(500);
res.status(r.statusCode);
for (const [key, value] of Object.entries(r.headers ?? {})) res.header(key, String(value));
if (r.body) res.send(Buffer.from(r.body, r.isBase64Encoded ? 'base64' : 'utf8'));
res.end();
}
});
});
15 changes: 4 additions & 11 deletions packages/shared/src/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pino from 'pino';
import { Writable } from 'stream';
import { PrettyTransform } from 'pretty-json-log';

export interface LogFunc {
(msg: string): void;
Expand All @@ -21,14 +21,16 @@ export interface LogType {
fatal: LogFunc;
child: (obj: Record<string, unknown>) => LogType;
}

const defaultOpts = { level: 'debug' };
/**
* Encapsulate the logger so that it can be swapped out
*/
export const LogConfig = {
/** Get the currently configured logger */
get(): LogType {
if (currentLog == null) {
currentLog = pino({ level: 'debug' });
currentLog = process.stdout.isTTY ? pino(defaultOpts, PrettyTransform.stream()) : pino(defaultOpts);
}
return currentLog;
},
Expand All @@ -37,15 +39,6 @@ export const LogConfig = {
currentLog = log;
},

/** Overwrite the logger with a new logger that outputs to the provided stream*/
setOutputStream(stream: Writable): void {
let level = 'debug';
if (currentLog) {
level = currentLog.level;
}
currentLog = pino({ level }, stream);
},

/** Disable the logger */
disable(): void {
LogConfig.get().level = 'silent';
Expand Down

0 comments on commit 408ff56

Please sign in to comment.