-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Broadcast stream instead of pipe, split arguments, add player statist…
…ics to application settings
- Loading branch information
Showing
16 changed files
with
201 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,63 @@ | ||
import * as esbuild from 'esbuild'; | ||
import { esbuildSiteConfig } from './build.site.mjs'; | ||
|
||
await esbuild.context(esbuildSiteConfig).then((x) => x.watch()); | ||
import http from 'node:http'; | ||
|
||
console.info('- watch site'); | ||
|
||
const raspi = { hostname: '192.168.3.139', port: '8000' }; | ||
|
||
// Start esbuild's server on a random local port | ||
const ctx = await esbuild.context({ ...esbuildSiteConfig, minify: false }); | ||
|
||
// The return value tells us where esbuild's local server is | ||
const { host, port } = await ctx.serve({ servedir: './build/public/', port: 8001 }); | ||
|
||
// Then start a proxy server on port 3000 | ||
http | ||
.createServer((req, res) => { | ||
// Forward requests to Raspberry PI | ||
const apiReq = http.request( | ||
{ | ||
hostname: raspi.hostname, | ||
port: raspi.port, | ||
path: req.url, | ||
method: req.method, | ||
headers: req.headers, | ||
}, | ||
(resp) => { | ||
resp.pipe(res, { end: true }); | ||
}, | ||
); | ||
|
||
if (req.url?.startsWith('/api')) { | ||
req.pipe(apiReq, { end: true }); | ||
return; | ||
} | ||
|
||
// Forward to esbuild serve | ||
const proxyReq = http.request( | ||
{ | ||
hostname: host, | ||
port: port, | ||
path: req.url, | ||
method: req.method, | ||
headers: req.headers, | ||
}, | ||
(proxyRes) => { | ||
// If esbuild returns "not found", send a custom 404 page | ||
if (proxyRes.statusCode === 404) { | ||
res.writeHead(404, { 'Content-Type': 'text/html' }); | ||
res.end('<h1>A custom 404 page</h1>'); | ||
return; | ||
} | ||
|
||
// Otherwise, forward the response from esbuild to the client | ||
res.writeHead(proxyRes?.statusCode || 0, proxyRes.headers); | ||
proxyRes.pipe(res, { end: true }); | ||
}, | ||
); | ||
|
||
// Forward the body of the request to esbuild | ||
req.pipe(proxyReq, { end: true }); | ||
}) | ||
.listen(8000); |
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,11 @@ | ||
import yargs from 'yargs/yargs'; | ||
|
||
export const parseArguments = () => | ||
yargs(process.argv.slice(2)) | ||
.options({ | ||
p: { type: 'number', alias: 'port', default: 8000 }, | ||
c: { type: 'boolean', alias: 'cors', default: false }, | ||
}) | ||
.parseSync(); | ||
|
||
export type Arguments = ReturnType<typeof parseArguments>; |
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
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.