diff --git a/src/main/server/index.ts b/src/main/server/index.ts index 0259aff..b7f988f 100644 --- a/src/main/server/index.ts +++ b/src/main/server/index.ts @@ -1,10 +1,12 @@ import http from 'http' import routers from './routerManager' import { - handleResponse + handleResponse, + ensureHTTPLink } from './utils' import picgo from '~/main/apis/picgo' import logger from '~/main/utils/logger' +import axios from 'axios' class Server { private httpServer: http.Server @@ -75,16 +77,24 @@ class Server { response.end() } } - private listen = (port: number) => { + // port as string is a bug + private listen = (port: number | string) => { logger.info(`[PicGo Server] is listening at ${port}`) - this.httpServer.listen(port, this.config.host).on('error', (err: ErrnoException) => { + if (typeof port === 'string') { + port = parseInt(port, 10) + } + this.httpServer.listen(port, this.config.host).on('error', async (err: ErrnoException) => { if (err.errno === 'EADDRINUSE') { - logger.warn(`[PicGo Server] ${port} is busy, trying with port ${port + 1}`) - this.config.port += 1 - picgo.saveConfig({ - 'settings.server': this.config - }) - this.listen(this.config.port) + try { + // make sure the system has a PicGo Server instance + await axios.post(ensureHTTPLink(`${this.config.host}:${port}/heartbeat`)) + this.shutdown(true) + } catch (e) { + logger.warn(`[PicGo Server] ${port} is busy, trying with port ${(port as number) + 1}`) + // fix a bug: not write an increase number to config file + // to solve the auto number problem + this.listen((port as number) + 1) + } } }) } @@ -93,9 +103,11 @@ class Server { this.listen(this.config.port) } } - shutdown () { + shutdown (hasStarted?: boolean) { this.httpServer.close() - logger.info('[PicGo Server] shutdown') + if (!hasStarted) { + logger.info('[PicGo Server] shutdown') + } } restart () { this.config = picgo.getConfig('settings.server') diff --git a/src/main/server/routerManager.ts b/src/main/server/routerManager.ts index feb0b4c..a82a1e4 100644 --- a/src/main/server/routerManager.ts +++ b/src/main/server/routerManager.ts @@ -8,7 +8,7 @@ import { } from './utils' import logger from '../utils/logger' -router.get('/upload', async ({ +router.post('/upload', async ({ response, list = [] } : { @@ -66,4 +66,18 @@ router.get('/upload', async ({ } }) +router.post('/heartbeat', async ({ + response +} : { + response: IHttpResponse, +}) => { + handleResponse({ + response, + body: { + success: true, + result: 'alive' + } + }) +}) + export default router diff --git a/src/main/server/utils.ts b/src/main/server/utils.ts index becbf5d..bbbc2dc 100644 --- a/src/main/server/utils.ts +++ b/src/main/server/utils.ts @@ -17,3 +17,9 @@ export const handleResponse = ({ response.write(JSON.stringify(body)) response.end() } + +export const ensureHTTPLink = (url: string): string => { + return url.startsWith('http') + ? url + : `http://${url}` +} diff --git a/src/renderer/pages/PicGoSetting.vue b/src/renderer/pages/PicGoSetting.vue index bc04235..7060925 100644 --- a/src/renderer/pages/PicGoSetting.vue +++ b/src/renderer/pages/PicGoSetting.vue @@ -541,6 +541,7 @@ export default class extends Vue { this.form.logLevel = logLevel } confirmServerSetting () { + this.server.port = parseInt(this.server.port, 10) this.letPicGoSaveData({ 'settings.server': this.server }) diff --git a/src/universal/types/types.d.ts b/src/universal/types/types.d.ts index 4ada9cf..3a4cabf 100644 --- a/src/universal/types/types.d.ts +++ b/src/universal/types/types.d.ts @@ -30,8 +30,8 @@ interface IServerCTX { } interface IServerConfig { - port: number - host: string, + port: number | string + host: string enable: boolean }