Skip to content

Commit

Permalink
🐛 Fix: confused port number auto increasing when opening a new PicGo app
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Mar 19, 2020
1 parent f357557 commit cd70a1a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
34 changes: 23 additions & 11 deletions src/main/server/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}
})
}
Expand All @@ -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')
Expand Down
16 changes: 15 additions & 1 deletion src/main/server/routerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from './utils'
import logger from '../utils/logger'

router.get('/upload', async ({
router.post('/upload', async ({
response,
list = []
} : {
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions src/main/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}
1 change: 1 addition & 0 deletions src/renderer/pages/PicGoSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
4 changes: 2 additions & 2 deletions src/universal/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ interface IServerCTX {
}

interface IServerConfig {
port: number
host: string,
port: number | string
host: string
enable: boolean
}

Expand Down

0 comments on commit cd70a1a

Please sign in to comment.