Skip to content

Commit

Permalink
Updating prod server
Browse files Browse the repository at this point in the history
  • Loading branch information
ncpleslie committed Jan 24, 2024
1 parent 349476a commit 18e6b5b
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/server/wss-prod-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,49 @@ import { applyWSSHandler } from "@trpc/server/adapters/ws";
import http from "http";
import next from "next";
import { parse } from "url";
import ws from "ws";
import { WebSocketServer } from "ws";
import type { Socket } from "net";
import { env } from "../env/server";
import { createContext } from "./trpc/context";
import { appRouter } from "./trpc/router/_app";

const app = next({ dev: env.NODE_ENV !== "production" });
const handle = app.getRequestHandler();

const server = http.createServer((req, res) => {
const proto = req.headers["x-forwarded-proto"];
if (proto && proto === "http") {
res.writeHead(303, {
location: `https://` + req.headers.host + (req.headers.url ?? ""),
});
res.end();
app.prepare().then(() => {
const server = http.createServer((req, res) => {
// const proto = req.headers["x-forwarded-proto"];
// if (proto && proto === "http") {
// res.writeHead(303, {
// location: `https://` + req.headers.host + (req.headers.url ?? ""),
// });
// res.end();

return;
}
// return;
// }

if (!req.url) {
return;
}
if (!req.url) {
return;
}

const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
});
const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
});

app.prepare().then(() => {
const wss = new ws.Server({ server });
const wss = new WebSocketServer({ server });
const handler = applyWSSHandler({ wss, router: appRouter, createContext });

process.on("SIGTERM", () => {
console.log("SIGTERM");
handler.broadcastReconnectNotification();
});

server.on("upgrade", (req, socket, head) => {
wss.handleUpgrade(req, socket as Socket, head, (ws) => {
wss.emit("connection", ws, req);
});
});

server.listen(env.PORT, "0.0.0.0", () => {
const address = server.address();
console.log(`Next address: ${JSON.stringify(address)}`);
Expand Down

0 comments on commit 18e6b5b

Please sign in to comment.