Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ DATABASE_URL=postgres://localhost:5432/monmap
# Optional: the webapp's connection pool size (default 10). Keep it under
# Postgres max_connections.
DB_POOL_MAX=
# Optional: TLS to Postgres (require, verify-full or disable). Unset,
# every host except localhost must serve TLS (@monashcoding/db). Set
# `disable` when the database is on a private Docker network.
DATABASE_SSL=

# ---- Public site origin ----
# Canonical URLs, sitemap, robots.txt and OG image URLs resolve from this
Expand All @@ -17,11 +21,11 @@ NEXT_PUBLIC_SITE_URL=https://monmap.monashcoding.com
# ---- Central MAC auth (auth.monashcoding.com) ----
# MonMap consumes the central MAC identity service; it does NOT run its
# own auth server or hold OAuth secrets. Server code verifies EdDSA JWTs
# locally against the service's JWKS (packages/webapp/lib/mac-auth.ts).
# locally against the service's JWKS (@monashcoding/auth).
#
# AUTH_URL — read server-side (JWT issuer + JWKS endpoint).
# NEXT_PUBLIC_AUTH_URL — same value, exposed to the browser so the Better
# Auth react client points sign-in/out at the service.
# NEXT_PUBLIC_AUTH_URL — same value, exposed to the browser so the auth
# client points sign-in/out at the service.
# JWT_AUDIENCE — expected `aud` claim on MAC tokens.
AUTH_URL=https://auth.monashcoding.com
NEXT_PUBLIC_AUTH_URL=https://auth.monashcoding.com
Expand All @@ -30,8 +34,8 @@ JWT_AUDIENCE=mac-suite
# ---- PostHog ----
# Public project token (safe to ship to the browser — PostHog API tokens
# are write-only for capture). Used by both
# packages/webapp/lib/analytics.ts (the one posthog.init) and
# packages/webapp/lib/posthog-server.ts; same value either side. Both
# packages/webapp/lib/analytics.ts (the browser) and
# packages/webapp/lib/analytics-server.ts; same value either side. Both
# send nothing outside production.
NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN=
# Server-side host. The browser reaches PostHog through the /ingest
Expand Down
4 changes: 2 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ than gray.

- Client code sends PostHog events only through `lib/analytics.ts`. An
eslint rule blocks direct `posthog-js` imports.
- Server code sends events only through `captureServer` in
`lib/posthog-server.ts`.
- Server code sends events only through `serverAnalytics` in
`lib/analytics-server.ts`.
- No event, property or label may carry an email, a name, a plan name
or a grade.
- Page views for the `/search` sorts are counted in Postgres, not PostHog.
Expand Down
4 changes: 3 additions & 1 deletion packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
},
"scripts": {
"generate": "drizzle-kit generate",
"migrate": "drizzle-kit migrate",
"migrate": "node src/migrate.ts",
"studio": "drizzle-kit studio",
"typecheck": "tsc --noEmit",
"test": "node --test 'src/*.test.ts'"
},
"dependencies": {
"@monashcoding/db": "0.2.0",
"@monmap/scraper": "workspace:*",
"dotenv": "^18.0.6",
"drizzle-orm": "0.45.4",
"postgres": "^3.4.9"
},
"devDependencies": {
"@monashcoding/tsconfig": "0.2.0",
"drizzle-kit": "^0.31.11"
}
}
13 changes: 13 additions & 0 deletions packages/db/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,16 @@ export const DATABASE_URL = (() => {
if (!url) throw new Error("DATABASE_URL is not set in root .env");
return url;
})();

/**
* TLS to Postgres: `require`, `verify-full` or `disable`. Unset,
* `@monashcoding/db` requires TLS for every host except localhost.
*/
export const DATABASE_SSL = (() => {
const mode = process.env.DATABASE_SSL;
if (!mode) return undefined;
if (mode === "require" || mode === "verify-full" || mode === "disable") {
return mode;
}
throw new Error("DATABASE_SSL must be require, verify-full or disable");
})();
45 changes: 23 additions & 22 deletions packages/db/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { drizzle } from "drizzle-orm/postgres-js";
import postgres, { type Options } from "postgres";
import { createDb as createMacDb, type SslMode } from "@monashcoding/db";
import * as schema from "./schema.ts";

export * from "./schema.ts";
Expand All @@ -10,38 +10,39 @@ export * from "./overrides.ts";
export type Database = ReturnType<typeof createDb>;

export interface CreateDbOptions {
/** Override postgres-js pool options. */
pool?: Options<Record<string, never>>;
/** Pool size. Defaults to `DB_POOL_MAX`, then 10. */
max?: number;
/**
* TLS to Postgres. Unset, `@monashcoding/db` requires TLS for every
* host except localhost; pass `disable` for a database on a private
* network that does not serve TLS.
*/
ssl?: SslMode;
}

