Skip to content

Commit

Permalink
Fix problem with enabling studio proxy (webhonc) (#297)
Browse files Browse the repository at this point in the history
* Fix initialization of webhonc

* Format
  • Loading branch information
brettimus authored Oct 4, 2024
1 parent 4a7fca4 commit f67233e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
7 changes: 6 additions & 1 deletion api/src/lib/webhonc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ const messageHandlers: {
},
connection_open: async (message, config) => {
const { connectionId } = message.payload;
setWebHoncConnectionId(config.db, connectionId);
logger.debug(
"connection_open message received, setting webhonc connection id:",
connectionId,
);
// Await this call so that the webhonc id is set before the query on the studio side is invalidated
await setWebHoncConnectionId(config.db, connectionId);
for (const ws of config.wsConnections) {
ws.send(
JSON.stringify({
Expand Down
21 changes: 16 additions & 5 deletions api/src/routes/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ app.get("/v0/settings", cors(), async (ctx) => {

/**
* Upsert the settings record
*
* NOTE - We need to start and stop webhonc when the proxy requests setting is updated.
*/
app.post("/v0/settings", cors(), async (ctx) => {
const currentSettings = await getAllSettings(ctx.get("db"));
const prevProxyUrlEnabled = currentSettings?.proxyRequestsEnabled;

const { content } = (await ctx.req.json()) as {
content: Record<string, string>;
};
Expand All @@ -40,16 +45,22 @@ app.post("/v0/settings", cors(), async (ctx) => {

logger.debug("Configuration updated...");

const proxyUrlEnabled = !!Number(
// HACK - We should techincally JSON parse the value here, but whatever.
const proxyUrlEnabled =
updatedSettings.find((setting) => setting.key === "proxyRequestsEnabled")
?.value,
);
?.value === "true";

if (proxyUrlEnabled) {
const shouldStartWebhonc = !prevProxyUrlEnabled && proxyUrlEnabled;
if (shouldStartWebhonc) {
logger.debug("Proxy requests enabled in settings update, starting webhonc");
await webhonc.start();
}

if (!proxyUrlEnabled) {
const shouldStopWebhonc = prevProxyUrlEnabled && !proxyUrlEnabled;
if (shouldStopWebhonc) {
logger.debug(
"Proxy requests disabled in settings update, stopping webhonc",
);
await webhonc.stop();
}

Expand Down
10 changes: 5 additions & 5 deletions studio/src/hooks/useWebsocketQueryInvalidation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WEBHONC_ID_KEY } from "@/components/WebhoncBadge/const";
import { useRealtimeService } from "@/hooks/useRealtimeService";
import { useQueryClient } from "@tanstack/react-query";

Expand All @@ -21,11 +22,10 @@ export function useWebsocketQueryInvalidation() {
}

case "connection_open": {
// TODO: rewriting some webhook/websocket stuff tbd if this is needed
console.debug("connection_open");
// queryClient.invalidateQueries({
// queryKey: [WEBHONC_ID_KEY, WEBHONC_REQUEST_KEY],
// });
console.debug("connection_open - invalidating webhonc id");
queryClient.invalidateQueries({
queryKey: [WEBHONC_ID_KEY],
});
break;
}

Expand Down

0 comments on commit f67233e

Please sign in to comment.