From 5547aa972fe7b63dab0b77bdc77789b6d7d86084 Mon Sep 17 00:00:00 2001 From: EGOIST Date: Sun, 8 May 2022 13:14:52 +0800 Subject: [PATCH] set fly replay header early --- app/lib/db.server.ts | 4 ++-- app/lib/env.ts | 9 +++------ server/fly.ts | 28 +--------------------------- server/index.ts | 37 +++++++++++++++++++++++++++++++------ 4 files changed, 37 insertions(+), 41 deletions(-) diff --git a/app/lib/db.server.ts b/app/lib/db.server.ts index 53eabcf6..3b1d3d55 100644 --- a/app/lib/db.server.ts +++ b/app/lib/db.server.ts @@ -1,6 +1,6 @@ import { PrismaClient, type Prisma } from "@prisma/client" import { IS_PROD } from "./constants" -import { isPrimaryRegion } from "./env" +import { IS_PRIMARY_REGION } from "./env" import { singleton } from "./singleton.server" const logLevel: Prisma.LogLevel[] = IS_PROD @@ -19,7 +19,7 @@ export const prismaWrite = /* @__PURE__ */ singleton( export const prismaRead = /* @__PURE__ */ singleton("prisma-read", () => { // 5433 is the read-replica port let url = process.env.DATABASE_URL - if (!isPrimaryRegion() && IS_PROD) { + if (!IS_PRIMARY_REGION && IS_PROD) { url = url.replace(":5432", ":5433") } console.log("read replica url", url.replace(/\w+@/, "PASSWORD@")) diff --git a/app/lib/env.ts b/app/lib/env.ts index bdadfe8f..169f041a 100644 --- a/app/lib/env.ts +++ b/app/lib/env.ts @@ -17,12 +17,6 @@ export const getServerEnv = ( return process.env[key] } -export const isPrimaryRegion = () => { - const FLY_REGION = getServerEnv("FLY_REGION") - const PRIMARY_REGION = getServerEnv("PRIMARY_REGION") - return Boolean(FLY_REGION && PRIMARY_REGION === FLY_REGION) -} - // Use /* @__PURE__ */ annotation to make tree-shaking work export const AUTH_COOKIE_NAME = /* @__PURE__ */ getServerEnv("AUTH_COOKIE_NAME") export const APP_NAME = /* @__PURE__ */ getCommonEnv("APP_NAME") @@ -38,3 +32,6 @@ export const S3_ENDPOINT = /* @__PURE__ */ getServerEnv("S3_ENDPOINT") export const MAILGUN_APIKEY = /* @__PURE__ */ getServerEnv("MAILGUN_APIKEY") export const MAILGUN_DOMAIN = /* @__PURE__ */ getServerEnv("MAILGUN_DOMAIN") export const MAILGUN_EU = /* @__PURE__ */ getServerEnv("MAILGUN_EU") +export const PRIMARY_REGION = /* @__PURE__ */ getServerEnv("PRIMARY_REGION") +export const FLY_REGION = /* @__PURE__ */ getServerEnv("FLY_REGION") +export const IS_PRIMARY_REGION = !FLY_REGION || PRIMARY_REGION === FLY_REGION diff --git a/server/fly.ts b/server/fly.ts index 564c7b90..165938b6 100644 --- a/server/fly.ts +++ b/server/fly.ts @@ -1,33 +1,7 @@ import { type RequestHandler } from "express" -import { IS_PROD } from "~/lib/constants" -import { getServerEnv, isPrimaryRegion } from "~/lib/env" - -const PRIMARY_REGION = getServerEnv("PRIMARY_REGION") -const FLY_REGION = getServerEnv("FLY_REGION") +import { FLY_REGION } from "~/lib/env" export const setFlyRegionHeader: RequestHandler = (req, res, next) => { res.setHeader("x-fly-region", FLY_REGION || "unknown") next() } - -export const getReplayResponse: RequestHandler = (req, res, next) => { - const { method, path: pathname } = req - - if ( - !IS_PROD || - ["GET", "OPTIONS", "HEAD"].includes(method) || - isPrimaryRegion() - ) { - return next() - } - - const logInfo = { - pathname, - method, - PRIMARY_REGION, - FLY_REGION, - } - console.info(`Replaying:`, logInfo) - res.set("fly-replay", `region=${PRIMARY_REGION}`) - return res.sendStatus(409) -} diff --git a/server/index.ts b/server/index.ts index 6932a664..667b5baa 100644 --- a/server/index.ts +++ b/server/index.ts @@ -1,18 +1,18 @@ import "./globals/index" +import http from "http" import path from "path" import express from "express" import compression from "compression" import morgan from "morgan" import { createRequestHandler } from "@remix-run/express" -import { getReplayResponse, setFlyRegionHeader } from "./fly" +import { setFlyRegionHeader } from "./fly" import { IS_PROD } from "~/lib/constants" +import { FLY_REGION, IS_PRIMARY_REGION, PRIMARY_REGION } from "~/lib/env" const BUILD_DIR = path.join(process.cwd(), "build") const app = express() -app.all("*", getReplayResponse) - app.use(setFlyRegionHeader) if (IS_PROD) { @@ -55,9 +55,34 @@ app.all( ) const port = process.env.PORT || 3000 -app.listen(port, () => { - console.log(`Open http://localhost:${port}`) -}) +http + .createServer((req, res) => { + const { method, url } = req + + if ( + IS_PROD && + method && + !["GET", "OPTIONS", "HEAD"].includes(method) && + !IS_PRIMARY_REGION + ) { + const logInfo = { + url, + method, + PRIMARY_REGION, + FLY_REGION, + } + console.info(`Replaying:`, logInfo) + res.setHeader("fly-replay", `region=${PRIMARY_REGION}`) + res.statusCode = 409 + res.end("replay") + return + } + + return app(req, res) + }) + .listen(port, () => { + console.log(`Open http://localhost:${port}`) + }) function purgeRequireCache() { // purge require cache on requests for "server side HMR" this won't let