1
0
Fork 0

[Feat] distinguish dev and prod in server and client (#19)

feature/custom-highlight-themes
Mist 2024-02-24 18:07:41 +08:00 committed by GitHub
parent d502bf8162
commit a11ac2246d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 15 additions and 1 deletions

View File

@ -4,12 +4,13 @@ import { ControlBar, Editor, Frame, Panel } from "./components";
import { useConfig, useEvent } from "./hooks"; import { useConfig, useEvent } from "./hooks";
import { toPng, toBlob } from "html-to-image"; import { toPng, toBlob } from "html-to-image";
import download from "downloadjs"; import download from "downloadjs";
import { getWebsocketHost } from "./utils";
const CODE_EMPTY_PLACEHOLDER = `print "Hello, CodeSnap.nvim!"`; const CODE_EMPTY_PLACEHOLDER = `print "Hello, CodeSnap.nvim!"`;
const WATER_MARK_PLACEHOLDER = "CodeSnap.nvim"; const WATER_MARK_PLACEHOLDER = "CodeSnap.nvim";
function App() { function App() {
const [socketUrl] = useState(`ws://${window.location.host}/ws`); const [socketUrl] = useState(`ws://${getWebsocketHost()}/ws`);
const { sendMessage, lastMessage, readyState } = useWebSocket(socketUrl); const { sendMessage, lastMessage, readyState } = useWebSocket(socketUrl);
const event = useEvent(lastMessage); const event = useEvent(lastMessage);
const config = useConfig(event?.config_setup); const config = useConfig(event?.config_setup);

View File

@ -0,0 +1,4 @@
export const getWebsocketHost = () =>
process.env.NODE_ENV === "development"
? "localhost:8080"
: window.location.host;

View File

@ -0,0 +1 @@
export * from "./addr";

View File

@ -1,6 +1,14 @@
use std::net::TcpListener; use std::net::TcpListener;
pub fn get_available_port() -> Option<u16> { 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)) (8000..10000).find(|port| port_is_available(*port))
} }