Skip to content

Commit fb3febd

Browse files
committed
support to open server on the external broswer
1 parent 0e71eff commit fb3febd

File tree

5 files changed

+59
-9
lines changed

5 files changed

+59
-9
lines changed

console/atest-desktop/index.html

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<div style="margin: 5px; display: flex; justify-content: center">
1313
<div>
1414
<div>Server Status</div>
15+
<div id="address"></div>
1516
<table>
1617
<tr>
1718
<td>
@@ -29,6 +30,14 @@
2930
<input name="extension-registry" id="extension-registry" type="text"/>
3031
</td>
3132
</tr>
33+
<tr>
34+
<td>
35+
<label for="download-timeout">Download timeout</label>
36+
</td>
37+
<td>
38+
<input name="download-timeout" id="download-timeout" type="text"/>
39+
</td>
40+
</tr>
3241
<tr>
3342
<td>Log</td>
3443
<td>
@@ -41,6 +50,7 @@
4150
</td>
4251
<td>
4352
<button type="button" id="open-server-page">Open Server Page</button>
53+
<button type="button" id="open-from-browser">Open from Browser</button>
4454
</td>
4555
</tr>
4656
</table>
@@ -61,6 +71,15 @@
6171
}
6272
})
6373

74+
document.getElementById('open-from-browser').addEventListener('click', async (e) => {
75+
const address = await window.electronAPI.getHomePage();
76+
if (address) {
77+
await window.electronAPI.openWithExternalBrowser(address);
78+
} else {
79+
alert('Please start the server first!');
80+
}
81+
})
82+
6483
const openServerBut = document.getElementById('open-server-page');
6584
openServerBut.addEventListener('click', async (e) => {
6685
window.location = await window.electronAPI.getHomePage()
@@ -91,13 +110,20 @@
91110

92111
const extensionRegistry = document.getElementById('extension-registry');
93112
extensionRegistry.addEventListener("input", function(e) {
94-
console.log('setExtensionRegistry', extensionRegistry.value)
95113
window.electronAPI.setExtensionRegistry(extensionRegistry.value)
96114
});
97115

116+
const downloadTimeout = document.getElementById('download-timeout');
117+
downloadTimeout.addEventListener("input", function(e) {
118+
window.electronAPI.setDownloadTimeout(downloadTimeout.value)
119+
});
120+
98121
(async function() {
99122
portInput.value = await window.electronAPI.getPort()
100123
extensionRegistry.value = await window.electronAPI.getExtensionRegistry()
124+
downloadTimeout.value = await window.electronAPI.getDownloadTimeout()
125+
126+
document.getElementById('address').innerText = await window.electronAPI.getHomePage();
101127
})();
102128
</script>
103129
</body>

console/atest-desktop/main.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Menu.setApplicationMenu(menu)
119119
let serverProcess;
120120
let serverPort = 7788;
121121
let extensionRegistry = "ghcr.io";
122+
let downloadTimeout = "1m";
122123

123124
// This method will be called when Electron has finished
124125
// initialization and is ready to create browser windows.
@@ -127,26 +128,33 @@ app.whenReady().then(() => {
127128
ipcMain.on('openLogDir', () => {
128129
shell.openExternal('file://' + server.getLogfile())
129130
})
131+
ipcMain.handle('openWithExternalBrowser', (e, address) => {
132+
shell.openExternal(address)
133+
})
130134
ipcMain.on('startServer', startServer)
131135
ipcMain.on('stopServer', stopServer)
132136
ipcMain.on('control', (e, okCallback, errCallback) => {
133137
server.control(okCallback, errCallback)
134138
})
139+
ipcMain.handle('getPort', () => {
140+
return serverPort
141+
})
135142
ipcMain.handle('setPort', (e, port) => {
136-
console.log('setPort', port)
137143
serverPort = port;
138144
})
145+
ipcMain.handle('getExtensionRegistry', () => {
146+
return extensionRegistry
147+
})
139148
ipcMain.handle('setExtensionRegistry', (e, registry) => {
140-
console.log('setExtensionRegistry', registry)
141149
extensionRegistry = registry
142150
})
143-
ipcMain.handle('getExtensionRegistry', () => {
144-
return extensionRegistry
151+
ipcMain.handle('getDownloadTimeout', () => {
152+
return downloadTimeout
145153
})
146-
ipcMain.handle('getHomePage', server.getHomePage)
147-
ipcMain.handle('getPort', () => {
148-
return serverPort
154+
ipcMain.handle('setDownloadTimeout', (e, timeout) => {
155+
downloadTimeout = timeout
149156
})
157+
ipcMain.handle('getHomePage', server.getHomePage)
150158
ipcMain.handle('getHealthzUrl', server.getHealthzUrl)
151159

152160
startServer()
@@ -195,6 +203,7 @@ const startServer = () => {
195203
"server",
196204
`--http-port=${serverPort}`,
197205
"--port=0",
206+
`--download-timeout=${downloadTimeout}`,
198207
`--extension-registry=${extensionRegistry}`,
199208
"--local-storage", path.join(homeData, "*.yaml")
200209
])

console/atest-desktop/preload.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ window.addEventListener('DOMContentLoaded', () => {
3131

3232
contextBridge.exposeInMainWorld('electronAPI', {
3333
openLogDir: () => ipcRenderer.send('openLogDir'),
34+
openWithExternalBrowser: (address) => ipcRenderer.invoke('openWithExternalBrowser', address),
3435
startServer: () => ipcRenderer.send('startServer'),
3536
stopServer: () => ipcRenderer.send('stopServer'),
3637
control: (okCallback, errCallback) => ipcRenderer.send('control', okCallback, errCallback),
@@ -39,5 +40,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
3940
setPort: (port) => ipcRenderer.invoke('setPort', port),
4041
setExtensionRegistry: (registry) => ipcRenderer.invoke('setExtensionRegistry', registry),
4142
getExtensionRegistry: () => ipcRenderer.invoke('getExtensionRegistry'),
43+
getDownloadTimeout: () => ipcRenderer.invoke('getDownloadTimeout'),
44+
setDownloadTimeout: (timeout) => ipcRenderer.invoke('setDownloadTimeout', timeout),
4245
getHealthzUrl: () => ipcRenderer.invoke('getHealthzUrl'),
4346
})

pkg/server/store_ext_manager.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,24 @@ func (s *storeExtManager) StopAll() error {
160160
serverLogger.Info("stop", "extensions", len(s.processs))
161161
for _, p := range s.processs {
162162
if p != nil {
163-
p.Signal(syscall.SIGTERM)
163+
// Use Kill on Windows, Signal on other platforms
164+
if isWindows() {
165+
p.Kill()
166+
} else {
167+
p.Signal(syscall.SIGTERM)
168+
}
164169
}
165170
}
166171
s.stopSingal <- struct{}{}
167172
return nil
168173
}
169174

175+
// isWindows returns true if the program is running on Windows OS.
176+
func isWindows() bool {
177+
return strings.Contains(strings.ToLower(os.Getenv("OS")), "windows") ||
178+
(strings.Contains(strings.ToLower(os.Getenv("GOOS")), "windows"))
179+
}
180+
170181
func (s *storeExtManager) WithDownloader(ociDownloader downloader.PlatformAwareOCIDownloader) {
171182
s.ociDownloader = ociDownloader
172183
}

pkg/server/store_ext_manager_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func TestStoreExtManager(t *testing.T) {
4444
err = mgr.Start("go", "")
4545
assert.NoError(t, err)
4646

47+
time.Sleep(time.Microsecond * 100)
4748
err = mgr.StopAll()
4849
assert.NoError(t, err)
4950
})

0 commit comments

Comments
 (0)