-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use fastify instead of koa (#22)
- Loading branch information
Showing
15 changed files
with
669 additions
and
692 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { InstanceManager } from '@/objects/instanceManager/instanceManager'; | ||
|
||
declare module 'fastify' { | ||
interface FastifyRequest { | ||
instanceManager: InstanceManager; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import fastifyStatic from '@fastify/static'; | ||
import fastifyWebsocket from '@fastify/websocket'; | ||
import { fastify } from 'fastify'; | ||
import { readdir } from 'node:fs/promises'; | ||
import path from 'path'; | ||
|
||
import { buildV1Router } from '@/api/router/gosu/v1'; | ||
import { config } from '@/config'; | ||
import { OVERLAYS_STATIC } from '@/constants/overlaysStatic'; | ||
import { InstanceManager } from '@/objects/instanceManager/instanceManager'; | ||
|
||
export const buildFastifyApp = async (instanceManager: InstanceManager) => { | ||
const app = fastify({ | ||
logger: false | ||
}); | ||
|
||
// register websocket plugin | ||
await app.register(fastifyWebsocket); | ||
|
||
// apply instanceManager to request | ||
app.addHook('onRequest', async (req) => { | ||
req.instanceManager = instanceManager; | ||
}); | ||
// apply cors rules to request | ||
app.addHook('onRequest', async (req, reply) => { | ||
reply.header('Access-Control-Allow-Origin', '*'); | ||
reply.header( | ||
'Access-Control-Allow-Headers', | ||
'Origin, X-Requested-With, Content-Type, Accept' | ||
); | ||
reply.header( | ||
'Access-Control-Allow-Methods', | ||
'POST, GET, PUT, DELETE, OPTIONS' | ||
); | ||
}); | ||
|
||
// ---- REGISTERING STATIC ---- | ||
app.register(fastifyStatic, { | ||
root: path.join(process.cwd(), 'static'), | ||
prefix: '/' // optional: default '/' | ||
}); | ||
const staticReply = (_, reply) => { | ||
reply.header('content-type', 'html'); | ||
reply.send(OVERLAYS_STATIC); | ||
}; | ||
app.get('/', staticReply); | ||
app.get('/api/getOverlays', async (_, reply) => { | ||
reply.send(await readdir(config.staticFolderPath)); | ||
}); | ||
|
||
buildV1Router(app); | ||
|
||
return app; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// @ts-nocheck | ||
import send from '@fastify/send'; | ||
import { FastifyInstance } from 'fastify'; | ||
|
||
import { config } from '@/config'; | ||
import { wLogger } from '@/logger'; | ||
import { sleep } from '@/utils/sleep'; | ||
|
||
export const buildV1Router = (app: FastifyInstance) => { | ||
app.register(async (app) => { | ||
app.get('/ws', { websocket: true }, async (connection, req) => { | ||
wLogger.debug('>>> ws: CONNECTED'); | ||
let isSocketConnected = true; | ||
|
||
connection.socket.on('close', () => { | ||
isSocketConnected = false; | ||
wLogger.debug('>>> ws: CLOSED'); | ||
}); | ||
|
||
while (isSocketConnected) { | ||
if (Object.keys(req.instanceManager.osuInstances).length < 1) { | ||
await sleep(500); | ||
continue; | ||
} | ||
|
||
connection.socket.send( | ||
JSON.stringify( | ||
Object.values( | ||
req.instanceManager.osuInstances | ||
)[0].getState(req.instanceManager) | ||
) | ||
); | ||
await sleep(config.wsSendInterval); | ||
} | ||
}); | ||
}); | ||
|
||
app.get('/json', (req, reply) => { | ||
if (Object.keys(req.instanceManager.osuInstances).length < 1) { | ||
reply.code(500); | ||
reply.send(null); | ||
return; | ||
} | ||
|
||
reply.send( | ||
Object.values(req.instanceManager.osuInstances)[0].getState( | ||
req.instanceManager | ||
) | ||
); | ||
}); | ||
|
||
app.get('/Songs/*', async (req, reply) => { | ||
if (Object.keys(req.instanceManager.osuInstances).length < 1) { | ||
reply.code(500); | ||
reply.send(null); | ||
return; | ||
} | ||
|
||
const { settings } = Object.values( | ||
req.instanceManager.osuInstances | ||
)[0].entities.getServices(['settings']); | ||
|
||
if (settings.songsFolder === '') { | ||
reply.code(404); | ||
reply.send({ | ||
error: 'not_ready' | ||
}); | ||
return; | ||
} | ||
|
||
const parsedURL = new URL( | ||
`${req.protocol}://${req.hostname}${req.url}` | ||
); | ||
const mapPath = parsedURL.pathname.replace('/Songs', ''); | ||
|
||
reply.hijack(); | ||
send(req, mapPath, { root: settings.songsFolder }).pipe(reply.raw); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.