mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 08:26:01 +00:00
electron app - auto find port
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import io from 'socket.io-client';
|
||||
import React from 'react';
|
||||
import resolveApi from './resolveApi';
|
||||
|
||||
const SocketContext = React.createContext(null);
|
||||
|
||||
@@ -7,7 +8,7 @@ export function SocketProvider({ children }) {
|
||||
const [socket, setSocket] = React.useState();
|
||||
React.useEffect(() => {
|
||||
// const newSocket = io('http://localhost:3000', { transports: ['websocket'] });
|
||||
const newSocket = io('http://localhost:3000');
|
||||
const newSocket = io(resolveApi());
|
||||
setSocket(newSocket);
|
||||
}, []);
|
||||
return <SocketContext.Provider value={socket}>{children}</SocketContext.Provider>;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import axios from 'axios';
|
||||
import resolveApi from './resolveApi';
|
||||
|
||||
export default axios.create({
|
||||
baseURL: 'http://localhost:3000',
|
||||
baseURL: resolveApi(),
|
||||
});
|
||||
|
||||
19
packages/web/src/utility/resolveApi.js
Normal file
19
packages/web/src/utility/resolveApi.js
Normal file
@@ -0,0 +1,19 @@
|
||||
export default function resolveApi() {
|
||||
if (window.require) {
|
||||
const electron = window.require('electron');
|
||||
|
||||
if (electron) {
|
||||
const port = electron.remote.getGlobal('port');
|
||||
return `http://localhost:${port}`;
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line
|
||||
const apiUrl = process.env.REACT_APP_API_URL;
|
||||
if (apiUrl) {
|
||||
if (apiUrl == 'ORIGIN') return window.location.origin;
|
||||
return apiUrl;
|
||||
}
|
||||
|
||||
return 'http://localhost:3000';
|
||||
}
|
||||
Reference in New Issue
Block a user