-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify server entry (#408)
- Loading branch information
Showing
20 changed files
with
324 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createMiddleware } from "@hattip/adapter-node"; | ||
import { createHattipEntry } from "./entry-hattip"; | ||
|
||
// we can reuse express middleware both for dev and vercel (by patching @remix-run/dev) | ||
// https://github.com/hattipjs/hattip/blob/03a704fa120dfe2eddd6cf22eff00c90bda2acb5/packages/bundler/bundler-vercel/readme.md | ||
|
||
export default createMiddleware(createHattipEntry(), { | ||
trustProxy: true, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { type RequestHandler, compose } from "@hattip/compose"; | ||
import { once } from "@hiogawa/utils"; | ||
import * as build from "@remix-run/dev/server-build"; | ||
import { createRequestHandler } from "@remix-run/server-runtime"; | ||
import { fetchRequestHandler } from "@trpc/server/adapters/fetch"; | ||
import type { Context } from "hono"; | ||
import { logger } from "hono/logger"; | ||
import { TRPC_ENDPOINT } from "../trpc/common"; | ||
import { createTrpcAppContext } from "../trpc/context"; | ||
import { trpcApp } from "../trpc/server"; | ||
import { initializeServer } from "./initialize-server"; | ||
|
||
// based on https://github.com/hi-ogawa/vite-fullstack-example/blob/92649f99b041820ec86650c99cfcd49a72e79f71/src/server/hattip.ts#L16-L28 | ||
|
||
export function createHattipEntry() { | ||
return compose( | ||
createLogger(), | ||
bootstrapHandler(), | ||
createTrpchandler(), | ||
createRemixHandler() | ||
); | ||
} | ||
|
||
function createRemixHandler(): RequestHandler { | ||
const mode = | ||
process.env.NODE_ENV === "production" ? "production" : "development"; | ||
const remixHandler = createRequestHandler(build, mode); | ||
return (ctx) => remixHandler(ctx.request); | ||
} | ||
|
||
function createTrpchandler(): RequestHandler { | ||
return async (ctx) => { | ||
if (!ctx.url.pathname.startsWith(TRPC_ENDPOINT)) { | ||
return ctx.next(); | ||
} | ||
return fetchRequestHandler({ | ||
endpoint: TRPC_ENDPOINT, | ||
req: ctx.request, | ||
router: trpcApp, | ||
createContext: createTrpcAppContext, | ||
onError: (e) => { | ||
console.error(e); | ||
}, | ||
}); | ||
}; | ||
} | ||
|
||
function bootstrapHandler(): RequestHandler { | ||
const initializeServerOnce = once(initializeServer); | ||
return async (ctx) => { | ||
await initializeServerOnce(); | ||
return ctx.next(); | ||
}; | ||
} | ||
|
||
function createLogger(): RequestHandler { | ||
// borrow hono's logger with minimal compatibility hack | ||
// https://github.com/honojs/hono/blob/0ffd795ec6cfb67d38ab902197bb5461a4740b8f/src/middleware/logger/index.ts | ||
const honoLogger = logger(); | ||
return async (ctx) => { | ||
let res!: Response; | ||
await honoLogger( | ||
{ | ||
req: { method: ctx.method, raw: ctx.request }, | ||
get res() { | ||
return res; | ||
}, | ||
} as Context, | ||
async () => { | ||
res = await ctx.next(); | ||
} | ||
); | ||
return res; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,16 @@ | ||
import { once } from "@hiogawa/utils"; | ||
import { installGlobals } from "@remix-run/node"; | ||
import { | ||
finalizeDrizzleClient, | ||
initializeDrizzleClient, | ||
} from "../db/drizzle-client.server"; | ||
import { initializeConfigServer } from "../utils/config"; | ||
import { initializeSessionStore } from "../utils/session.server"; | ||
|
||
export const initializeServer = once(async () => { | ||
installGlobals(); | ||
export async function initializeServer() { | ||
initializeConfigServer(); | ||
initializeSessionStore(); | ||
await initializeDrizzleClient(); | ||
}); | ||
} | ||
|
||
export async function finalizeServer() { | ||
await finalizeDrizzleClient(); | ||
} | ||
|
||
// to workaround async initialization on the server (cf. @remix-run/server-runtime patch) | ||
export function injectInitializeServer() { | ||
Object.assign(globalThis, { __onRequestHandler: initializeServer }); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const TRPC_ENDPOINT = "/trpc"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.