🐛 Fix: confused port number auto increasing when opening a new PicGo app

This commit is contained in:
Molunerfinn 2020-03-19 19:25:05 +08:00
parent f3575575ac
commit cd70a1a5cc
5 changed files with 47 additions and 14 deletions

View File

@ -1,10 +1,12 @@
import http from 'http' import http from 'http'
import routers from './routerManager' import routers from './routerManager'
import { import {
handleResponse handleResponse,
ensureHTTPLink
} from './utils' } from './utils'
import picgo from '~/main/apis/picgo' import picgo from '~/main/apis/picgo'
import logger from '~/main/utils/logger' import logger from '~/main/utils/logger'
import axios from 'axios'
class Server { class Server {
private httpServer: http.Server private httpServer: http.Server
@ -75,16 +77,24 @@ class Server {
response.end() 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}`) 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') { if (err.errno === 'EADDRINUSE') {
logger.warn(`[PicGo Server] ${port} is busy, trying with port ${port + 1}`) try {
this.config.port += 1 // make sure the system has a PicGo Server instance
picgo.saveConfig({ await axios.post(ensureHTTPLink(`${this.config.host}:${port}/heartbeat`))
'settings.server': this.config this.shutdown(true)
}) } catch (e) {
this.listen(this.config.port) 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) this.listen(this.config.port)
} }
} }
shutdown () { shutdown (hasStarted?: boolean) {
this.httpServer.close() this.httpServer.close()
logger.info('[PicGo Server] shutdown') if (!hasStarted) {
logger.info('[PicGo Server] shutdown')
}
} }
restart () { restart () {
this.config = picgo.getConfig('settings.server') this.config = picgo.getConfig('settings.server')

View File

@ -8,7 +8,7 @@ import {
} from './utils' } from './utils'
import logger from '../utils/logger' import logger from '../utils/logger'
router.get('/upload', async ({ router.post('/upload', async ({
response, response,
list = [] 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 export default router

View File

@ -17,3 +17,9 @@ export const handleResponse = ({
response.write(JSON.stringify(body)) response.write(JSON.stringify(body))
response.end() response.end()
} }
export const ensureHTTPLink = (url: string): string => {
return url.startsWith('http')
? url
: `http://${url}`
}

View File

@ -541,6 +541,7 @@ export default class extends Vue {
this.form.logLevel = logLevel this.form.logLevel = logLevel
} }
confirmServerSetting () { confirmServerSetting () {
this.server.port = parseInt(this.server.port, 10)
this.letPicGoSaveData({ this.letPicGoSaveData({
'settings.server': this.server 'settings.server': this.server
}) })

View File

@ -30,8 +30,8 @@ interface IServerCTX {
} }
interface IServerConfig { interface IServerConfig {
port: number port: number | string
host: string, host: string
enable: boolean enable: boolean
} }