Skip to content

Commit

Permalink
try out proxy possibility
Browse files Browse the repository at this point in the history
  • Loading branch information
rusty-key committed Sep 5, 2024
1 parent 17bd7d5 commit 4bc559e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
61 changes: 52 additions & 9 deletions commands/reshowcase
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require("fs");
const path = require("path");
const os = require("os");
const esbuild = require("esbuild");
const http = require("http");
const { htmlPlugin } = require("@craftamap/esbuild-plugin-html");

const toAbsolutePath = (filepath) => {
Expand Down Expand Up @@ -174,7 +175,7 @@ const defaultConfig = {
};

const getPort = () => {
const defaultPort = 8000;
const defaultPort = 9000;
const prefix = "--port=";
const arg = process.argv.find((item) => item.startsWith(prefix));
if (arg === undefined) {
Expand All @@ -190,9 +191,11 @@ const getPort = () => {
}
};

const {pathRewrites, ...esCustomConfig} = customConfig;

const config = {
...defaultConfig,
...customConfig,
...esCustomConfig,
define: { ...defaultConfig.define, ...(customConfig.define || {}) },
plugins: [...defaultConfig.plugins, ...(customConfig.plugins || [])],
};
Expand All @@ -211,9 +214,7 @@ if (isBuild) {
process.exit(1);
});
} else {
const port = getPort();
const durationLabel = "[Reshowcase] Watch and serve started. Duration";
console.time(durationLabel);
const clientPort = 3030 || getPort();

esbuild
.context(config)
Expand All @@ -225,11 +226,53 @@ if (isBuild) {
process.exit(1);
})
.then((ctx) => {
return ctx.serve({ port: port, servedir: outputPath });
return ctx.serve({ servedir: outputPath })
})
.then((_serveResult) => {
console.timeEnd(durationLabel);
console.error("[Reshowcase] Watch mode started on port:", port);
.then((s) => {
const server = http.createServer((req, res) => {
let changeOrigin = false;
let host = s.host;
let port = s.port;

pathRewrites?.forEach(rewrite => {
if (req.url.startsWith(rewrite.context)) {
const url = new URL(rewrite.target);
host = url.hostname;
port = url.port;
changeOrigin = rewrite.changeOrigin;
}
});

const proxyReq = http.request({
hostname: host,
port: port,
path: req.url,
method: req.method,
headers: {
...req.headers,
...(changeOrigin ? { host: `${host}:${port}` } : {})
},
}, proxyRes => {
res.writeHead(proxyRes.statusCode, proxyRes.headers)
proxyRes.pipe(res, { end: true })
})

proxyReq.on("error", err => {
console.error("Proxy request error:", err);
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Internal Server Error");
});

req.pipe(proxyReq, { end: true })
})

server.listen(clientPort, error => {
if (error) {
return console.error(error)
}

console.log(`[Reshowcase] Server listening on port ${clientPort}`)
})
})
.catch((error) => {
console.error("[Reshowcase] Esbuild serve start failed:", error);
Expand Down
2 changes: 1 addition & 1 deletion dune
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
start
--entry=%{project_root}/example/example/example/Demo.js
--output=%{project_root}/build
--port=8000)))
--port=9000)))

(rule
(alias build-example)
Expand Down
15 changes: 15 additions & 0 deletions example/Demo.re
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ module Css = {
];
};

module Test = {
[@react.component]
let make = () => {
React.useEffect1(
() => {
Fetch.fetch("/v4/tkGetAppearance")->ignore;
None;
},
[||],
);

"I am making request"->React.string;
};
};

demo(({addDemo: _, addCategory}) =>
addCategory("Buttons", ({addDemo, addCategory: _}) => {
addDemo("Normal", ({string, bool, _}) => {
Expand Down

0 comments on commit 4bc559e

Please sign in to comment.