-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e6b4a5
commit 468e736
Showing
404 changed files
with
10,607 additions
and
5,801 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"name": "examples-fastify-server", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"build": "esbuild src/server/index.ts src/client/index.ts --bundle --packages=external --platform=node --format=esm --outdir=dist --sourcemap", | ||
"dev": "dotenv -e ../../.env -- tsx watch src/server", | ||
"lint": "eslint --cache --ext \".js,.ts,.tsx\" src", | ||
"type-check": "tsc", | ||
"start:server": "node dist/server/index.js", | ||
"start": "run-p start:* --print-label", | ||
"test-dev": "start-server-and-test 'tsx src/server' http-get://localhost:2022 'tsx src/client'", | ||
"test-start": "start-server-and-test 'node dist/server' http-get://localhost:2022 'node dist/client'" | ||
}, | ||
"dependencies": { | ||
"@grassroots/api": "workspace:*", | ||
"@grassroots/auth": "workspace:*", | ||
"@grassroots/contracts": "workspace:*", | ||
"@grassroots/db": "workspace:*", | ||
"@grassroots/shared": "workspace:*", | ||
"@grassroots/validators": "workspace:*", | ||
"@trpc/client": "11.0.0-rc.441", | ||
"@trpc/server": "11.0.0-rc.441", | ||
"cors": "^2.8.5", | ||
"express": "^4.17.1", | ||
"iron-session": "^8.0.1", | ||
"kysely": "^0.27.3", | ||
"superjson": "2.2.1", | ||
"tslib": "^2.5.0", | ||
"viem": "catalog:", | ||
"ws": "^8.0.0", | ||
"zod": "catalog:" | ||
}, | ||
"devDependencies": { | ||
"@grassroots/tsconfig": "workspace:*", | ||
"@types/cors": "^2.8.17", | ||
"@types/express": "^4.17.17", | ||
"@types/node": "catalog:", | ||
"@types/ws": "^8.2.0", | ||
"dotenv-cli": "^7.4.2", | ||
"esbuild": "^0.17.10", | ||
"eslint": "^8.57.0", | ||
"npm-run-all": "^4.1.5", | ||
"start-server-and-test": "^1.12.0", | ||
"tsx": "^4.0.0", | ||
"typescript": "^5.4.5", | ||
"wait-port": "^1.0.1" | ||
}, | ||
"publishConfig": { | ||
"access": "restricted" | ||
} | ||
} |
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,7 @@ | ||
import type { ServerOptions } from './server/server'; | ||
|
||
export const serverConfig: ServerOptions = { | ||
dev: false, | ||
port: 2022, | ||
prefix: '/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { serverConfig } from '../config'; | ||
import { createServer } from './server'; | ||
|
||
const server = createServer(serverConfig); | ||
|
||
void server.start(); |
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,20 @@ | ||
import type { NextFunction, Request, Response } from "express"; | ||
import type { IronSession, SessionOptions } from "iron-session"; | ||
import { Session } from "@grassroots/auth"; | ||
import { getIronSession } from "iron-session"; | ||
|
||
declare module "http" { | ||
interface IncomingMessage { | ||
session: IronSession<Session> | null; | ||
} | ||
} | ||
|
||
// middleware | ||
export function ironSession( | ||
sessionOptions: SessionOptions, | ||
): (req: Request, res: Response, next: NextFunction) => Promise<void> { | ||
return async function ironSessionMiddleware(req, res, next) { | ||
req.session = await getIronSession(req, res, sessionOptions); | ||
next(); | ||
}; | ||
} |
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,61 @@ | ||
import type { Server } from "http"; | ||
import { appRouter, createContext } from "@grassroots/api"; | ||
import { sessionOptions } from "@grassroots/auth"; | ||
import * as trpcExpress from "@trpc/server/adapters/express"; | ||
import cors from "cors"; | ||
import express from "express"; | ||
|
||
import { ironSession } from "./iron-session"; | ||
|
||
export interface ServerOptions { | ||
dev?: boolean; | ||
port?: number; | ||
prefix?: string; | ||
} | ||
// express implementation | ||
|
||
let server: Server; | ||
|
||
export function createServer(opts: ServerOptions) { | ||
const dev = opts.dev ?? true; | ||
const port = opts.port ?? 3000; | ||
const prefix = opts.prefix ?? "/trpc"; | ||
const app = express(); | ||
|
||
app.use( | ||
cors({ | ||
origin: "http://localhost:3000", | ||
credentials: true, | ||
}), | ||
); | ||
app.use(ironSession(sessionOptions)); | ||
app.use( | ||
"/trpc", | ||
trpcExpress.createExpressMiddleware({ | ||
router: appRouter, | ||
createContext, | ||
}), | ||
); | ||
|
||
app.get("/", async () => { | ||
return { hello: "wait-on 💨" }; | ||
}); | ||
|
||
const start = async () => { | ||
try { | ||
server = app.listen({ port }); | ||
console.log("listening on port", port); | ||
} catch (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
}; | ||
const stop = async () => { | ||
if (!server) { | ||
console.error("Server not running"); | ||
process.exit(1); | ||
} | ||
server.close(); | ||
}; | ||
return { server, start, stop }; | ||
} |
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,18 @@ | ||
{ | ||
"extends": "@grassroots/tsconfig/base.json", | ||
"compilerOptions": { | ||
"lib": ["es2022"], | ||
"baseUrl": ".", | ||
"paths": { | ||
"~/*": ["./src/*"] | ||
}, | ||
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json", | ||
"module": "esnext" | ||
}, | ||
"include": [ | ||
".", | ||
"../../packages/api/src/context.ts", | ||
"../../packages/auth/src/auth.ts" | ||
], | ||
"exclude": ["node_modules"] | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.