Skip to content

Commit

Permalink
feat: new catalogs data format
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed May 13, 2024
1 parent 39107f9 commit d4a9c2f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 33 deletions.
32 changes: 31 additions & 1 deletion api/src/catalog/catalog.routes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
import type express from 'express';
import express from 'express';
import asyncHandler from 'express-async-handler';
import { verifyHeaders, refreshCatalog } from './catalog.handlers.js';
import { authWithEvals } from '../auth/auth.handlers.js';
import { STATIC_FILE_DIR } from '../config.js';

const staticJSON = (path: string) =>
express.static(`${STATIC_FILE_DIR}${path}`, {
cacheControl: true,
maxAge: '1h',
lastModified: true,
etag: true,
extensions: ['json'],
});

export default (app: express.Express): void => {
// Enable static catalog refresh on demand.
// After the crawler runs, we hit this route to refresh the static files.
app.get('/api/catalog/refresh', verifyHeaders, asyncHandler(refreshCatalog));

// Evals data require NetID authentication
app.use(
'/api/catalog/evals',
authWithEvals,
staticJSON('/catalogs-v2/evals'),
);

// Serve public catalog files without authentication
app.use('/api/catalog/public', staticJSON('/catalogs-v2/public'));

// Legacy data formats
// TODO: remove
app.use(
'/api/static/catalogs/evals',
authWithEvals,
staticJSON('/catalogs/evals'),
);
app.use('/api/static/catalogs/public', staticJSON('/catalogs/public'));
};
6 changes: 6 additions & 0 deletions api/src/catalog/catalog.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ async function fetchData(
type: 'evals' | 'public',
overwrite: boolean,
) {
// Support two versions of the frontend.
// TODO: remove the legacy format and rename catalogs-v2 to catalogs
const filePath = `${STATIC_FILE_DIR}/catalogs/${type}/${seasonCode}.json`;
const v2FilePath = `${STATIC_FILE_DIR}/catalogs-v2/${type}/${seasonCode}.json`;
await fs.mkdir(path.dirname(filePath), { recursive: true });
if (
!overwrite &&
Expand All @@ -106,6 +109,7 @@ async function fetchData(
filePath,
JSON.stringify(data.listings.map(postprocess)),
);
await fs.writeFile(v2FilePath, JSON.stringify(data.listings));
winston.info(
`Fetched ${type} data for ${seasonCode}; n=${data.listings.length}`,
);
Expand All @@ -126,6 +130,8 @@ export async function fetchCatalog(overwrite: boolean) {
}

winston.info(`Fetched ${seasons.length} seasons`);

// The seasons.json and infoAttributes.json files are copied to frontend
await fs.writeFile(
`${STATIC_FILE_DIR}/seasons.json`,
JSON.stringify(seasons.sort((a, b) => Number(b) - Number(a))),
Expand Down
26 changes: 1 addition & 25 deletions api/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createProxyMiddleware } from 'http-proxy-middleware';
import passport from 'passport';
import { createClient } from 'redis';

import { authWithEvals, passportConfig } from './auth/auth.handlers.js';
import { passportConfig } from './auth/auth.handlers.js';
import casAuth from './auth/auth.routes.js';
import canny from './canny/canny.routes.js';
import catalog from './catalog/catalog.routes.js';
Expand All @@ -20,7 +20,6 @@ import {
INSECURE_PORT,
SESSION_SECRET,
CORS_OPTIONS,
STATIC_FILE_DIR,
REDIS_HOST,
isDev,
SENTRY_DSN,
Expand Down Expand Up @@ -157,29 +156,6 @@ canny(app);
user(app);
linkPreview(app);

// Evals data require NetID authentication
app.use(
'/api/static/catalogs/evals',
authWithEvals,
express.static(`${STATIC_FILE_DIR}/catalogs/evals`, {
cacheControl: true,
maxAge: '1h',
lastModified: true,
etag: true,
}),
);

// Serve public catalog files without authentication
app.use(
'/api/static',
express.static(STATIC_FILE_DIR, {
cacheControl: true,
maxAge: '1h',
lastModified: true,
etag: true,
}),
);

app.get('/api/ping', (req, res) => {
res.json('pong');
});
Expand Down
Empty file.
Empty file.
19 changes: 12 additions & 7 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Endpoints marked as "needs eval access" additionally returns 401 with `error: "U
- Body:
- `error`: `"NOT_FERRY"`

### `GET` `/api/static/catalogs/evals/{season}.json`
### `GET` `/api/catalog/evals/{season}`

#### Request

Expand All @@ -112,19 +112,24 @@ Endpoints marked as "needs eval access" additionally returns 401 with `error: "U
**Status: 200**

- Body:
- `ListingRatings[]` (see `static` folder for examples)

### `GET` `/api/static/catalogs/public/{season}.json`
- See the `evalsBySeason` query

TODO: rename this to `/api/catalog` and remove `.json`?
### `GET` `/api/catalog/public/{season}`

#### Response

**Status: 200**

- Body:
- `Listing[]` (see `static` folder for examples)
- Types are available via GraphQL
- See the `catalogBySeason` query

### `GET` `/api/static/catalogs/evals/{season}.json`

DEPRECATED: use `/api/catalog/evals/{season}` instead

### `GET` `/api/static/catalogs/public/{season}.json`

DEPRECATED: use `/api/catalog/public/{season}` instead

## Auth

Expand Down

0 comments on commit d4a9c2f

Please sign in to comment.