From 2a79219f9cdc7d80e527d1db43562599c3e1135e Mon Sep 17 00:00:00 2001 From: Bart Huijgen Date: Tue, 9 Mar 2021 21:28:16 +0100 Subject: [PATCH] Make GCP setting configurable --- .../src/pubsub-render.ts | 1 + .../src/components/BlueprintLink.tsx | 3 +-- .../src/pages/api/blueprint/thumbnail.ts | 16 +++++++++------ apps/blueprints/src/pages/api/image-to-gen.ts | 6 ++++-- libs/database/src/lib/data/blueprint_page.ts | 20 ++++++++++--------- libs/database/src/lib/gcp-pubsub.ts | 4 +++- libs/database/src/lib/gcp-storage.ts | 5 +++-- 7 files changed, 33 insertions(+), 22 deletions(-) diff --git a/apps/blueprint-image-function/src/pubsub-render.ts b/apps/blueprint-image-function/src/pubsub-render.ts index 6a8d20c..e23d155 100644 --- a/apps/blueprint-image-function/src/pubsub-render.ts +++ b/apps/blueprint-image-function/src/pubsub-render.ts @@ -11,6 +11,7 @@ import { renderImage } from "./image-renderer"; export async function subscribeToPubSub() { const topic = getBlueprintImageRequestTopic(); + if (!topic) throw Error("PubSub Topic not found"); const [subscription] = await topic .subscription("blueprint-image-function-app", { flowControl: { allowExcessMessages: false, maxMessages: 1, maxExtension: 3600 }, diff --git a/apps/blueprints/src/components/BlueprintLink.tsx b/apps/blueprints/src/components/BlueprintLink.tsx index 6902055..578f6e4 100644 --- a/apps/blueprints/src/components/BlueprintLink.tsx +++ b/apps/blueprints/src/components/BlueprintLink.tsx @@ -68,8 +68,7 @@ export const BlueprintLink: React.FC = ({ type = "tile", }) => { const [imageError, setImageError] = useState(false); - const onImageError = (error: unknown) => { - console.log(error); + const onImageError = () => { setImageError(true); }; diff --git a/apps/blueprints/src/pages/api/blueprint/thumbnail.ts b/apps/blueprints/src/pages/api/blueprint/thumbnail.ts index a078370..13aabdf 100644 --- a/apps/blueprints/src/pages/api/blueprint/thumbnail.ts +++ b/apps/blueprints/src/pages/api/blueprint/thumbnail.ts @@ -16,9 +16,11 @@ const handler: NextApiHandler = apiHandler(async (req, res) => { const blueprintImageRequestTopic = getBlueprintImageRequestTopic(); if (blueprintPage.blueprint_id) { - blueprintImageRequestTopic.publishJSON({ - blueprintId: blueprintPage.blueprint_id, - }); + if (blueprintImageRequestTopic) { + blueprintImageRequestTopic.publishJSON({ + blueprintId: blueprintPage.blueprint_id, + }); + } return res.json({ blueprint_id: blueprintPage.blueprint_id }); } else if (blueprintPage.blueprint_book_id) { const blueprintBook = await getBlueprintBookById(blueprintPage.blueprint_book_id); @@ -26,9 +28,11 @@ const handler: NextApiHandler = apiHandler(async (req, res) => { const firstBlueprintId = getFirstBlueprintFromChildTree(blueprintBook.child_tree); const firstBlueprint = await getBlueprintById(firstBlueprintId); if (!firstBlueprint) throw new ApiError(500, "Failed to find blueprint"); - blueprintImageRequestTopic.publishJSON({ - blueprintId: firstBlueprintId, - }); + if (blueprintImageRequestTopic) { + blueprintImageRequestTopic.publishJSON({ + blueprintId: firstBlueprintId, + }); + } return res.json({ blueprint_id: firstBlueprintId }); } }); diff --git a/apps/blueprints/src/pages/api/image-to-gen.ts b/apps/blueprints/src/pages/api/image-to-gen.ts index f44ea17..3a4a07b 100644 --- a/apps/blueprints/src/pages/api/image-to-gen.ts +++ b/apps/blueprints/src/pages/api/image-to-gen.ts @@ -7,11 +7,13 @@ import { hasBlueprintImage, } from "@factorio-sites/database"; import { Blueprint } from "@factorio-sites/types"; +import { apiHandler } from "../../utils/api-handler"; const DISABLED = true; const getOneMessage = async (): Promise => { const topic = getBlueprintImageRequestTopic(); + if (!topic) throw Error("Pubsub TOPIC not found"); const [subscription] = await topic .subscription("blueprint-image-function-app", { flowControl: { allowExcessMessages: false, maxMessages: 1 }, @@ -44,7 +46,7 @@ const getOneMessage = async (): Promise => { }); }; -const handler: NextApiHandler = async (req, res) => { +const handler: NextApiHandler = apiHandler(async (req, res) => { if (DISABLED) return res.status(400).send("Method not availablee"); // Allow the url to be used in the blueprint editor @@ -67,6 +69,6 @@ const handler: NextApiHandler = async (req, res) => { string: string, }) ); -}; +}); export default handler; diff --git a/libs/database/src/lib/data/blueprint_page.ts b/libs/database/src/lib/data/blueprint_page.ts index 28271fc..029eea4 100644 --- a/libs/database/src/lib/data/blueprint_page.ts +++ b/libs/database/src/lib/data/blueprint_page.ts @@ -147,15 +147,17 @@ export async function createBlueprintPage( }); const blueprintImageRequestTopic = getBlueprintImageRequestTopic(); - if (type === "blueprint") { - blueprintImageRequestTopic.publishJSON({ - blueprintId: targetId, - }); - } else if (data.child_tree) { - const firstBlueprintId = getFirstBlueprintFromChildTree(data.child_tree); - blueprintImageRequestTopic.publishJSON({ - blueprintId: firstBlueprintId, - }); + if (blueprintImageRequestTopic) { + if (type === "blueprint") { + blueprintImageRequestTopic.publishJSON({ + blueprintId: targetId, + }); + } else if (data.child_tree) { + const firstBlueprintId = getFirstBlueprintFromChildTree(data.child_tree); + blueprintImageRequestTopic.publishJSON({ + blueprintId: firstBlueprintId, + }); + } } console.log(`Created Blueprint Page`); diff --git a/libs/database/src/lib/gcp-pubsub.ts b/libs/database/src/lib/gcp-pubsub.ts index c06a784..441bec6 100644 --- a/libs/database/src/lib/gcp-pubsub.ts +++ b/libs/database/src/lib/gcp-pubsub.ts @@ -1,9 +1,11 @@ import { PubSub, Message } from "@google-cloud/pubsub"; +import { getEnv } from "./env.util"; const pubsub = new PubSub(); export function getBlueprintImageRequestTopic() { - return pubsub.topic("projects/factorio-sites/topics/blueprint-image-request"); + const topic = getEnv("GCP_BLUEPRINT_IMAGES_PUBSUB"); + return topic ? pubsub.topic(topic) : null; } export type PubSubMessage = Message; diff --git a/libs/database/src/lib/gcp-storage.ts b/libs/database/src/lib/gcp-storage.ts index 4cd63f2..f6ac48b 100644 --- a/libs/database/src/lib/gcp-storage.ts +++ b/libs/database/src/lib/gcp-storage.ts @@ -1,9 +1,10 @@ import { Storage } from "@google-cloud/storage"; +import { getEnvOrThrow } from "./env.util"; const storage = new Storage(); -const BLUEPRINT_BUCKET = storage.bucket("blueprint-strings"); -const IMAGE_BUCKET = storage.bucket("blueprint-images"); +const BLUEPRINT_BUCKET = storage.bucket(getEnvOrThrow("GCP_BLUEPRINT_STRINGS_BUCKET")); +const IMAGE_BUCKET = storage.bucket(getEnvOrThrow("GCP_BLUEPRINT_IMAGES_BUCKET")); /* * BlueprintString