Skip to content

Commit

Permalink
Update stuff, add authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
mbenedettini committed Sep 23, 2024
1 parent f979424 commit c8a172c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 37 deletions.
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ services:
- .:/app
ports:
- 8080:8080
# - 9229:9229
environment:
- CHROME_PORT=9222
- CHROME_HOSTNAME=chrome
# - ECS=true
- AUTHORIZATION_KEY=1234567890
4 changes: 2 additions & 2 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import puppeteer from "https://deno.land/x/puppeteer@9.0.2/mod.ts";
import * as oak from "https://deno.land/x/oak@v9.0.1/mod.ts";
import puppeteer from "npm:puppeteer[email protected]";
import * as oak from "https://deno.land/x/oak@v17.0.0/mod.ts";

export {
puppeteer,
Expand Down
34 changes: 11 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,25 @@ import { html2pdf } from "./puppeteer.ts";
const app = new oak.Application();
const router = new oak.Router();

router.get("/", ctx => {
router.get("/", (ctx: oak.Context) => {
ctx.response.body = {
message: "Hello world"
};
});


router.post("/html2pdf", async ctx => {
if (ctx.request.hasBody) {
const formData: oak.BodyFormData = await ctx.request.body({ type: "form-data" }); // oak can also automatically identify the type
const fdr: oak.FormDataReader = await formData.value;
const actualResults: [string, string | oak.FormDataFile][] = [];
for await (const result of fdr.stream({ maxSize: 400000 })) {
actualResults.push(result);
}
const firstRawHTML = (actualResults[0][1] as oak.FormDataFile).content as Uint8Array;
const html = new TextDecoder().decode(firstRawHTML);
const pdf = await html2pdf(html);
ctx.response.body = pdf;
ctx.response.type = 'application/pdf';
router.post("/html2pdf", async (ctx: oak.Context) => {
if (!ctx.request.hasBody) {
return;
}
});

router.post("/html2pdf:json", async ctx => {
const result = ctx.request.body();

if (result.type !== "json") {
const authorizationKey = Deno.env.get("AUTHORIZATION_KEY");
if (Boolean(authorizationKey) && ctx.request.headers.get("Authorization") !== authorizationKey) {
ctx.response.status = 401;
return;
}
const value = await result.value;
const html = value.value as string;

const formData = await ctx.request.body.formData();
const htmlFile = formData.get("html") as File;
const html = new TextDecoder().decode(await htmlFile.arrayBuffer());
const pdf = await html2pdf(html);
ctx.response.body = pdf;
ctx.response.type = 'application/pdf';
Expand Down
11 changes: 1 addition & 10 deletions src/puppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import Hosts from 'https://deno.land/x/[email protected]/mod.ts';


async function html2pdf(html: string, browserURL?: string) {
let host = '';
if (!browserURL) {
let host = '';
/*
Expand All @@ -19,15 +18,7 @@ async function html2pdf(html: string, browserURL?: string) {
provided in fetch(), and there aren't other reliable http client
implementations.
*/
if (Deno.env.get('ECS')) {
console.log(">>>>>>>>>>>>>>>>>>> ECS");
const hostsPath = "/etc/hosts"; // Hosts file path
const hosts = new Hosts(Deno.readTextFileSync(hostsPath));
host = hosts.resolve(Deno.env.get('CHROME_HOSTNAME') as string) as string;
} else {
host = (await Deno.resolveDns(Deno.env.get('CHROME_HOSTNAME') as string, 'A'))[0];
}
console.log(host);
host = (await Deno.resolveDns(Deno.env.get('CHROME_HOSTNAME') as string, 'A'))[0];
const port = Deno.env.get('CHROME_PORT');
browserURL = `http://${host}:${port}`;
}
Expand Down

0 comments on commit c8a172c

Please sign in to comment.