init new sub project, backend
This commit is contained in:
2
backend/.gitignore
vendored
Normal file
2
backend/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/dist/
|
||||
/node_modules/
|
0
backend/build.ts
Normal file
0
backend/build.ts
Normal file
2338
backend/package-lock.json
generated
Normal file
2338
backend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
25
backend/package.json
Normal file
25
backend/package.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "imbusy-be",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"build": "esbuild src/index.ts --bundle --platform=node --target=node18 --outfile=dist/index.js",
|
||||
"clean": "rm -rf dist",
|
||||
"watch": "npm run build -- --watch"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"type": "commonjs",
|
||||
"devDependencies": {
|
||||
"@types/aws-lambda": "^8.10.150",
|
||||
"@types/node": "^24.0.3",
|
||||
"esbuild": "^0.25.5",
|
||||
"typescript": "^5.8.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-apigatewaymanagementapi": "^3.830.0",
|
||||
"aws-sdk": "^2.1692.0"
|
||||
}
|
||||
}
|
48
backend/src/index.ts
Normal file
48
backend/src/index.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { ApiGatewayManagementApiClient, PostToConnectionCommand } from "@aws-sdk/client-apigatewaymanagementapi";
|
||||
import { APIGatewayProxyHandlerV2 } from "aws-lambda";
|
||||
|
||||
export const main: APIGatewayProxyHandlerV2 = async (event) => {
|
||||
const body = event.body;
|
||||
const {domainName, stage, connectionId, routeKey} = event.requestContext;
|
||||
|
||||
const apiGateway = new ApiGatewayManagementApiClient({
|
||||
endpoint: `https://${domainName}/${stage}`,
|
||||
});
|
||||
|
||||
console.log("Received event: ", JSON.stringify(event, null, 2));
|
||||
|
||||
try {
|
||||
switch (routeKey) {
|
||||
case '$connect':
|
||||
console.log(`New connection: ${connectionId}`);
|
||||
return { statusCode: 200, body: 'Connected' };
|
||||
|
||||
case '$disconnect':
|
||||
console.log(`Disconnected: ${connectionId}`);
|
||||
return { statusCode: 200, body: 'Disconnected' };
|
||||
|
||||
default:
|
||||
console.log(`Received message: ${body}`);
|
||||
await sendMessageToClient(apiGateway, connectionId, `Echo: ${body}`);
|
||||
return { statusCode: 200, body: `Echoed: ${body}` };
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error: ', error);
|
||||
return { statusCode: 500, body: 'Error processing request' };
|
||||
}
|
||||
};
|
||||
|
||||
// メッセージをクライアントに送信する関数(修正済み)
|
||||
const sendMessageToClient = async (apiGateway, connectionId, message) => {
|
||||
try {
|
||||
const command = new PostToConnectionCommand({
|
||||
ConnectionId: connectionId,
|
||||
Data: Buffer.from(message),
|
||||
});
|
||||
|
||||
await apiGateway.send(command);
|
||||
console.log(`Sent message to ${connectionId}: ${message}`);
|
||||
} catch (error) {
|
||||
console.error(`Failed to send message to ${connectionId}:`, error);
|
||||
}
|
||||
};
|
11
backend/tsconfig.json
Normal file
11
backend/tsconfig.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"module": "commonjs",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"outDir": "dist",
|
||||
"rootDir": "src"
|
||||
},
|
||||
"include": ["src/**/*"]
|
||||
}
|
Reference in New Issue
Block a user