first-api & file-upload
This commit is contained in:
12
examples/file-upload/backend/src/api/ApiUpload.ts
Normal file
12
examples/file-upload/backend/src/api/ApiUpload.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import fs from "fs/promises";
|
||||
import { ApiCall } from "tsrpc";
|
||||
import { ReqUpload, ResUpload } from "../shared/protocols/PtlUpload";
|
||||
|
||||
export async function ApiUpload(call: ApiCall<ReqUpload, ResUpload>) {
|
||||
// Write to file, or push to remote OSS...
|
||||
await fs.writeFile('uploads/' + call.req.fileName, call.req.fileData);
|
||||
|
||||
call.succ({
|
||||
url: 'http://127.0.0.1:3000/uploads/' + call.req.fileName
|
||||
});
|
||||
}
|
47
examples/file-upload/backend/src/index.ts
Normal file
47
examples/file-upload/backend/src/index.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import fs from "fs/promises";
|
||||
import * as path from "path";
|
||||
import { HttpConnection, HttpServer } from "tsrpc";
|
||||
import { serviceProto } from "./shared/protocols/serviceProto";
|
||||
|
||||
// Create the Server
|
||||
const server = new HttpServer(serviceProto, {
|
||||
port: 3000,
|
||||
cors: '*'
|
||||
});
|
||||
|
||||
// Flow: Serve static files
|
||||
server.flows.preRecvBufferFlow.push(async v => {
|
||||
let conn = v.conn as HttpConnection;
|
||||
if (conn.httpReq.method === 'GET') {
|
||||
if (conn.httpReq.url?.startsWith('/uploads/')) {
|
||||
let file = await fs.readFile(decodeURIComponent(conn.httpReq.url.replace(/^\//, ''))).catch(e => { });
|
||||
if (file) {
|
||||
server.logger.log('GET', conn.httpReq.url);
|
||||
conn.httpRes.end(file);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
conn.httpRes.end('404 Not Found')
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return v;
|
||||
})
|
||||
|
||||
// Entry function
|
||||
async function main() {
|
||||
// Auto implement APIs
|
||||
await server.autoImplementApi(path.resolve(__dirname, 'api'));
|
||||
|
||||
// Ensure "uploads" dir is exists
|
||||
await fs.mkdir('uploads').catch(e => { });
|
||||
|
||||
await server.start();
|
||||
};
|
||||
|
||||
main().catch(e => {
|
||||
// Exit if any error during the startup
|
||||
server.logger.error(e);
|
||||
process.exit(-1);
|
||||
});
|
@@ -0,0 +1,8 @@
|
||||
export interface ReqUpload {
|
||||
fileName: string,
|
||||
fileData: Uint8Array
|
||||
}
|
||||
|
||||
export interface ResUpload {
|
||||
url: string;
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
import { ServiceProto } from 'tsrpc-proto';
|
||||
import { ReqUpload, ResUpload } from './PtlUpload';
|
||||
|
||||
export interface ServiceType {
|
||||
api: {
|
||||
"Upload": {
|
||||
req: ReqUpload,
|
||||
res: ResUpload
|
||||
}
|
||||
},
|
||||
msg: {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const serviceProto: ServiceProto<ServiceType> = {
|
||||
"version": 2,
|
||||
"services": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "Upload",
|
||||
"type": "api"
|
||||
}
|
||||
],
|
||||
"types": {
|
||||
"PtlUpload/ReqUpload": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "fileName",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "fileData",
|
||||
"type": {
|
||||
"type": "Buffer",
|
||||
"arrayType": "Uint8Array"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlUpload/ResUpload": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 2,
|
||||
"name": "url",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user