/**
* Build a Drizzle client bound to a Postgres connection string.
*
* We pass `casing: "snake_case"` to match drizzle.config.ts — schema
* fields are camel-cased in TS, snake-cased in the DB.
* The connection comes from `@monashcoding/db`: it names itself
* `monmap` on the server, picks the TLS mode and recycles idle
* sockets. Statements are not prepared: Drizzle runs every query
* through postgres.js `unsafe()`, which never prepares.
*
* Pool defaults suit a long-lived process that talks to Postgres
* directly (the self-hosted webapp and the ingest CLI): up to 10
* connections, idle sockets kept for 5 minutes and each connection
* replaced after an hour. Pass `{ pool }` to tune.
*
* Statements are not prepared. Drizzle runs every query through
* postgres.js `unsafe()`, which never prepares whatever the pool says,
* so `prepare: true` here would have no effect. Do not force it on:
* the variable-length IN and VALUES lists would fill postgres.js's
* per-connection statement cache, which it never evicts.
* The Drizzle client is built here, not taken from the package,
* because the schema needs `casing: "snake_case"` (fields are
* camel-cased in TS, snake-cased in the DB, matching
* drizzle.config.ts) and the package's client has no casing option.
*/
export function createDb(
url: string,
options: CreateDbOptions = {},
): ReturnType<typeof drizzle<typeof schema>> {
const sql = postgres(url, {
max: 10,
prepare: false,
idle_timeout: 300,
max_lifetime: 60 * 60,
connect_timeout: 10,
...options.pool,
const { sql } = createMacDb({
app: "monmap",
schema,
url,
...(options.max === undefined ? {} : { max: options.max }),
...(options.ssl === undefined ? {} : { ssl: options.ssl }),
});
return drizzle(sql, { schema, casing: "snake_case" });
}
15 changes: 15 additions & 0 deletions packages/db/src/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { runMigrations } from "@monashcoding/db";
import { DATABASE_SSL, DATABASE_URL } from "./env.ts";

// Apply the pending migrations in ../drizzle to DATABASE_URL, on one
// connection. Same ledger (drizzle.__drizzle_migrations) as
// `drizzle-kit migrate`, which this replaces.
await runMigrations({
app: "monmap",
migrationsFolder: resolve(fileURLToPath(import.meta.url), "../../drizzle"),
url: DATABASE_URL,
...(DATABASE_SSL === undefined ? {} : { ssl: DATABASE_SSL }),
});
console.log("Migrations applied.");
5 changes: 4 additions & 1 deletion packages/db/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"extends": "@monashcoding/tsconfig/node.json",
"compilerOptions": {
"allowImportingTsExtensions": true
},
"include": ["src/**/*", "drizzle.config.ts"]
}
1 change: 1 addition & 0 deletions packages/ingest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"drizzle-orm": "0.45.4"
},
"devDependencies": {
"@monashcoding/tsconfig": "0.2.0",
"tsx": "^4.23.15"
}
}
5 changes: 3 additions & 2 deletions packages/ingest/src/apply-overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
extractRequirementGroups,
units,
} from "@monmap/db";
import { DATABASE_URL } from "@monmap/db/env";
import { DATABASE_SSL, DATABASE_URL } from "@monmap/db/env";
import { loadCurriculumOverrides } from "./overrides.ts";
import { foldPlaceholderUnits } from "./parse.ts";

Expand All @@ -28,7 +28,8 @@ console.log(
);

const db = createDb(DATABASE_URL, {
pool: { max: 2, idle_timeout: 0, prepare: false },
max: 2,
ssl: DATABASE_SSL,
});

const rows = await db
Expand Down
5 changes: 3 additions & 2 deletions packages/ingest/src/backfill-curriculum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
units,
unitYearLinks,
} from "@monmap/db";
import { DATABASE_URL } from "@monmap/db/env";
import { DATABASE_SSL, DATABASE_URL } from "@monmap/db/env";
import {
extractCourseAosRefs,
extractUnitYearLinks,
Expand All @@ -51,7 +51,8 @@ if (yearFlag !== -1 && !/^\d{4}$/.test(onlyYear ?? ""))
const overrides = loadCurriculumOverrides();

const db = createDb(DATABASE_URL, {
pool: { max: 2, idle_timeout: 0, prepare: false },
max: 2,
ssl: DATABASE_SSL,
});

const rows = await db
Expand Down
5 changes: 3 additions & 2 deletions packages/ingest/src/eval-curriculum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
units,
type RequirementGroup,
} from "@monmap/db";
import { DATABASE_URL } from "@monmap/db/env";
import { DATABASE_SSL, DATABASE_URL } from "@monmap/db/env";
import { loadCurriculumOverrides } from "./overrides.ts";

const outFlag = process.argv.indexOf("--out");
Expand All @@ -52,7 +52,8 @@ const onlyYear = yearFlag !== -1 ? process.argv[yearFlag + 1] : undefined;
const overrides = loadCurriculumOverrides();

const db = createDb(DATABASE_URL, {
pool: { max: 2, idle_timeout: 0, prepare: false },
max: 2,
ssl: DATABASE_SSL,
});

