-
-
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.
api v2
- Loading branch information
Showing
38 changed files
with
2,306 additions
and
452 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,80 @@ | ||
import { config } from '@tosu/common'; | ||
|
||
import buildBaseApi from './router/index'; | ||
import buildV1Api from './router/v1'; | ||
import buildV2Api from './router/v2'; | ||
import { HttpServer } from './utils/http'; | ||
import { Websocket } from './utils/socket'; | ||
|
||
export class Server { | ||
instanceManager: any; | ||
app = new HttpServer(); | ||
|
||
constructor({ instanceManager }: { instanceManager: any }) { | ||
this.instanceManager = instanceManager; | ||
|
||
this.middlrewares(); | ||
} | ||
|
||
start() { | ||
const WS_V1 = new Websocket({ | ||
instanceManager: this.instanceManager, | ||
pollRateFieldName: 'pollRate', | ||
stateFunctionName: 'getState' | ||
}); | ||
const WS_V2 = new Websocket({ | ||
instanceManager: this.instanceManager, | ||
pollRateFieldName: 'pollRate', | ||
stateFunctionName: 'getStateV2' | ||
}); | ||
const WS_V2_KEYS = new Websocket({ | ||
instanceManager: this.instanceManager, | ||
pollRateFieldName: 'keyOverlayPollRate', | ||
stateFunctionName: 'getKeyOverlay' | ||
}); | ||
|
||
buildBaseApi(this.app); | ||
buildV1Api({ | ||
app: this.app, | ||
websocket: WS_V1 | ||
}); | ||
buildV2Api({ | ||
app: this.app, | ||
websocket: WS_V2, | ||
keysWebsocket: WS_V2_KEYS | ||
}); | ||
|
||
this.app.listen(config.serverPort, config.serverIP); | ||
} | ||
|
||
restart() { | ||
this.app.server.close(); | ||
this.app.listen(config.serverPort, config.serverIP); | ||
} | ||
|
||
middlrewares() { | ||
const that = this; | ||
|
||
this.app.use((_, res, next) => { | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
res.setHeader( | ||
'Access-Control-Allow-Headers', | ||
'Origin, X-Requested-With, Content-Type, Accept' | ||
); | ||
res.setHeader( | ||
'Access-Control-Allow-Methods', | ||
'POST, GET, PUT, DELETE, OPTIONS' | ||
); | ||
next(); | ||
}); | ||
|
||
this.app.use((req, _, next) => { | ||
req.instanceManager = that.instanceManager; | ||
next(); | ||
}); | ||
} | ||
} | ||
|
||
export * from './utils/http'; | ||
export * from './utils/socket'; | ||
export * from './utils/index'; | ||
export * from './http/index'; | ||
export * from './socket/index'; |
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,62 @@ | ||
import { config } from '@tosu/common'; | ||
import path from 'path'; | ||
|
||
import { HttpServer, getContentType, sendJson } from '../index'; | ||
import { directoryWalker, readDirectory } from '../utils/directories'; | ||
|
||
export default function buildBaseApi(app: HttpServer) { | ||
app.route('/json', 'GET', (req, res) => { | ||
const osuInstances: any = Object.values( | ||
req.instanceManager.osuInstances || {} | ||
); | ||
if (osuInstances.length < 1) { | ||
res.statusCode = 500; | ||
return sendJson(res, { error: 'not_ready' }); | ||
} | ||
|
||
const json = osuInstances[0].getState(req.instanceManager); | ||
sendJson(res, json); | ||
}); | ||
|
||
app.route('/api/overlays', 'GET', (req, res) => { | ||
const staticPath = | ||
config.staticFolderPath || | ||
path.join(path.dirname(process.execPath), 'static'); | ||
|
||
readDirectory(staticPath, '/', (html: string) => { | ||
res.writeHead(200, { 'Content-Type': getContentType('file.html') }); | ||
res.end(html); | ||
}); | ||
}); | ||
|
||
app.route(/.*/, 'GET', (req, res) => { | ||
const url = req.url || '/'; | ||
const folderPath = | ||
config.staticFolderPath || | ||
path.join(path.dirname(process.execPath), 'static'); | ||
|
||
if (url == '/') { | ||
return readDirectory(folderPath, url, (html: string) => { | ||
res.writeHead(200, { | ||
'Content-Type': getContentType('file.html') | ||
}); | ||
res.end(html); | ||
}); | ||
} | ||
|
||
const extension = path.extname(url); | ||
if (extension == '' && !url.endsWith('/')) { | ||
res.writeHead(301, { Location: url + '/' }); | ||
return res.end(); | ||
} | ||
|
||
const selectIndexHTML = url.endsWith('/') ? url + 'index.html' : url; | ||
directoryWalker({ | ||
_htmlRedirect: true, | ||
res, | ||
baseUrl: url, | ||
pathname: selectIndexHTML, | ||
folderPath | ||
}); | ||
}); | ||
} |
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,48 @@ | ||
import { HttpServer, Websocket, sendJson } from '../index'; | ||
import { directoryWalker } from '../utils/directories'; | ||
|
||
export default function buildV1Api({ | ||
app, | ||
websocket | ||
}: { | ||
app: HttpServer; | ||
websocket: Websocket; | ||
}) { | ||
app.server.on('upgrade', function (request, socket, head) { | ||
if (request.url == '/ws') { | ||
websocket.socket.handleUpgrade( | ||
request, | ||
socket, | ||
head, | ||
function (ws) { | ||
websocket.socket.emit('connection', ws, request); | ||
} | ||
); | ||
} | ||
}); | ||
|
||
app.route(/\/Songs\/(?<filePath>.*)/, 'GET', (req, res) => { | ||
const url = req.url || '/'; | ||
|
||
const osuInstances: any = Object.values( | ||
req.instanceManager.osuInstances || {} | ||
); | ||
if (osuInstances.length < 1) { | ||
res.statusCode = 500; | ||
return sendJson(res, { error: 'not_ready' }); | ||
} | ||
|
||
const { settings } = osuInstances[0].entities.getServices(['settings']); | ||
if (settings.songsFolder === '') { | ||
res.statusCode = 500; | ||
return sendJson(res, { error: 'not_ready' }); | ||
} | ||
|
||
directoryWalker({ | ||
res, | ||
baseUrl: url, | ||
pathname: req.params.filePath, | ||
folderPath: settings.songsFolder | ||
}); | ||
}); | ||
} |
Oops, something went wrong.