Skip to content

Commit

Permalink
add server
Browse files Browse the repository at this point in the history
  • Loading branch information
victorekpo committed Jul 12, 2024
1 parent 5d9ccdd commit 62e5068
Show file tree
Hide file tree
Showing 25 changed files with 661 additions and 4 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ package-lock.json
local.js
assets/pages.js
assets/partials.js
.wrangler
.wrangler

dist/
.idea/
cdk.out/
.aws-sam/
47 changes: 47 additions & 0 deletions server/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module.exports = {
parser: '@typescript-eslint/parser',
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended'
],
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module'
},
env: {
es6: true,
node: true,
browser: true
},
// Ignore files within the node_modules directory
ignorePatterns: ['node_modules/**', 'cdk.out/**'],
rules: {
'no-duplicate-case': 'error',
'no-empty': 'error',
'no-extra-semi': 'error',
'no-func-assign': 'error',
'no-irregular-whitespace': 'error',
'no-unreachable': 'error',
curly: 'error',
'dot-notation': 'error',
eqeqeq: 'error',
'no-empty-function': 'error',
'no-multi-spaces': 'error',
'no-mixed-spaces-and-tabs': 'error',
'no-trailing-spaces': 'error',
'default-case': 'error',
'no-fallthrough': 'error',
'no-unused-vars': 'error',
'no-use-before-define': 'error',
'no-redeclare': 'error',
'brace-style': 'error',
indent: ['error', 2],
quotes: ['error', 'single'],
semi: ['error', 'always'],
radix: 'off',
// TypeScript specific rules
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/no-use-before-define': 'error',
'@typescript-eslint/no-redeclare': 'error'
}
};
40 changes: 40 additions & 0 deletions server/.github/workflows/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Manual Bootstrap CDK

on:
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '20'

- name: Install AWS CLI
run: |
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
- name: Configure AWS CLI
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: 'us-east-1' # Replace with your preferred AWS region
run: |
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
aws configure set region $AWS_DEFAULT_REGION
echo "access_key: $AWS_ACCESS_KEY_ID"
echo "secret_key: AWS_SECRET_ACCESS_KEY"
- name: Install dependencies
run: npm install

- name: Run tests
run: npm run bootstrap
38 changes: 38 additions & 0 deletions server/.github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Manual Deploy CDK

on:
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '20'

- name: Install AWS CLI
run: |
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
- name: Configure AWS CLI
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: 'us-east-1' # Replace with your preferred AWS region
run: |
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
aws configure set region $AWS_DEFAULT_REGION
- name: Install dependencies
run: npm install

- name: Run tests
run: npm run deploy
24 changes: 24 additions & 0 deletions server/.github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test CDK Stack

on:
push:
branches:
- '*'

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '20'

- name: Install dependencies
run: npm install

- name: Run tests
run: npm test
8 changes: 8 additions & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.env*
node_modules/
dist/
local.js
.idea/
package-lock.json
cdk.out/
.aws-sam/
1 change: 1 addition & 0 deletions server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Serverless Backend
41 changes: 41 additions & 0 deletions server/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"app": "npx ts-node --prefer-ts-exts src/index.ts",
"watch": {
"include": [
"**"
],
"exclude": [
"README.md",
"cdk*.json",
"**/*.d.ts",
"**/*.js",
"tsconfig.json",
"package*.json",
"yarn.lock",
"node_modules",
"test"
]
},
"context": {
"appName": "MyAwesomeApp",
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
"@aws-cdk/core:stackRelativeExports": true,
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
"@aws-cdk/aws-lambda:recognizeVersionProps": true,
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true,
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
"@aws-cdk/core:checkSecretUsage": true,
"@aws-cdk/aws-iam:minimizePolicies": true,
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
"@aws-cdk/core:target-partitions": [
"aws",
"aws-cn"
]
}
}
Empty file added server/develop.sh
Empty file.
8 changes: 8 additions & 0 deletions server/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/src'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
}
};
9 changes: 9 additions & 0 deletions server/local/api/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

npm run synth

## sam.cmd for windows regular sam for linux/mac
sam.cmd build \
-t ../../cdk.out/SagaStack-Demo.template.json

