Skip to content

Commit

Permalink
Merge pull request #83 from duckRabbitPy/supabase-prod
Browse files Browse the repository at this point in the history
add route for prodding supabase
  • Loading branch information
duckRabbitPy authored Apr 23, 2024
2 parents 0e1befc + 8d3b962 commit 71d77a0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
6 changes: 5 additions & 1 deletion client/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ button {
padding: 0.2em;
}
}
button:hover {
button:not(:disabled):hover {
border-color: #646cff;
color: #747bff;
}

button:disabled:hover {
color: grey;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
Expand Down
3 changes: 3 additions & 0 deletions server/src/httpServer/createHttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { registerRouter } from "./routes/register/register";
import { authRouter } from "./routes/auth/auth";
import path from "path";
import { wsApplication } from "@wll8/express-ws/dist/src/type";
import { ping } from "./requestHandlers";
import { pingRouter } from "./routes/db-ping/ping";

export function createHttpServer(app: wsApplication) {
const isDev = process.env.NODE_ENV === "development";
Expand All @@ -26,6 +28,7 @@ export function createHttpServer(app: wsApplication) {
app.use("/api/login", loginRouter);
app.use("/api/register", registerRouter);
app.use("/api/auth", authRouter);
app.use("/api/ping", pingRouter);
app.use("/api/game-sessions", gameRouter);

// React app running on the same port as the server, in development use Vite server on port 5173
Expand Down
24 changes: 22 additions & 2 deletions server/src/httpServer/requestHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Request, RequestHandler } from "express";
import { pipe, Effect as E, Effect } from "effect";
import { getOpenGameSessionsQuery } from "../models/gamestate/queries";
import {
getHealthCheckSnapShotCount,
getOpenGameSessionsQuery,
} from "../models/gamestate/queries";
import path from "path";
import {
safeParseBoolean,
Expand Down Expand Up @@ -64,7 +67,6 @@ const checkHasValidToken = (req: Request, pool: Pool) => {
};

export const createGameSession: RequestHandler = (req, res) => {
safeParseNonEmptyString(req.headers.authorization);
const createGameSession = DBConnection.pipe(
E.flatMap((connection) => connection.pool),
E.flatMap((pool) => checkHasValidToken(req, pool)),
Expand Down Expand Up @@ -333,3 +335,21 @@ export const auth: RequestHandler = (req, res) => {

return E.runPromise(runnable);
};

export const ping: RequestHandler = (req, res) => {
const healthCheckKey = req.query.key;
if (healthCheckKey !== process.env.HEALTH_CHECK_KEY) {
return res.status(403).send("Incorrect health check key");
}
const getSnapShotCount = DBConnection.pipe(
E.flatMap((connection) => connection.pool),
E.flatMap((pool) => getHealthCheckSnapShotCount(pool)),
E.map((count) => {
res.status(200).send(`pong! Snapshot count: ${count}`);
})
);

const runnable = E.provide(getSnapShotCount, DBConnectionLive);

return E.runPromise(runnable);
};
12 changes: 12 additions & 0 deletions server/src/httpServer/routes/db-ping/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Router } from "express";
import { ping } from "../../requestHandlers";

const pingRouter = Router();

pingRouter.get("/", ping);

pingRouter.use((_, res) => {
res.status(406).json({ message: "Method Not Acceptable" });
});

export { pingRouter };
16 changes: 16 additions & 0 deletions server/src/models/gamestate/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,19 @@ export const getOpenGameSessionsQuery = (pool: Pool) => {
catch: () => new PostgresError({ message: "postgres query error" }),
}).pipe(E.retry({ times: 1 }));
};

export const getHealthCheckSnapShotCount = (pool: Pool) => {
const get = async () => {
try {
const result = await pool.query(`SELECT COUNT(*) FROM game_snapshots`);
return result.rows[0].count;
} catch (error) {
logAndThrowError(error);
}
};

return E.tryPromise({
try: () => get(),
catch: () => new PostgresError({ message: "postgres query error" }),
}).pipe(E.retry({ times: 1 }));
};

0 comments on commit 71d77a0

Please sign in to comment.