custom http res
This commit is contained in:
26
examples/custom-http-res/src/api/ApiAddData.ts
Normal file
26
examples/custom-http-res/src/api/ApiAddData.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { ApiCall } from "tsrpc";
|
||||
import { ReqAddData, ResAddData } from "../shared/protocols/PtlAddData";
|
||||
import { AllData } from "./ApiGetData";
|
||||
|
||||
// This is a demo code file
|
||||
// Feel free to delete it
|
||||
|
||||
export async function ApiAddData(call: ApiCall<ReqAddData, ResAddData>) {
|
||||
// Error
|
||||
if (call.req.content === '') {
|
||||
call.error('Content is empty');
|
||||
return;
|
||||
}
|
||||
|
||||
let time = new Date();
|
||||
AllData.unshift({
|
||||
content: call.req.content,
|
||||
time: time
|
||||
})
|
||||
console.log('AllData', AllData)
|
||||
|
||||
// Success
|
||||
call.succ({
|
||||
time: time
|
||||
});
|
||||
}
|
13
examples/custom-http-res/src/api/ApiGetData.ts
Normal file
13
examples/custom-http-res/src/api/ApiGetData.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { ApiCall } from "tsrpc";
|
||||
import { ReqGetData, ResGetData } from "../shared/protocols/PtlGetData";
|
||||
|
||||
// This is a demo code file
|
||||
// Feel free to delete it
|
||||
|
||||
export async function ApiGetData(call: ApiCall<ReqGetData, ResGetData>) {
|
||||
call.succ({
|
||||
data: AllData
|
||||
})
|
||||
}
|
||||
|
||||
export const AllData: { content: string, time: Date }[] = [];
|
45
examples/custom-http-res/src/index.ts
Normal file
45
examples/custom-http-res/src/index.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
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: '*'
|
||||
});
|
||||
|
||||
// Custom HTTP Reponse
|
||||
server.flows.preRecvBufferFlow.push(v => {
|
||||
let conn = v.conn as HttpConnection;
|
||||
|
||||
if (conn.httpReq.method === 'GET') {
|
||||
// 特定 URL: /test
|
||||
if (conn.httpReq.url === '/test') {
|
||||
conn.httpRes.end('test test')
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// 默认 GET 响应
|
||||
conn.httpRes.end('Hello World');
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return v;
|
||||
})
|
||||
|
||||
// Entry function
|
||||
async function main() {
|
||||
// Auto implement APIs
|
||||
await server.autoImplementApi(path.resolve(__dirname, 'api'));
|
||||
|
||||
// TODO
|
||||
// Prepare something... (e.g. connect the db)
|
||||
|
||||
await server.start();
|
||||
};
|
||||
|
||||
main().catch(e => {
|
||||
// Exit if any error during the startup
|
||||
server.logger.error(e);
|
||||
process.exit(-1);
|
||||
});
|
10
examples/custom-http-res/src/shared/protocols/PtlAddData.ts
Normal file
10
examples/custom-http-res/src/shared/protocols/PtlAddData.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
// This is a demo code file
|
||||
// Feel free to delete it
|
||||
|
||||
export interface ReqAddData {
|
||||
content: string;
|
||||
}
|
||||
|
||||
export interface ResAddData {
|
||||
time: Date
|
||||
}
|
13
examples/custom-http-res/src/shared/protocols/PtlGetData.ts
Normal file
13
examples/custom-http-res/src/shared/protocols/PtlGetData.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
// This is a demo code file
|
||||
// Feel free to delete it
|
||||
|
||||
export interface ReqGetData {
|
||||
|
||||
}
|
||||
|
||||
export interface ResGetData {
|
||||
data: {
|
||||
content: string,
|
||||
time: Date
|
||||
}[]
|
||||
}
|
@@ -0,0 +1,98 @@
|
||||
import { ServiceProto } from 'tsrpc-proto';
|
||||
import { ReqAddData, ResAddData } from './PtlAddData';
|
||||
import { ReqGetData, ResGetData } from './PtlGetData';
|
||||
|
||||
// This is a demo service proto file (auto generated)
|
||||
// Feel free to delete it
|
||||
|
||||
export interface ServiceType {
|
||||
api: {
|
||||
"AddData": {
|
||||
req: ReqAddData,
|
||||
res: ResAddData
|
||||
},
|
||||
"GetData": {
|
||||
req: ReqGetData,
|
||||
res: ResGetData
|
||||
}
|
||||
},
|
||||
msg: {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const serviceProto: ServiceProto<ServiceType> = {
|
||||
"version": 1,
|
||||
"services": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "AddData",
|
||||
"type": "api"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "GetData",
|
||||
"type": "api"
|
||||
}
|
||||
],
|
||||
"types": {
|
||||
"PtlAddData/ReqAddData": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "content",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlAddData/ResAddData": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "time",
|
||||
"type": {
|
||||
"type": "Date"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlGetData/ReqGetData": {
|
||||
"type": "Interface"
|
||||
},
|
||||
"PtlGetData/ResGetData": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "data",
|
||||
"type": {
|
||||
"type": "Array",
|
||||
"elementType": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "content",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "time",
|
||||
"type": {
|
||||
"type": "Date"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user