diff --git a/src/background.ts b/src/background.ts index 23a29832..7925d8b7 100644 --- a/src/background.ts +++ b/src/background.ts @@ -561,9 +561,9 @@ ipcMain.on('toggleShortKeyModifiedMode', (evt: IpcMainEvent, val: boolean) => { bus.emit('toggleShortKeyModifiedMode', val) }) -// const shortKeyHash = { -// upload: uploadClipboardFiles -// } +ipcMain.on('updateServer', () => { + server.restart() +}) const gotTheLock = app.requestSingleInstanceLock() diff --git a/src/main/server/index.ts b/src/main/server/index.ts index 6afeb989..919fe2e1 100644 --- a/src/main/server/index.ts +++ b/src/main/server/index.ts @@ -3,11 +3,18 @@ import routers from './routerManager' import { handleResponse } from './utils' +import picgo from '~/main/utils/picgo' +import logger from '~/main/utils/logger' class Server { private httpServer: http.Server - private port: number = 36677 + private config: IServerConfig constructor () { + this.config = picgo.getConfig('settings.server') || { + port: 36677, + host: '127.0.0.1', + enable: true + } this.httpServer = http.createServer(this.handleRequest) } private handleRequest = (request: http.IncomingMessage, response: http.ServerResponse) => { @@ -52,20 +59,31 @@ class Server { } } private listen = (port: number) => { - console.log(`server listen at ${port}`) - this.httpServer.listen(port, '0.0.0.0').on('error', (err: ErrnoException) => { + logger.info(`[PicGo Server] is listening at ${port}`) + this.httpServer.listen(port, this.config.host).on('error', (err: ErrnoException) => { if (err.errno === 'EADDRINUSE') { - console.log(`----- Port ${port} is busy, trying with port ${port + 1} -----`) - this.port += 1 - this.listen(this.port) + logger.warn(`[PicGo Server] ${port} is busy, trying with port ${port + 1}`) + this.config.port += 1 + picgo.saveConfig({ + 'settings.server.port': this.config.port + }) + this.listen(this.config.port) } }) } startup () { - this.listen(this.port) + if (this.config.enable) { + this.listen(this.config.port) + } } shutdown () { this.httpServer.close() + logger.info('[PicGo Server] shutdown') + } + restart () { + this.config = picgo.getConfig('settings.server') + this.shutdown() + this.startup() } } diff --git a/src/renderer/pages/PicGoSetting.vue b/src/renderer/pages/PicGoSetting.vue index 53a4c57f..ae6ced04 100644 --- a/src/renderer/pages/PicGoSetting.vue +++ b/src/renderer/pages/PicGoSetting.vue @@ -36,6 +36,11 @@ > 点击设置 + + 点击设置 + @@ -239,6 +244,55 @@ 确定 + +
+ 如果你不知道Server的作用,请阅读文档,或者不用修改配置。 +
+ + + + + + + + 取消 + 确定 + +