Skip to content

Commit

Permalink
feat: add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
3mcd committed Jul 18, 2022
1 parent f99b296 commit f823199
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Required environment variables:

Optional environment variables:

- `ACCESS_TOKEN` — a simple token to authenticate consumers. Must be incldued in an `Authorization` header to the `/convert` endpoint if specified, otherwise the server will respond with a 401: Unauthorized. **Placeholder until proper auth is implemented.**
- `ACCESS_KEY` — a simple token to authenticate consumers. Must be incldued in an `Authorization` header to the `/convert` endpoint if specified, otherwise the server will respond with a 401: Unauthorized. **Placeholder until proper auth is implemented.**
- `CONVERT_BODY_LIMIT` — (default: 50mb) Integer expressing an upper limit in bytes of request body for the `/convert` endpoint
- `SENTRY_DSN` — Sentry.io data source name

Expand Down
2 changes: 1 addition & 1 deletion fly.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# fly.toml file generated for pubstash on 2022-07-14T12:04:49-04:00
# fly.toml file generated for pubstash on 2022-07-14T17:45:16-04:00

app = "pubstash"
kill_signal = "SIGINT"
Expand Down
41 changes: 32 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { installSentry } from "./sentry.js";
import { StorageProviderRegistry } from "./storage/index.js";
import { has, Maybe } from "./utils.js";

let conversionCount = 0;

class InvalidParameterError extends Error {
name = "InvalidParameterError";
status = 400;
Expand Down Expand Up @@ -87,17 +89,38 @@ router
accessKeyAuth(accessKey),
koaBody({ textLimit: convertBodyLimit }),
async (ctx) => {
switch (ctx.query.format) {
case "pdf": {
const url = await convertToPDF(ctx.request.body);
ctx.body = { url };
break;
const id = conversionCount++;
const start = performance.now();
console.log(
`convert start ${id} format=${ctx.query.format} byte_length=${ctx.request.length}`
);
try {
switch (ctx.query.format) {
case "pdf": {
const url = await convertToPDF(ctx.request.body);
ctx.body = { url };
break;
}
default:
throw new InvalidParameterError(
"Missing or unsupported query parameter: format"
);
}
default:
throw new InvalidParameterError(
"Missing or unsupported query parameter: format"
);
} catch (error) {
console.log(
`convert failure ${id} duration=${(
(performance.now() - start) /
1000
).toFixed(3)}s`
);
throw error;
}
console.log(
`convert success ${id} duration=${(
(performance.now() - start) /
1000
).toFixed(3)}s`
);
}
);

Expand Down

0 comments on commit f823199

Please sign in to comment.