@@ -5,6 +5,7 @@ import fastify, {
55 type FastifyRequest ,
66} from 'fastify'
77import cors from '@fastify/cors'
8+ import FastifyStatic from '@fastify/static'
89import { MikroORM } from '@mikro-orm/sqlite'
910import fastifySSE from '@fastify/sse'
1011import { Token } from '../lib/db/models/token.entity.js'
@@ -13,6 +14,8 @@ import { log } from '@lib/utils/logger.js'
1314import { setupShutdownHandler } from '@lib/utils/utils.js'
1415import type { SpotifyAccessToken } from '@lib/core/types.js'
1516import redisClient from '@lib/utils/redis.js'
17+ import { fileURLToPath } from 'node:url'
18+ import path from 'node:path'
1619
1720interface 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+
5461export 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 || / l o c a l h o s t : 5 0 0 0 / . test ( origin ) || / l o c a l h o s t : 5 0 0 1 / . test ( origin ) || / 1 9 2 .1 6 8 .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 )
0 commit comments