mirror of
https://github.com/captbaritone/webamp.git
synced 2026-01-23 02:15:01 +00:00
Get rid of custom server for next.js
This commit is contained in:
parent
b64a007d0f
commit
71ca6d9230
6 changed files with 25 additions and 51 deletions
|
|
@ -49,7 +49,6 @@ declare global {
|
|||
notify(action: ApiAction): void;
|
||||
log(message: string): void;
|
||||
logError(message: string): void;
|
||||
startTime: number;
|
||||
session: {
|
||||
username: string | undefined;
|
||||
};
|
||||
|
|
@ -69,11 +68,6 @@ export function createApp({ eventHandler, logger, extraMiddleware }: Options) {
|
|||
app.use(Sentry.Handlers.requestHandler() as RequestHandler);
|
||||
}
|
||||
|
||||
app.use(function (req, res, next) {
|
||||
req.startTime = Date.now();
|
||||
next();
|
||||
});
|
||||
|
||||
// https://expressjs.com/en/guide/behind-proxies.html
|
||||
// This is needed in order to allow `cookieSession({secure: true})` cookies to be sent.
|
||||
app.set("trust proxy", "loopback");
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
import { createYoga } from "graphql-yoga";
|
||||
import { createYoga, YogaInitialContext } from "graphql-yoga";
|
||||
import { getSchema } from "../../api/graphql/schema";
|
||||
import UserContext from "../../data/UserContext";
|
||||
import DiscordEventHandler from "../../api/DiscordEventHandler";
|
||||
|
||||
const handler = new DiscordEventHandler();
|
||||
|
||||
interface NextContext {
|
||||
params: Promise<Record<string, string>>;
|
||||
|
|
@ -21,6 +25,23 @@ const { handleRequest } = createYoga<NextContext>({
|
|||
methods: ["GET", "POST", "OPTIONS"],
|
||||
credentials: true,
|
||||
},
|
||||
context: (ctx: YogaInitialContext) => {
|
||||
return {
|
||||
...ctx.request,
|
||||
ctx: new UserContext(),
|
||||
notify: (action) => handler.handle(action),
|
||||
log(message: string) {
|
||||
console.log(message, {
|
||||
url: ctx.request.url,
|
||||
});
|
||||
},
|
||||
error(message: string) {
|
||||
console.error(message, {
|
||||
url: ctx.request.url,
|
||||
});
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export {
|
||||
|
|
|
|||
|
|
@ -26,10 +26,6 @@ const configs = {
|
|||
},
|
||||
development: {
|
||||
...production,
|
||||
connection: {
|
||||
...production.connection,
|
||||
filename: path.join(__dirname, "./skins-dev.sqlite3"),
|
||||
},
|
||||
},
|
||||
production,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
/** @type {import('next').NextConfig} */
|
||||
const nextConfig = {
|
||||
serverExternalPackages: ["knex", "imagemin-optipng"],
|
||||
serverExternalPackages: ["knex", "imagemin-optipng", "discord.js"],
|
||||
};
|
||||
|
||||
module.exports = nextConfig;
|
||||
|
|
|
|||
|
|
@ -55,9 +55,9 @@
|
|||
"sync": "ts-node --transpile-only ./tasks/syncWithArchive.ts",
|
||||
"migrate": "knex migrate:latest",
|
||||
"grats": "grats",
|
||||
"dev:next": "ts-node --transpile-only server.ts",
|
||||
"dev:next": "next dev",
|
||||
"build:next": "next build",
|
||||
"start:next": "NODE_ENV=production ts-node --transpile-only server.ts",
|
||||
"start:next": "next start",
|
||||
"lint:next": "next lint"
|
||||
},
|
||||
"prettier": {},
|
||||
|
|
|
|||
|
|
@ -1,37 +0,0 @@
|
|||
import dotenv from "dotenv";
|
||||
|
||||
dotenv.config();
|
||||
import { parse } from "url";
|
||||
import next from "next";
|
||||
import { createApp } from "./api/app";
|
||||
import DiscordEventHandler from "./api/DiscordEventHandler";
|
||||
|
||||
const handler = new DiscordEventHandler();
|
||||
|
||||
const port = parseInt(process.env.PORT || "3000", 10);
|
||||
const dev = process.env.NODE_ENV !== "production";
|
||||
const app = next({ dev });
|
||||
const handle = app.getRequestHandler();
|
||||
|
||||
app.prepare().then(() => {
|
||||
const expressApp = createApp({
|
||||
eventHandler: (action) => handler.handle(action),
|
||||
logger: {
|
||||
log: (message, context) => console.log(message, context),
|
||||
logError: (message, context) => console.error(message, context),
|
||||
},
|
||||
});
|
||||
|
||||
// If a route does not match in express, fallback to Next.js routes
|
||||
expressApp.all("*", (req, res) => {
|
||||
const parsedUrl = parse(req.url, true);
|
||||
return handle(req, res, parsedUrl);
|
||||
});
|
||||
|
||||
expressApp.listen(port);
|
||||
console.log(
|
||||
`> Server listening at http://localhost:${port} as ${
|
||||
dev ? "development" : process.env.NODE_ENV
|
||||
}`
|
||||
);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue