Skip to content

Commit

Permalink
Found some more
Browse files Browse the repository at this point in the history
  • Loading branch information
bhollis committed Jan 5, 2025
1 parent 32dfa57 commit 7bde8b8
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 65 deletions.
24 changes: 0 additions & 24 deletions api/apps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import * as Sentry from '@sentry/node';
import { ListToken } from '@stately-cloud/client';
import { keyBy } from 'es-toolkit';
import { RequestHandler } from 'express';
import { getAllApps as getAllAppsPostgres } from '../db/apps-queries.js';
import { pool } from '../db/index.js';
import { metrics } from '../metrics/index.js';
import { ApiApp } from '../shapes/app.js';
import { getAllApps, updateApps } from '../stately/apps-queries.js';
Expand Down Expand Up @@ -72,13 +70,6 @@ export async function refreshApps(): Promise<void> {
stopAppsRefresh();

try {
if (apps.length === 0) {
// Start off with a copy from postgres, just in case StatelyDB is having
// problems.
await fetchAppsFromPostgres();
digestApps();
}

if (!token) {
// First time, get 'em all
const [appsFromStately, newToken] = await getAllApps();
Expand Down Expand Up @@ -110,21 +101,6 @@ export async function refreshApps(): Promise<void> {
}
}

async function fetchAppsFromPostgres() {
const client = await pool.connect();
try {
apps = await getAllAppsPostgres(client);
appsByApiKey = keyBy(apps, (a) => a.dimApiKey.toLowerCase());
origins = new Set<string>();
for (const app of apps) {
origins.add(app.origin);
}
return apps;
} finally {
client.release();
}
}

function digestApps() {
appsByApiKey = keyBy(apps, (a) => a.dimApiKey.toLowerCase());
origins = new Set<string>();
Expand Down
2 changes: 0 additions & 2 deletions api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import http from 'http';
import morgan from 'morgan';
import vhost from 'vhost';
import { refreshApps, stopAppsRefresh } from './apps/index.js';
import { closeDbPool } from './db/index.js';
import { app as dimGgApp } from './dim-gg/server.js';
import { metrics } from './metrics/index.js';
import { app as dimApiApp } from './server.js';
Expand Down Expand Up @@ -137,7 +136,6 @@ createTerminus(server, {
onShutdown: async () => {
console.log('Shutting down');
stopAppsRefresh();
closeDbPool();
},
});

Expand Down
17 changes: 0 additions & 17 deletions api/routes/create-app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import asyncHandler from 'express-async-handler';
import { DatabaseError } from 'pg-protocol';
import { v4 as uuid } from 'uuid';
import { insertApp as insertAppPostgres } from '../db/apps-queries.js';
import { transaction } from '../db/index.js';
import { ApiApp, CreateAppRequest } from '../shapes/app.js';
import { insertApp } from '../stately/apps-queries.js';
import { badRequest } from '../utils.js';
Expand Down Expand Up @@ -52,20 +49,6 @@ export const createAppHandler = asyncHandler(async (req, res) => {
// Put it in StatelyDB
app = await insertApp(app);

// Also put it in Postgres, for now!
await transaction(async (client) => {
try {
await insertAppPostgres(client, app);
} catch (e) {
// This is a unique constraint violation, so just get the app!
if (e instanceof DatabaseError && e.code === '23505') {
await client.query('ROLLBACK');
} else {
throw e;
}
}
});

// Only return the recovered app if it's for the same origin and key
if (app.origin === originUrl.origin && app.bungieApiKey === request.bungieApiKey) {
res.send({
Expand Down
24 changes: 2 additions & 22 deletions api/routes/platform-info.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
import asyncHandler from 'express-async-handler';
import { pool } from '../db/index.js';
import { defaultGlobalSettings, GlobalSettings } from '../shapes/global-settings.js';
import { getGlobalSettings } from '../stately/global-settings.js';
import { camelize } from '../utils.js';

export const platformInfoHandler = asyncHandler(async (req, res) => {
const flavor = (req.query.flavor as string) ?? 'app';

let settings: GlobalSettings | undefined = undefined;
try {
// Try StatelyDB first, then fall back to Postgres
settings = await getGlobalSettings(flavor);
} catch (e) {
console.error('Error loading global settings from Stately:', flavor, e);
}

if (!settings) {
const result = await pool.query<GlobalSettings>({
name: 'get_global_settings',
text: 'SELECT * FROM global_settings where flavor = $1 LIMIT 1',
values: [flavor],
});
settings =
result.rowCount! > 0
? { ...defaultGlobalSettings, ...camelize(result.rows[0]) }
: defaultGlobalSettings;
}
// Try StatelyDB first, then fall back to Postgres
const settings = await getGlobalSettings(flavor);

// Instruct CF to cache for 15 minutes
res.set('Cache-Control', 'public, max-age=900');
Expand Down

0 comments on commit 7bde8b8

Please sign in to comment.