configure nginx to run in container

This commit is contained in:
Evan
2025-02-27 19:21:18 -08:00
parent a6ab4da694
commit 38365fd9d0
9 changed files with 139 additions and 132 deletions
+2 -2
View File
@@ -585,7 +585,7 @@ export class HostLobbyModal extends LitElement {
private async putGameConfig() {
const response = await fetch(
`${getServerConfig().workerPath(this.lobbyId)}/game/${this.lobbyId}`,
`${window.location.origin}/${getServerConfig().workerPath(this.lobbyId)}/game/${this.lobbyId}`,
{
method: "PUT",
headers: {
@@ -610,7 +610,7 @@ export class HostLobbyModal extends LitElement {
);
this.close();
const response = await fetch(
`${getServerConfig().workerPath(this.lobbyId)}/start_game/${this.lobbyId}`,
`${window.location.origin}/${getServerConfig().workerPath(this.lobbyId)}/start_game/${this.lobbyId}`,
{
method: "POST",
headers: {
+6 -6
View File
@@ -1,8 +1,8 @@
import { LitElement, html, css } from "lit";
import { customElement, property, state, query } from "lit/decorators.js";
import { GameMapType, GameType } from "../core/game/Game";
import { LitElement, css, html } from "lit";
import { customElement, query, state } from "lit/decorators.js";
import { getServerConfig } from "../core/configuration/Config";
import { consolex } from "../core/Consolex";
import { getConfig, getServerConfig } from "../core/configuration/Config";
import { GameMapType, GameType } from "../core/game/Game";
import { GameInfo } from "../core/Schemas";
@customElement("join-private-lobby-modal")
@@ -360,7 +360,7 @@ export class JoinPrivateLobbyModal extends LitElement {
consolex.log(`Joining lobby with ID: ${lobbyId}`);
this.message = "Checking lobby..."; // Set initial message
const url = `${window.location.origin}/${getServerConfig().workerPath(lobbyId)}/game/${lobbyId}/exists`;
const url = `/${getServerConfig().workerPath(lobbyId)}/game/${lobbyId}/exists`;
fetch(url, {
method: "GET",
headers: {
@@ -400,7 +400,7 @@ export class JoinPrivateLobbyModal extends LitElement {
if (!this.lobbyIdInput?.value) return;
fetch(
`${getServerConfig().workerPath(this.lobbyIdInput.value)}/lobby/${this.lobbyIdInput.value}`,
`/${getServerConfig().workerPath(this.lobbyIdInput.value)}/game/${this.lobbyIdInput.value}`,
{
method: "GET",
headers: {
+1 -1
View File
@@ -45,7 +45,7 @@ export class PublicLobby extends LitElement {
async fetchLobbies(): Promise<GameInfo[]> {
try {
const response = await fetch("/public_lobbies");
const response = await fetch(`/public_lobbies`);
if (!response.ok)
throw new Error(`HTTP error! status: ${response.status}`);
const data = await response.json();
+26 -12
View File
@@ -11,6 +11,7 @@ import rateLimit from "express-rate-limit";
import { fileURLToPath } from "url";
const config = getServerConfig();
const readyWorkers = new Set();
const app = express();
const server = http.createServer(app);
@@ -54,6 +55,31 @@ export async function startMaster() {
console.log(`Started worker ${i} (PID: ${worker.process.pid})`);
}
cluster.on("message", (worker, message) => {
if (message.type === "WORKER_READY") {
const workerId = message.workerId;
readyWorkers.add(workerId);
console.log(
`Worker ${workerId} is ready. (${readyWorkers.size}/${config.numWorkers()} ready)`,
);
// Start scheduling when all workers are ready
if (readyWorkers.size === config.numWorkers()) {
console.log("All workers ready, starting game scheduling");
// let the workers start up
const scheduleLobbies = () => {
schedulePublicGame().catch((error) => {
console.error("Error scheduling public game:", error);
});
};
scheduleLobbies();
setInterval(scheduleLobbies, config.gameCreationRate());
setInterval(() => fetchLobbies(), 250);
}
}
});
// Handle worker crashes
cluster.on("exit", (worker, code, signal) => {
const workerId = (worker as any).process?.env?.WORKER_ID;
@@ -81,18 +107,6 @@ export async function startMaster() {
server.listen(PORT, () => {
console.log(`Master HTTP server listening on port ${PORT}`);
});
sleep(5000).then(() => {
// let the workers start up
const scheduleLobbies = () => {
schedulePublicGame().catch((error) => {
console.error("Error scheduling public game:", error);
});
};
scheduleLobbies();
setInterval(scheduleLobbies, config.gameCreationRate());
setInterval(() => fetchLobbies(), 250);
});
}
// Add lobbies endpoint to list public games for this worker
+8
View File
@@ -308,6 +308,14 @@ export function startWorker() {
server.listen(PORT, () => {
console.log(`Worker ${workerId} running on http://localhost:${PORT}`);
console.log(`Handling requests with path prefix /w${workerId}/`);
// Signal to the master process that this worker is ready
if (process.send) {
process.send({
type: "WORKER_READY",
workerId: workerId,
});
console.log(`Worker ${workerId} signaled ready state to master`);
}
});
// Global error handler