sam.cmd local start-api
4 changes: 4 additions & 0 deletions server/local/lambdas/reserveFLights/env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Parameters": {
}
}
7 changes: 7 additions & 0 deletions server/local/lambdas/reserveFLights/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"departCity": "Milwaukee",
"departTime": "2024-01-01T00:00:00.000Z",
"arriveCity": "Abuja",
"arriveTime": "2024-01-03T00:00:00.000Z",
"requestId": "tripRequest123"
}
16 changes: 16 additions & 0 deletions server/local/lambdas/reserveFLights/reserveFlights.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Local Testing of Lambda Functions
```
sam local invoke StateMachinereserveFlightLambdaHandler77D8E840 -t ./cdk.out/CdkServerlessSagaStack.template.json
```

## Local Testing of API
```
sam local start-api
```

[Local Step Functions Testing](https://docs.aws.amazon.com/step-functions/latest/dg/sfn-local-lambda.html)

[Local SAM Testing](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-cdk-getting-started.html)

Examples
-`cdk synth && sam local invoke StateMachinereserveFlightLambdaHandler77D8E840 -t ./cdk.out/CdkServerlessSagaStack.template.json --env-vars ./env.json --event ./event.json`
9 changes: 9 additions & 0 deletions server/local/lambdas/reserveFLights/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

npm run synth

# sam.cmd for windows regular sam for linux/mac
sam.cmd local invoke StateMachinereserveFlightLambdaHandler77D8E840 \
-t ../../../cdk.out/SagaStack-Demo.template.json \
--env-vars ./env.json \
--event ./event.json
37 changes: 34 additions & 3 deletions server/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
{
"name": "server",
"name": "serverless-data-machine",
"version": "1.0.0",
"bin": {
"index": "src/index.js"
},
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "tsc",
"bootstrap": "cdk bootstrap",
"deploy": "cdk deploy --all",
"dev": "sh -c './develop.sh'",
"destroy": "cdk destroy --all",
"lint": "eslint **/*.ts",
"lint-fix": "eslint **/*.ts --fix",
"synth": "cdk synth",
"test": "jest",
"watch": "tsc -w"
},
"keywords": [],
"author": "",
"license": "ISC"
"license": "ISC",
"dependencies": {
"aws-cdk-lib": "^2.147.2",
"aws-sdk": "^2.1651.0",
"constructs": "^10.3.0",
"source-map-support": "^0.5.21",
"uuid": "^10.0.0"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/uuid": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^7.15.0",
"@typescript-eslint/parser": "^7.15.0",
"aws-cdk": "^2.147.2",
"eslint": "^8.57.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.5",
"ts-node": "^10.9.2",
"typescript": "^5.5.2"
}
}
50 changes: 50 additions & 0 deletions server/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Construct } from "constructs";
import * as Apigw from 'aws-cdk-lib/aws-apigateway';
import { IRestApi } from "aws-cdk-lib/aws-apigateway";
import { IFunction } from 'aws-cdk-lib/aws-lambda';

export const createApiModels = (scope: Construct, api: IRestApi) => {
// For POST, PUT, DELETE Requests
const requestModel = new Apigw.Model(scope, 'RequestModel', {
restApi: api,
schema: {
type: Apigw.JsonSchemaType.OBJECT,
properties: {
name: { type: Apigw.JsonSchemaType.STRING },
age: { type: Apigw.JsonSchemaType.NUMBER }
},
required: ['name']
}
});

const responseModel = new Apigw.Model(scope, 'ResponseModel', {
restApi: api,
schema: {
type: Apigw.JsonSchemaType.OBJECT,
properties: {
message: { type: Apigw.JsonSchemaType.STRING },
status: { type: Apigw.JsonSchemaType.STRING }
}
}
});

return {
requestModel,
responseModel
};
};

export const linkBackendApi = (scope: Construct, api: IRestApi, handler: IFunction, responseModel: any) => {
// Create a new resource (API path)
const resource = api.root.addResource('start');

// Add a GET method to the resource with lambda integration to the handler function
resource.addMethod('GET', new Apigw.LambdaIntegration(handler), {
methodResponses: [{
statusCode: '200',
responseModels: {
'application/json': responseModel
}
}]
});
};
Loading

0 comments on commit 62e5068

Please sign in to comment.