🐛 Fix: server may never start

This commit is contained in:
Molunerfinn 2020-01-08 10:57:19 +08:00
parent 5f2b7c7802
commit 73870a5326
3 changed files with 24 additions and 7 deletions

View File

@ -10,13 +10,30 @@ class Server {
private httpServer: http.Server
private config: IServerConfig
constructor () {
this.config = picgo.getConfig('settings.server') || {
port: 36677,
host: '127.0.0.1',
enable: true
let config = picgo.getConfig('settings.server')
const result = this.checkIfConfigIsValid(config)
if (result) {
this.config = config
} else {
config = {
port: 36677,
host: '127.0.0.1',
enable: true
}
this.config = config
picgo.saveConfig({
'settings.server': config
})
}
this.httpServer = http.createServer(this.handleRequest)
}
private checkIfConfigIsValid (config: IObj | undefined) {
if (config && config.port && config.host && (config.enable !== undefined)) {
return true
} else {
return false
}
}
private handleRequest = (request: http.IncomingMessage, response: http.ServerResponse) => {
if (request.method === 'POST') {
if (!routers.getHandler(request.url!)) {
@ -65,7 +82,7 @@ class Server {
logger.warn(`[PicGo Server] ${port} is busy, trying with port ${port + 1}`)
this.config.port += 1
picgo.saveConfig({
'settings.server.port': this.config.port
'settings.server': this.config
})
this.listen(this.config.port)
}

View File

@ -47,7 +47,7 @@ class Uploader {
})
picgo.on('uploadProgress', progress => {
this.webContents!.send('uploadProgress', progress)
this.webContents?.send('uploadProgress', progress)
})
picgo.on('beforeTransform', ctx => {
if (db.get('settings.uploadNotification')) {

View File

@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "esnext",
"target": "es2020",
"module": "esnext",
"strict": true,
"jsx": "preserve",