set fly replay header early

This commit is contained in:
EGOIST 2022-05-08 13:14:52 +08:00
parent d022ef19cf
commit 5547aa972f
4 changed files with 37 additions and 41 deletions

View File

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

View File

@ -17,12 +17,6 @@ export const getServerEnv = <T extends keyof ServerEnv>(
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

View File

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

View File

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