1
0
mirror of https://github.com/barthuijgen/factorio-sites.git synced 2025-01-24 11:46:19 +02:00

Make GCP setting configurable

This commit is contained in:
Bart Huijgen 2021-03-09 21:28:16 +01:00
parent b8d5cfc507
commit 2a79219f9c
7 changed files with 33 additions and 22 deletions

View File

@ -11,6 +11,7 @@ import { renderImage } from "./image-renderer";
export async function subscribeToPubSub() { export async function subscribeToPubSub() {
const topic = getBlueprintImageRequestTopic(); const topic = getBlueprintImageRequestTopic();
if (!topic) throw Error("PubSub Topic not found");
const [subscription] = await topic const [subscription] = await topic
.subscription("blueprint-image-function-app", { .subscription("blueprint-image-function-app", {
flowControl: { allowExcessMessages: false, maxMessages: 1, maxExtension: 3600 }, flowControl: { allowExcessMessages: false, maxMessages: 1, maxExtension: 3600 },

View File

@ -68,8 +68,7 @@ export const BlueprintLink: React.FC<BlueprintLinkProps> = ({
type = "tile", type = "tile",
}) => { }) => {
const [imageError, setImageError] = useState(false); const [imageError, setImageError] = useState(false);
const onImageError = (error: unknown) => { const onImageError = () => {
console.log(error);
setImageError(true); setImageError(true);
}; };

View File

@ -16,9 +16,11 @@ const handler: NextApiHandler = apiHandler(async (req, res) => {
const blueprintImageRequestTopic = getBlueprintImageRequestTopic(); const blueprintImageRequestTopic = getBlueprintImageRequestTopic();
if (blueprintPage.blueprint_id) { if (blueprintPage.blueprint_id) {
blueprintImageRequestTopic.publishJSON({ if (blueprintImageRequestTopic) {
blueprintId: blueprintPage.blueprint_id, blueprintImageRequestTopic.publishJSON({
}); blueprintId: blueprintPage.blueprint_id,
});
}
return res.json({ blueprint_id: blueprintPage.blueprint_id }); return res.json({ blueprint_id: blueprintPage.blueprint_id });
} else if (blueprintPage.blueprint_book_id) { } else if (blueprintPage.blueprint_book_id) {
const blueprintBook = await getBlueprintBookById(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 firstBlueprintId = getFirstBlueprintFromChildTree(blueprintBook.child_tree);
const firstBlueprint = await getBlueprintById(firstBlueprintId); const firstBlueprint = await getBlueprintById(firstBlueprintId);
if (!firstBlueprint) throw new ApiError(500, "Failed to find blueprint"); if (!firstBlueprint) throw new ApiError(500, "Failed to find blueprint");
blueprintImageRequestTopic.publishJSON({ if (blueprintImageRequestTopic) {
blueprintId: firstBlueprintId, blueprintImageRequestTopic.publishJSON({
}); blueprintId: firstBlueprintId,
});
}
return res.json({ blueprint_id: firstBlueprintId }); return res.json({ blueprint_id: firstBlueprintId });
} }
}); });

View File

@ -7,11 +7,13 @@ import {
hasBlueprintImage, hasBlueprintImage,
} from "@factorio-sites/database"; } from "@factorio-sites/database";
import { Blueprint } from "@factorio-sites/types"; import { Blueprint } from "@factorio-sites/types";
import { apiHandler } from "../../utils/api-handler";
const DISABLED = true; const DISABLED = true;
const getOneMessage = async (): Promise<Blueprint> => { const getOneMessage = async (): Promise<Blueprint> => {
const topic = getBlueprintImageRequestTopic(); const topic = getBlueprintImageRequestTopic();
if (!topic) throw Error("Pubsub TOPIC not found");
const [subscription] = await topic const [subscription] = await topic
.subscription("blueprint-image-function-app", { .subscription("blueprint-image-function-app", {
flowControl: { allowExcessMessages: false, maxMessages: 1 }, flowControl: { allowExcessMessages: false, maxMessages: 1 },
@ -44,7 +46,7 @@ const getOneMessage = async (): Promise<Blueprint> => {
}); });
}; };
const handler: NextApiHandler = async (req, res) => { const handler: NextApiHandler = apiHandler(async (req, res) => {
if (DISABLED) return res.status(400).send("Method not availablee"); if (DISABLED) return res.status(400).send("Method not availablee");
// Allow the url to be used in the blueprint editor // Allow the url to be used in the blueprint editor
@ -67,6 +69,6 @@ const handler: NextApiHandler = async (req, res) => {
string: string, string: string,
}) })
); );
}; });
export default handler; export default handler;

View File

@ -147,15 +147,17 @@ export async function createBlueprintPage(
}); });
const blueprintImageRequestTopic = getBlueprintImageRequestTopic(); const blueprintImageRequestTopic = getBlueprintImageRequestTopic();
if (type === "blueprint") { if (blueprintImageRequestTopic) {
blueprintImageRequestTopic.publishJSON({ if (type === "blueprint") {
blueprintId: targetId, blueprintImageRequestTopic.publishJSON({
}); blueprintId: targetId,
} else if (data.child_tree) { });
const firstBlueprintId = getFirstBlueprintFromChildTree(data.child_tree); } else if (data.child_tree) {
blueprintImageRequestTopic.publishJSON({ const firstBlueprintId = getFirstBlueprintFromChildTree(data.child_tree);
blueprintId: firstBlueprintId, blueprintImageRequestTopic.publishJSON({
}); blueprintId: firstBlueprintId,
});
}
} }
console.log(`Created Blueprint Page`); console.log(`Created Blueprint Page`);

View File

@ -1,9 +1,11 @@
import { PubSub, Message } from "@google-cloud/pubsub"; import { PubSub, Message } from "@google-cloud/pubsub";
import { getEnv } from "./env.util";
const pubsub = new PubSub(); const pubsub = new PubSub();
export function getBlueprintImageRequestTopic() { 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; export type PubSubMessage = Message;

View File

@ -1,9 +1,10 @@
import { Storage } from "@google-cloud/storage"; import { Storage } from "@google-cloud/storage";
import { getEnvOrThrow } from "./env.util";
const storage = new Storage(); const storage = new Storage();
const BLUEPRINT_BUCKET = storage.bucket("blueprint-strings"); const BLUEPRINT_BUCKET = storage.bucket(getEnvOrThrow("GCP_BLUEPRINT_STRINGS_BUCKET"));
const IMAGE_BUCKET = storage.bucket("blueprint-images"); const IMAGE_BUCKET = storage.bucket(getEnvOrThrow("GCP_BLUEPRINT_IMAGES_BUCKET"));
/* /*
* BlueprintString * BlueprintString