-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Feature(server): add http server for uploading images by a http req…
…uest port 37766
- Loading branch information
1 parent
3fd9572
commit c56d4ef
Showing
13 changed files
with
558 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import http from 'http' | ||
import routers from './routerManager' | ||
import { | ||
handleResponse | ||
} from './utils' | ||
|
||
class Server { | ||
private httpServer: http.Server | ||
private port: number = 36677 | ||
constructor () { | ||
this.httpServer = http.createServer(this.handleRequest) | ||
} | ||
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 { | ||
postObj = JSON.parse(body) | ||
} 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() | ||
} | ||
} | ||
private listen = (port: number) => { | ||
console.log(`server listen at ${port}`) | ||
this.httpServer.listen(port).on('error', (err: ErrnoException) => { | ||
if (err.errno === 'EADDRINUSE') { | ||
console.log(`----- Port ${port} is busy, trying with port ${port + 1} -----`) | ||
this.port += 1 | ||
this.listen(this.port) | ||
} | ||
}) | ||
} | ||
startup () { | ||
this.listen(this.port) | ||
} | ||
shutdown () { | ||
this.httpServer.close() | ||
} | ||
} | ||
|
||
export default new Server() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Router { | ||
private router = new Map<string, routeHandler>() | ||
|
||
get (url: string, callback: routeHandler): void { | ||
this.router.set(url, callback) | ||
} | ||
post (url: string, callback: routeHandler): void { | ||
this.router.set(url, callback) | ||
} | ||
|
||
getHandler (url: string) { | ||
if (this.router.has(url)) { | ||
return this.router.get(url) | ||
} else { | ||
return null | ||
} | ||
} | ||
} | ||
|
||
export default new Router() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import router from './router' | ||
import { | ||
uploadWithClipboardFiles, | ||
uploadWithFiles | ||
} from '~/main/utils/busApi/index' | ||
import { | ||
handleResponse | ||
} from './utils' | ||
import logger from '../utils/logger' | ||
|
||
router.get('/upload', async ({ | ||
response, | ||
list = [] | ||
} : { | ||
response: IHttpResponse, | ||
list?: string[] | ||
}): Promise<void> => { | ||
try { | ||
if (list.length === 0) { | ||
// upload with clipboard | ||
const res = await uploadWithClipboardFiles() | ||
if (res.success) { | ||
handleResponse({ | ||
response, | ||
body: { | ||
success: true, | ||
result: [res.result] | ||
} | ||
}) | ||
} else { | ||
handleResponse({ | ||
response | ||
}) | ||
} | ||
} else { | ||
// upload with files | ||
const pathList = list.map(item => { | ||
return { | ||
path: item | ||
} | ||
}) | ||
const res = await uploadWithFiles(pathList) | ||
if (res.success) { | ||
handleResponse({ | ||
response, | ||
body: { | ||
success: true, | ||
result: res.result | ||
} | ||
}) | ||
} else { | ||
handleResponse({ | ||
response | ||
}) | ||
} | ||
} | ||
} catch (err) { | ||
logger.error(err) | ||
handleResponse({ | ||
response, | ||
body: { | ||
success: false, | ||
message: err | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
export default router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export const handleResponse = ({ | ||
response, | ||
statusCode = 200, | ||
header = { | ||
'Content-Type': 'application/json' | ||
}, | ||
body = { | ||
success: false | ||
} | ||
} : { | ||
response: IHttpResponse, | ||
statusCode?: number, | ||
header?: IObj, | ||
body?: any | ||
}) => { | ||
response.writeHead(statusCode, header) | ||
response.write(JSON.stringify(body)) | ||
response.end() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const GET_SETTING_WINDOW = 'GET_SETTING_WINDOW' | ||
export const GET_MINI_WINDOW = 'GET_SETTING_WINDOW' | ||
export const UPLOAD_WITH_FILES = 'UPLOAD_WITH_FILES' | ||
export const UPLOAD_WITH_FILES_RESPONSE = 'UPLOAD_WITH_FILES_RESPONSE' | ||
export const UPLOAD_WITH_CLIPBOARD_FILES = 'UPLOAD_WITH_CLIPBOARD_FILES' | ||
export const UPLOAD_WITH_CLIPBOARD_FILES_RESPONSE = 'UPLOAD_WITH_CLIPBOARD_FILES_RESPONSE' |
Oops, something went wrong.