Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: implement file upload #2

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ module.exports = {
'no-empty-function': 'off',
'import/no-extraneous-dependencies': 'off',
'no-extra-semi': 'off',
'no-console': 'off',
'dot-notation': 'off',
'max-len': [
'error',
150,
],
},
};
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ jspm_packages

# autogenerated files
swagger

# dotenv
.env
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cSpell.words": [
"autoswagger",
"eventbridge",
"middyfied",
"typefiles"
]
}
18,295 changes: 12,306 additions & 5,989 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 20 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,37 @@
"description": "Lambdas and DynamoDB",
"main": "serverless.ts",
"scripts": {
"deploy": "npx sls deploy",
"offline": "npx sls offline start",
"print": "npx sls print",
"test": "echo \"Error: no test specified\" && exit 1"
},
"engines": {
"node": ">=14.15.0"
},
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.549.0",
"@aws-sdk/client-eventbridge": "^3.552.0",
"@aws-sdk/client-s3": "^3.550.0",
"@aws-sdk/client-ses": "^3.478.0",
"@aws-sdk/s3-presigned-post": "^3.550.0",
"@middy/core": "^3.4.0",
"@middy/http-json-body-parser": "^3.4.0",
"@middy/validator": "^5.1.0",
"aws-lambda": "^1.0.7",
"aws-sdk": "^2.1522.0",
"serverless-auto-swagger": "^2.12.0",
"serverless-dynamodb": "^0.2.47",
"@tokenizer/s3": "^0.2.3",
"dynamodb-toolbox": "^0.9.2",
"file-type": "^19.0.0",
"http-errors": "^2.0.0",
"install": "^0.13.0",
"npm": "^10.5.1",
"uuid": "^9.0.1"
},
"devDependencies": {
"@serverless/typescript": "^3.0.0",
"@types/aws-lambda": "^8.10.71",
"@types/glob": "^8.1.0",
"@types/http-errors": "^2.0.4",
"@types/node": "^14.14.25",
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"cz-conventional-changelog": "^3.3.0",
Expand All @@ -33,6 +44,10 @@
"eslint-plugin-import": "^2.29.1",
"json-schema-to-ts": "^1.5.0",
"serverless": "^3.0.0",
"serverless-auto-swagger": "^2.12.0",
"serverless-dotenv-plugin": "^6.0.0",
"serverless-dynamodb": "^0.2.47",
"serverless-dynamodb-local": "^0.2.40",
"serverless-esbuild": "^1.23.3",
"serverless-offline": "^13.3.2",
"ts-node": "^10.9.2",
Expand Down
270 changes: 134 additions & 136 deletions serverless.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import type { AWS } from '@serverless/typescript';
import scanFs from '@libs/fs-scanner';
import { ref } from '@libs/ref-factory';

import { EventBridge } from '@resources/event-bridge/event-bridge';

// S3
import { Bucket } from '@resources/s3/s3';

// DynamoDB Tables
import { UsersTable } from '@resources/dynamodb/users-table';
import { ShopsTable } from '@resources/dynamodb/shops-table';
import { FilesTable } from '@resources/dynamodb/files-table';

// Authorizer
import { getUploadUrlAuthorizer } from '@functions/authorizer/upload/config';
// import { getDownloadUrlAuthorizer } from '@functions/authorizer/download/config';

// User
import {
Expand All @@ -16,149 +32,131 @@ import {
} from '@functions/shop/routes';

// Mail
import {
sendMail,
} from '@functions/mail/routes';
import { sendMail } from '@functions/mail/routes';

const serverlessConfiguration: AWS = {
service: 'NodeTeam',
frameworkVersion: '3',
plugins: ['serverless-esbuild', 'serverless-dynamodb', 'serverless-auto-swagger', 'serverless-offline'],
provider: {
name: 'aws',
runtime: 'nodejs18.x',
apiGateway: {
minimumCompressionSize: 1024,
shouldStartNameWithService: true,
},
environment: {
AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
},
},
functions: {
usersGetAll,
usersCreate,
usersGetById,
usersGetByEmail,
setVerifiedUser,
shopsCreate,
shopsGetById,
sendMail,
},
package: { individually: true },
custom: {
esbuild: {
bundle: true,
minify: false,
sourcemap: true,
exclude: ['aws-sdk'],
target: 'node18',
define: { 'require.resolve': undefined },
platform: 'node',
concurrency: 10,
},
autoswagger: {
title: 'NodeTeam',
basePath: '/dev',
},
'serverless-dynamodb': {
start: {
port: 8000,
docker: false,
migrate: true,
inMemory: false,
dbPath: '../.dynamodb',
// File
import { getSignedUploadUrl, listFiles } from '@functions/file/crud/routes';
import { dispatchFileUploadedEvent } from '@functions/file/dispatch-file-uploaded-event/config';
import { onFileUploaded } from '@functions/file/on-file-uploaded/config';

const TYPE_FILE_PATTERN = './src/**/*.d.ts';

const getConfiguration = async (): Promise<AWS> => {
const typeFileNames = await scanFs(TYPE_FILE_PATTERN);

return {
service: 'NodeTeam',
frameworkVersion: '3',
plugins: [
'serverless-dotenv-plugin',
'serverless-auto-swagger',
'serverless-esbuild',
'serverless-dynamodb',
'serverless-offline',
],
provider: {
name: 'aws',
runtime: 'nodejs18.x',
apiGateway: {
minimumCompressionSize: 1024,
shouldStartNameWithService: true,
},
},
},
resources: {
Resources: {
UsersTable: {
Type: 'AWS::DynamoDB::Table',
Properties: {
TableName: 'UsersTable',
AttributeDefinitions: [{
AttributeName: 'userId',
AttributeType: 'S',
}, {
AttributeName: 'email',
AttributeType: 'S',
}],
KeySchema: [{
AttributeName: 'userId',
KeyType: 'HASH',
}],
GlobalSecondaryIndexes: [{
IndexName: 'email-index',
KeySchema: [{
AttributeName: 'email',
KeyType: 'HASH',
}, {
AttributeName: 'userId',
KeyType: 'RANGE',
}],
AttributeDefinitions: [{
AttributeName: 'email',
AttributeType: 'S',
}],
Projection: {
ProjectionType: 'ALL',
},
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
environment: {
AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
},
iamRoleStatements: [
{
Effect: 'Allow',
Resource: [
{
'Fn::Join': ['', [{ 'Fn::GetAtt': ['Bucket', 'Arn'] }, '/*']],
},
}],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
],
Action: ['s3:PutObject', 's3:GetObject', 's3:DeleteObject'],
},
{
Effect: 'Allow',
Resource: [{ 'Fn::GetAtt': ['FilesTable', 'Arn'] }],
Action: [
'dynamodb:Query',
'dynamodb:GetItem',
'dynamodb:DeleteItem',
'dynamodb:PutItem',
],
},
{
Effect: 'Allow',
Resource: [{ 'Fn::GetAtt': ['EventBridge', 'Arn'] }],
Action: ['events:PutEvents'],
},
],
},
functions: {
dispatchFileUploadedEvent,
getUploadUrlAuthorizer,
getSignedUploadUrl,
listFiles,
onFileUploaded,
setVerifiedUser,
shopsCreate,
shopsGetById,
sendMail,
usersCreate,
usersGetAll,
usersGetById,
usersGetByEmail,
},
package: { individually: true },
custom: {
esbuild: {
bundle: true,
minify: false,
sourcemap: true,
exclude: [],
target: 'node18',
define: { 'require.resolve': undefined },
platform: 'node',
concurrency: 10,
},
ShopsTable: {
Type: 'AWS::DynamoDB::Table',
Properties: {
TableName: 'ShopsTable',
AttributeDefinitions: [{
AttributeName: 'shopId',
AttributeType: 'S',
}, {
AttributeName: 'name',
AttributeType: 'S',
}],
KeySchema: [{
AttributeName: 'shopId',
KeyType: 'HASH',
}],
GlobalSecondaryIndexes: [{
IndexName: 'name-index',
KeySchema: [{
AttributeName: 'name',
KeyType: 'HASH',
}, {
AttributeName: 'shopId',
KeyType: 'RANGE',
}],
AttributeDefinitions: [{
AttributeName: 'name',
AttributeType: 'S',
}],
Projection: {
ProjectionType: 'ALL',
},
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
}],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
autoswagger: {
title: 'NodeTeam',
basePath: '/dev',
typefiles: typeFileNames,
apiType: 'http',
apiKeyHeaders: ['Authorization'],
},
'serverless-dynamodb': {
start: {
port: 8000,
docker: false,
migrate: true,
inMemory: false,
dbPath: '../.dynamodb',
},
},
bucketName: ref({ Bucket }),
filesTableName: ref({ FilesTable }),
filesTableStreamArn: { 'Fn::GetAtt': ['FilesTable', 'StreamArn'] },
filesTableArn: { 'Fn::GetAtt': ['FilesTable', 'Arn'] },
eventBridgeArn: 'arn:aws:events:#{AWS::Region}:#{AWS::AccountId}:event-bus/NodeTeam',
eventBusName: ref({ EventBridge }),
getSignedDownloadUrlArn: {
'Fn::GetAtt': ['GetSignedDownloadUrlLambdaFunction', 'Arn'],
},
getSignedUploadUrlArn: {
'Fn::GetAtt': ['GetSignedUploadUrlLambdaFunction', 'Arn'],
},
},
resources: {
Resources: {
Bucket,
EventBridge,
FilesTable,
ShopsTable,
UsersTable,
},
},
},
};
};

module.exports = serverlessConfiguration;
module.exports = getConfiguration();
5 changes: 5 additions & 0 deletions src/constants/dynamodb.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const TABLE_NAMES = {
USERS: 'UsersTable',
SHOPS: 'ShopsTable',
FILES: 'FilesTable2',
} as const;
6 changes: 6 additions & 0 deletions src/constants/eventbridge.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const S4_EVENTS = {
SOURCE: 's4-events',
DETAIL_TYPES: {
FILE_UPLOADED: 'FILE_UPLOADED',
},
} as const;
Loading