Skip to content

Commit 11adc9d

Browse files
committed
setup web server to handle audio requests in prod
1 parent 0af3b23 commit 11adc9d

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

src/api/index.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fastify, {
55
type FastifyRequest,
66
} from 'fastify'
77
import cors from '@fastify/cors'
8+
import FastifyStatic from '@fastify/static'
89
import { MikroORM } from '@mikro-orm/sqlite'
910
import fastifySSE from '@fastify/sse'
1011
import { Token } from '../lib/db/models/token.entity.js'
@@ -13,6 +14,8 @@ import { log } from '@lib/utils/logger.js'
1314
import { setupShutdownHandler } from '@lib/utils/utils.js'
1415
import type { SpotifyAccessToken } from '@lib/core/types.js'
1516
import redisClient from '@lib/utils/redis.js'
17+
import { fileURLToPath } from 'node:url'
18+
import path from 'node:path'
1619

1720
interface Client {
1821
id: number
@@ -51,6 +54,10 @@ const sendAudioUpdates = (data: string) => {
5154
})
5255
}
5356

57+
const __filename = fileURLToPath(import.meta.url)
58+
const __dirname = path.dirname(__filename)
59+
const web = path.join(__dirname, '..', '..', 'build', 'web')
60+
5461
export const routes = {
5562
async getTwitchToken(id: string, scopes: string) {
5663
const scopesArray = scopes ? scopes.split(',') : undefined
@@ -126,18 +133,33 @@ export const init = async (port: number) => {
126133
}
127134

128135
await server.register(cors, {
129-
origin: ['http://localhost:5000', 'http://localhost:5001'],
136+
origin: (origin, cb) => {
137+
if (!origin || /localhost:5000/.test(origin) || /localhost:5001/.test(origin) || /192.168.1.\d+/.test(origin)) {
138+
cb(null, true)
139+
return
140+
}
141+
142+
cb(new Error('Not allowed'), false)
143+
},
130144
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
131145
})
132146

147+
await server.register(FastifyStatic, {
148+
root: web
149+
})
150+
133151
await server.register(fastifySSE)
134152

135153
// GET /ping - test endpoint
136154
server.get('/ping', async (_request, _reply) => {
137155
return 'pong\n'
138156
})
139157

140-
// GET /api/token/{id} - for retrieveing twitch api tokens
158+
server.get('/', async (_request, reply) => {
159+
return reply.sendFile('index.html')
160+
})
161+
162+
// GET /api/token/{id} - for retrieving twitch api tokens
141163
server.get<{
142164
Params: RequestParams
143165
Querystring: TokenQueryString
@@ -252,6 +274,7 @@ export const init = async (port: number) => {
252274
reply.code(200).send({ message: 'spotify token updated successfully' })
253275
})
254276

277+
// GET /events - SSR listener for audio events
255278
server.get('/events', { sse: true }, async (_request, reply) => {
256279
reply.sse.keepAlive()
257280
await reply.sse.send({ data: 'Connected' })
@@ -270,6 +293,7 @@ export const init = async (port: number) => {
270293
})
271294

272295
if (process.env.NODE_ENV === 'development') {
296+
log.api.info('API server running in development mode')
273297
return server.listen({ port }, (err, address) => {
274298
if (err) {
275299
log.api.error(err)

src/web/react/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default function MyApp() {
3636
const [connectionState, setConnectionState] = useState('CONNECTING')
3737

3838
useEffect(() => {
39-
const es = new EventSource('http://localhost:4000/events')
39+
const es = new EventSource('/events')
4040

4141
es.onopen = () => {
4242
console.log('SSE Connection Opened')

vite.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export default defineConfig({
1313
port: 5000, // dev server will run on port 5000
1414
proxy: {
1515
// custom proxy rules for dev server, allows shorthand for fetch requests
16-
'/api': {
17-
target: 'http://localhost:4000',
16+
'/events': {
17+
target: 'http://localhost:4000/events',
1818
changeOrigin: true,
1919
},
2020
},

0 commit comments

Comments
 (0)