Skip to content

Commit cd70a1a

Browse files
committed
🐛 Fix: confused port number auto increasing when opening a new PicGo app
1 parent f357557 commit cd70a1a

File tree

5 files changed

+47
-14
lines changed

5 files changed

+47
-14
lines changed

src/main/server/index.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import http from 'http'
22
import routers from './routerManager'
33
import {
4-
handleResponse
4+
handleResponse,
5+
ensureHTTPLink
56
} from './utils'
67
import picgo from '~/main/apis/picgo'
78
import logger from '~/main/utils/logger'
9+
import axios from 'axios'
810

911
class Server {
1012
private httpServer: http.Server
@@ -75,16 +77,24 @@ class Server {
7577
response.end()
7678
}
7779
}
78-
private listen = (port: number) => {
80+
// port as string is a bug
81+
private listen = (port: number | string) => {
7982
logger.info(`[PicGo Server] is listening at ${port}`)
80-
this.httpServer.listen(port, this.config.host).on('error', (err: ErrnoException) => {
83+
if (typeof port === 'string') {
84+
port = parseInt(port, 10)
85+
}
86+
this.httpServer.listen(port, this.config.host).on('error', async (err: ErrnoException) => {
8187
if (err.errno === 'EADDRINUSE') {
82-
logger.warn(`[PicGo Server] ${port} is busy, trying with port ${port + 1}`)
83-
this.config.port += 1
84-
picgo.saveConfig({
85-
'settings.server': this.config
86-
})
87-
this.listen(this.config.port)
88+
try {
89+
// make sure the system has a PicGo Server instance
90+
await axios.post(ensureHTTPLink(`${this.config.host}:${port}/heartbeat`))
91+
this.shutdown(true)
92+
} catch (e) {
93+
logger.warn(`[PicGo Server] ${port} is busy, trying with port ${(port as number) + 1}`)
94+
// fix a bug: not write an increase number to config file
95+
// to solve the auto number problem
96+
this.listen((port as number) + 1)
97+
}
8898
}
8999
})
90100
}
@@ -93,9 +103,11 @@ class Server {
93103
this.listen(this.config.port)
94104
}
95105
}
96-
shutdown () {
106+
shutdown (hasStarted?: boolean) {
97107
this.httpServer.close()
98-
logger.info('[PicGo Server] shutdown')
108+
if (!hasStarted) {
109+
logger.info('[PicGo Server] shutdown')
110+
}
99111
}
100112
restart () {
101113
this.config = picgo.getConfig('settings.server')

src/main/server/routerManager.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from './utils'
99
import logger from '../utils/logger'
1010

11-
router.get('/upload', async ({
11+
router.post('/upload', async ({
1212
response,
1313
list = []
1414
} : {
@@ -66,4 +66,18 @@ router.get('/upload', async ({
6666
}
6767
})
6868

69+
router.post('/heartbeat', async ({
70+
response
71+
} : {
72+
response: IHttpResponse,
73+
}) => {
74+
handleResponse({
75+
response,
76+
body: {
77+
success: true,
78+
result: 'alive'
79+
}
80+
})
81+
})
82+
6983
export default router

src/main/server/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ export const handleResponse = ({
1717
response.write(JSON.stringify(body))
1818
response.end()
1919
}
20+
21+
export const ensureHTTPLink = (url: string): string => {
22+
return url.startsWith('http')
23+
? url
24+
: `http://${url}`
25+
}

src/renderer/pages/PicGoSetting.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ export default class extends Vue {
541541
this.form.logLevel = logLevel
542542
}
543543
confirmServerSetting () {
544+
this.server.port = parseInt(this.server.port, 10)
544545
this.letPicGoSaveData({
545546
'settings.server': this.server
546547
})

src/universal/types/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ interface IServerCTX {
3030
}
3131

3232
interface IServerConfig {
33-
port: number
34-
host: string,
33+
port: number | string
34+
host: string
3535
enable: boolean
3636
}
3737

0 commit comments

Comments
 (0)