Skip to content

Commit

Permalink
🐛 Fix: server may never start
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Jan 8, 2020
1 parent 5f2b7c7 commit 73870a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
27 changes: 22 additions & 5 deletions src/main/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!)) {
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/utils/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "esnext",
"target": "es2020",
"module": "esnext",
"strict": true,
"jsx": "preserve",
Expand Down

0 comments on commit 73870a5

Please sign in to comment.