-const routes = {
- '/': function root(response) {
- response.write(fs.readFileSync('host/host.html'))
- response.end()
- },
- '/admin.js': function js(response) {
- response.write(fs.readFileSync('host/host.js'))
- response.end()
- },
- '/adapter.js': function adapter(response) {
- response.write(fs.readFileSync('adapter.js'))
- response.end()
- },
- '/favicon.ico': function favicon(response) {
- response.write('N/A')
- response.end()
- },
- '/streamlist': function streamlist(response) {
- getDefaultStream()
- }
-}
-
-const rooms = {}
-
-
-/* TODO: Swap out url.parse with regex? */
-function determineResponse(request) {
- const clientIP = request.connection.remoteAddress
- || request.socket.remoteAddress
- || request.connection.socket.remoteAddress
- || req.headers['x-forwarded-for'].split(',')[0]
- const parsedURL = url.parse(request.url)
-
- /* Get the room name */
- const pathname = parsedURL.pathname
-
+const router = {
+ skelPage: fs.readFileSync('./skel.html', { encoding: 'utf8' }).split('<!--STRAPP_SRC-->'),
+ clientJS: fs.readFileSync(opts['client-js']),
+ hostJS: fs.readFileSync(opts['host-js']),
+ routes: {},
+ httpsOpt: undefined,
+ httpd: undefined,
+ wsProtocol: opts['no-tls'] ? 'ws' : 'wss',
+ respond: (request,response) => {
+ let body = []
+ request.on('error', function(err) {
+ console.error(`error is ${err}`);
+ }).on('data', function(chunk) {
+ console.log(`chunk is ${chunk}`)
+ body.push(chunk);
+ }).on('end', function() {
+ console.log(`body is ${body}`)
+ })
+ console.log('server handling request')
+ const serveFile = (fPath) => {
+ fs.readFile(fPath, { encoding: 'utf8' }, (err, data) => {
+ if (err || data == undefined) {
+ response.writeHead(404)
+ response.end()
+ }
+ else {
+ response.writeHead(200, { 'Content-Type': mime.lookup(fPath) })
+ response.write(data)
+ response.end()
+ }
+ })
+ }
+ const htArgv = request.url.slice(1).split("?")
+ let routePath = htArgv[0].split('/')
+ let routeName = routePath[0]
+ if (routeName === '' || routeName === 'index.html')
+ serveFile(opts['index'])
+ else if (routeName in opts['bindings']) {
+ let localPath = path.normalize(opts['bindings'][routeName].concat(path.sep + routePath.slice(1).join(path.sep)))
+ if (localPath.includes(opts['bindings'][routeName])) {
+ fs.readdir(localPath, (err, files) => {
+ if (err)
+ serveFile(localPath)
+ else
+ serveFile(`${localPath}/index.html`)
+ })
+ }
+ else {
+ console.log(`SEC: ${localPath} references files not in route`)
+ }
+ }
+ /* TODO: Handle reconnecting host */
+ else if (routeName in router.routes) {