const legacyAuto = (groups: readonly RequirementGroup[]): Set<string> => {
Expand Down
10 changes: 5 additions & 5 deletions packages/ingest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readdir } from "node:fs/promises";
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { createDb } from "@monmap/db";
import { DATABASE_URL } from "@monmap/db/env";
import { DATABASE_SSL, DATABASE_URL } from "@monmap/db/env";
import { ingest } from "./ingest.ts";

function parseArgs(argv: readonly string[]): { year: string; allYears: boolean; dataDir: string } {
Expand All @@ -24,11 +24,11 @@ function parseArgs(argv: readonly string[]): { year: string; allYears: boolean;
}

const args = parseArgs(process.argv.slice(2));
// Ingest is a long-running CLI doing bulk inserts — opt out of the
// serverless-tuned defaults from `createDb` and use a larger pool with
// no idle reaping.
// Ingest is a long-running CLI doing bulk inserts, on a small pool of
// its own.
const db = createDb(DATABASE_URL, {
pool: { max: 4, idle_timeout: 0, prepare: false },
max: 4,
ssl: DATABASE_SSL,
});

const years = args.allYears
Expand Down
5 changes: 4 additions & 1 deletion packages/ingest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"extends": "@monashcoding/tsconfig/node.json",
"compilerOptions": {
"allowImportingTsExtensions": true
},
"include": ["src/**/*"]
}
1 change: 1 addition & 0 deletions packages/scraper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@monashcoding/tsconfig": "0.2.0",
"tsx": "^4.23.15"
}
}
5 changes: 4 additions & 1 deletion packages/scraper/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"extends": "@monashcoding/tsconfig/node.json",
"compilerOptions": {
"allowImportingTsExtensions": true
},
"include": ["src/**/*"]
}
2 changes: 1 addition & 1 deletion packages/webapp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ reviews. It is a Next.js app in the monorepo.
- Analytics: client code sends PostHog events only through
`lib/analytics.ts` (an eslint rule enforces it), which loads
posthog-js on demand. Server code sends them only through
`captureServer` in `lib/posthog-server.ts`. Neither sends anything
`serverAnalytics.capture` in `lib/analytics-server.ts`. Neither sends anything
outside production, and no event carries an email, a name, a plan
name or a grade.
- Page views for the /search sorts are counted in Postgres, not
Expand Down
8 changes: 5 additions & 3 deletions packages/webapp/app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"use server"

import { getClaims, getCurrentUser } from "@/lib/auth-server"
import { captureServer } from "@/lib/posthog-server"
import { getClaims } from "@monashcoding/auth/next"

import { getCurrentUser } from "@/lib/auth-server"
import { serverAnalytics } from "@/lib/analytics-server"
import {
cleanGradeCode,
cleanMark,
Expand Down Expand Up @@ -132,7 +134,7 @@ export async function createBlankPlanAction(
cleanPlanName(formData?.get("name")?.toString()) ?? "Default plan"
const state = defaultState(year, null, 3)
const plan = await createUserPlan(u.id, name, state)
await captureServer(u.id, "plan_created_server", { handbook_year: year })
await serverAnalytics.capture(u.id, "plan_created_server", { handbook_year: year })
redirect(`/?plan=${plan.id}`)
}

Expand Down
21 changes: 21 additions & 0 deletions packages/webapp/app/api/health/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createHealthHandler } from "@monashcoding/server"
import { sql } from "drizzle-orm"

import { getDb } from "@/lib/db/client"

/**
* 200 when the database answers, 503 when it does not, for Dokploy and
* uptime monitors.
*
* GET /api/health
*/
export const dynamic = "force-dynamic"

export const GET = createHealthHandler({
service: "monmap",
checks: {
db: async () => {
await getDb().execute(sql`select 1`)
},
},
})
7 changes: 5 additions & 2 deletions packages/webapp/app/api/plan-graph/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { handleRoute } from "@monashcoding/server"

import type { PlanGraphData } from "@/lib/api/client"
import { MAX_GRAPH_CODES, splitCodes } from "@/lib/api/query"
import {
Expand All @@ -6,6 +8,7 @@ import {
json,
knownYear,
searchParams,
ROUTE_OPTIONS,
} from "@/lib/api/server"
import { expandRequisiteGraph, hydratePlannerUnits } from "@/lib/db/queries"

Expand All @@ -19,7 +22,7 @@ import { expandRequisiteGraph, hydratePlannerUnits } from "@/lib/db/queries"
*/
export const dynamic = "force-dynamic"

export async function GET(req: Request): Promise<Response> {
export const GET = handleRoute<unknown>(async function GET(req: Request) {
const params = searchParams(req)
const year = await knownYear(params.get("year"))
const codes = splitCodes(params.get("codes"), MAX_GRAPH_CODES)
Expand All @@ -36,4 +39,4 @@ export async function GET(req: Request): Promise<Response> {
units: Object.fromEntries(hydrated.units),
}
return json(body, HANDBOOK_CACHE)
}
}, ROUTE_OPTIONS)
Loading
Loading