-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
137 lines (116 loc) · 4.76 KB
/
Copy pathserver.js
File metadata and controls
137 lines (116 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import 'dotenv/config';
import path from 'path';
import express from 'express';
import cookieParser from 'cookie-parser';
import { getQueueCounts } from './queues.js';
import { isConfigured as twitchConfigured, searchGames } from './twitch.js';
import { sampleFlairs } from './reddit.js';
import { addIgnore, discover as discoverGames, removeIgnore } from './gameFinder.js';
import { createQueuesRouter } from './bullBoard.js';
const LISTEN_PORT = 4000;
const INTERNAL_SERVER_ERROR = 500;
const SERVICE_UNAVAILABLE = 503;
const QUEUES_BASE_PATH = '/queues';
const app = express();
const API_TOKEN = process.env.API_TOKEN;
if ( !API_TOKEN ) {
throw new Error( 'Unable to load API token' );
}
app.use( cookieParser() );
app.use( express.static( path.join( import.meta.dirname, 'web' ) ) );
app.get( '/api-token', ( request, response ) => {
response.send( API_TOKEN );
} );
app.get( '/api/queues', async ( request, response ) => {
try {
response.json( await getQueueCounts() );
} catch ( queuesError ) {
console.error( queuesError );
response.status( INTERNAL_SERVER_ERROR ).json( {
error: 'Failed to read queues',
} );
}
} );
// Twitch game search for the box art picker. The browser can't call Twitch
// (CORS + the client secret), so the lookup happens here; see twitch.js. Reports
// 503 when no Twitch credentials are configured so the picker can fall back to
// manual entry.
app.get( '/api/twitch-games', async ( request, response ) => {
if ( !twitchConfigured() ) {
response.status( SERVICE_UNAVAILABLE ).json( {
error: 'Twitch not configured',
} );
return;
}
try {
response.json( {
results: await searchGames( request.query.q ),
} );
} catch ( twitchError ) {
console.error( twitchError );
response.status( INTERNAL_SERVER_ERROR ).json( {
error: 'Twitch lookup failed',
} );
}
} );
// Subreddit flair sampler for the Reddit flair editor (see reddit.js). The
// browser can't read reddit.com (CORS), so the scan happens here. No credentials
// needed — uses the public .json endpoints — so a failure is a real upstream
// error (rate limit, banned/empty subreddit), reported as 500.
app.get( '/api/reddit-flairs', async ( request, response ) => {
try {
response.json( await sampleFlairs( request.query.subreddit ) );
} catch ( redditError ) {
console.error( redditError );
response.status( INTERNAL_SERVER_ERROR ).json( {
error: 'Reddit flair scan failed',
} );
}
} );
// Game Finder discovery scan: untracked games trending on Twitch / topping
// Steam's Early Access sellers. Runs server-side (Twitch secret + Steam CORS),
// result cached ~10 min; ?force=1 (the Rescan button) bypasses the cache. An
// unconfigured Twitch just yields Steam-only results, so no 503.
app.get( '/api/game-finder', async ( request, response ) => {
try {
response.json( await discoverGames( {
force: request.query.force === '1',
} ) );
} catch ( finderError ) {
console.error( finderError );
response.status( INTERNAL_SERVER_ERROR ).json( {
error: 'Game finder scan failed',
} );
}
} );
// Add / remove a game from the persistent ignore list. The name is passed in
// the query string (not a JSON body) so the dev-server middleware can mirror
// this without body parsing. Returns the updated ignore list.
app.post( '/api/game-finder/ignore', ( request, response ) => {
response.json( {
ignored: addIgnore( request.query.name || '' ),
} );
} );
app.post( '/api/game-finder/unignore', ( request, response ) => {
response.json( {
ignored: removeIgnore( request.query.name || '' ),
} );
} );
// The full Bull Board UI, behind the same basic auth the rest of the admin sits
// behind. Only mounted when a REDIS_URL is configured (otherwise null).
const queuesRouter = createQueuesRouter( QUEUES_BASE_PATH );
if ( queuesRouter ) {
app.use( QUEUES_BASE_PATH, queuesRouter );
}
// SPA fallback: the client uses BrowserRouter (clean paths like /games), so a
// direct hit or refresh on a client route must return the built shell rather
// than 404. This runs only for requests not matched above — real assets
// (express.static), /api-token, /api/queues, and /queues are all registered
// earlier, so the router takes only what's left. (Express 5 requires the
// wildcard to be named, hence '*splat' rather than '*'.)
app.get( '*splat', ( request, response ) => {
response.sendFile( path.join( import.meta.dirname, 'web', 'index.html' ) );
} );
app.listen( process.env.PORT || LISTEN_PORT, '0.0.0.0', () => {
console.log( `Admin interface listening on port ${ process.env.PORT || LISTEN_PORT }!` );
} );