Skip to content

Commit

Permalink
[Feat] distinguish dev and prod in server and client
Browse files Browse the repository at this point in the history
  • Loading branch information
mistricky committed Feb 24, 2024
1 parent 0a8ec5b commit 2a0374e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion snap-client/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { ControlBar, Editor, Frame, Panel } from "./components";
import { useConfig, useEvent } from "./hooks";
import { toPng, toBlob } from "html-to-image";
import download from "downloadjs";
import { getWebsocketHost } from "./utils";

const CODE_EMPTY_PLACEHOLDER = `print "Hello, CodeSnap.nvim!"`;
const WATER_MARK_PLACEHOLDER = "CodeSnap.nvim";

function App() {
const [socketUrl] = useState(`ws://${window.location.host}/ws`);
const [socketUrl] = useState(`ws://${getWebsocketHost()}/ws`);
const { sendMessage, lastMessage, readyState } = useWebSocket(socketUrl);
const event = useEvent(lastMessage);
const config = useConfig(event?.config_setup);
Expand Down
4 changes: 4 additions & 0 deletions snap-client/src/utils/addr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const getWebsocketHost = () =>
process.env.NODE_ENV === "development"
? "localhost:8080"
: window.location.host;
1 change: 1 addition & 0 deletions snap-client/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./addr";
8 changes: 8 additions & 0 deletions snap-server/src/port.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use std::net::TcpListener;

pub fn get_available_port() -> Option<u16> {
if cfg!(debug_assertions) {
Some(8080)
} else {
find_available_port()
}
}

fn find_available_port() -> Option<u16> {
(8000..10000).find(|port| port_is_available(*port))
}

Expand Down

0 comments on commit 2a0374e

Please sign in to comment.