1
0
mirror of https://github.com/barthuijgen/factorio-sites.git synced 2025-01-09 14:45:40 +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() {
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 },

View File

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

View File

@ -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 });
}
});

View File

@ -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<Blueprint> => {
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<Blueprint> => {
});
};
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;

View File

@ -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`);

View File

@ -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;

View File

@ -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