2019-12-31 10:50:19 -05:00
|
|
|
import http from 'http'
|
|
|
|
import routers from './routerManager'
|
|
|
|
import {
|
2020-03-19 07:25:05 -04:00
|
|
|
handleResponse,
|
|
|
|
ensureHTTPLink
|
2019-12-31 10:50:19 -05:00
|
|
|
} from './utils'
|
2020-04-10 11:28:46 -04:00
|
|
|
import picgo from '@core/picgo'
|
|
|
|
import logger from '@core/picgo/logger'
|
2020-03-19 07:25:05 -04:00
|
|
|
import axios from 'axios'
|
2019-12-31 10:50:19 -05:00
|
|
|
|
|
|
|
class Server {
|
|
|
|
private httpServer: http.Server
|
2019-12-31 22:58:09 -05:00
|
|
|
private config: IServerConfig
|
2019-12-31 10:50:19 -05:00
|
|
|
constructor () {
|
2020-01-07 21:57:19 -05:00
|
|
|
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
|
|
|
|
})
|
2019-12-31 22:58:09 -05:00
|
|
|
}
|
2019-12-31 10:50:19 -05:00
|
|
|
this.httpServer = http.createServer(this.handleRequest)
|
|
|
|
}
|
2020-01-07 21:57:19 -05:00
|
|
|
private checkIfConfigIsValid (config: IObj | undefined) {
|
|
|
|
if (config && config.port && config.host && (config.enable !== undefined)) {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-12-31 10:50:19 -05:00
|
|
|
private handleRequest = (request: http.IncomingMessage, response: http.ServerResponse) => {
|
|
|
|
if (request.method === 'POST') {
|
|
|
|
if (!routers.getHandler(request.url!)) {
|
|
|
|
handleResponse({
|
|
|
|
response,
|
|
|
|
statusCode: 404,
|
|
|
|
header: {},
|
|
|
|
body: {
|
|
|
|
success: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
let body: string = ''
|
|
|
|
let postObj: IObj
|
|
|
|
request.on('data', chunk => {
|
|
|
|
body += chunk
|
|
|
|
})
|
|
|
|
request.on('end', () => {
|
|
|
|
try {
|
2019-12-31 11:06:48 -05:00
|
|
|
postObj = (body === '') ? {} : JSON.parse(body)
|
2019-12-31 10:50:19 -05:00
|
|
|
} catch (err) {
|
|
|
|
return handleResponse({
|
|
|
|
response,
|
|
|
|
body: {
|
|
|
|
success: false,
|
|
|
|
message: 'Not sending data in JSON format'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const handler = routers.getHandler(request.url!)
|
|
|
|
handler!({
|
|
|
|
...postObj,
|
|
|
|
response
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
response.statusCode = 404
|
|
|
|
response.end()
|
|
|
|
}
|
|
|
|
}
|
2020-03-19 07:25:05 -04:00
|
|
|
// port as string is a bug
|
|
|
|
private listen = (port: number | string) => {
|
2019-12-31 22:58:09 -05:00
|
|
|
logger.info(`[PicGo Server] is listening at ${port}`)
|
2020-03-19 07:25:05 -04:00
|
|
|
if (typeof port === 'string') {
|
|
|
|
port = parseInt(port, 10)
|
|
|
|
}
|
|
|
|
this.httpServer.listen(port, this.config.host).on('error', async (err: ErrnoException) => {
|
2019-12-31 10:50:19 -05:00
|
|
|
if (err.errno === 'EADDRINUSE') {
|
2020-03-19 07:25:05 -04:00
|
|
|
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)
|
|
|
|
}
|
2019-12-31 10:50:19 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
startup () {
|
2019-12-31 22:58:09 -05:00
|
|
|
if (this.config.enable) {
|
|
|
|
this.listen(this.config.port)
|
|
|
|
}
|
2019-12-31 10:50:19 -05:00
|
|
|
}
|
2020-03-19 07:25:05 -04:00
|
|
|
shutdown (hasStarted?: boolean) {
|
2019-12-31 10:50:19 -05:00
|
|
|
this.httpServer.close()
|
2020-03-19 07:25:05 -04:00
|
|
|
if (!hasStarted) {
|
|
|
|
logger.info('[PicGo Server] shutdown')
|
|
|
|
}
|
2019-12-31 22:58:09 -05:00
|
|
|
}
|
|
|
|
restart () {
|
|
|
|
this.config = picgo.getConfig('settings.server')
|
|
|
|
this.shutdown()
|
|
|
|
this.startup()
|
2019-12-31 10:50:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new Server()
|