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

Task 9 #21

Open
wants to merge 20 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
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
./dist
./node_modules
.eslintignore
.eslintrcjs
.gitignore
.prettierc
.docker-compose.yaml
Dockerfile
env.example
get-token.sh
README.md
.env
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
6 changes: 3 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ lerna-debug.log*
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml

.env
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:20-alpine as build

WORKDIR /app

COPY package*.json /

RUN npm install

COPY . .

RUN npm run build

FROM node:20-alpine as production

COPY --from=build /app/dist ./dist

EXPOSE 4000

CMD ["node", "dist/main.js"]
113 changes: 58 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,78 @@
<p align="center">
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" /></a>
</p>

[travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
[travis-url]: https://travis-ci.org/nestjs/nest
[linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
[linux-url]: https://travis-ci.org/nestjs/nest

<p align="center">A progressive <a href="http://nodejs.org" target="blank">Node.js</a> framework for building efficient and scalable server-side applications, heavily inspired by <a href="https://angular.io" target="blank">Angular</a>.</p>
<p align="center">
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/dm/@nestjs/core.svg" alt="NPM Downloads" /></a>
<a href="https://travis-ci.org/nestjs/nest"><img src="https://api.travis-ci.org/nestjs/nest.svg?branch=master" alt="Travis" /></a>
<a href="https://travis-ci.org/nestjs/nest"><img src="https://img.shields.io/travis/nestjs/nest/master.svg?label=linux" alt="Linux" /></a>
<a href="https://coveralls.io/github/nestjs/nest?branch=master"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#5" alt="Coverage" /></a>
<a href="https://gitter.im/nestjs/nestjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge"><img src="https://badges.gitter.im/nestjs/nestjs.svg" alt="Gitter" /></a>
<a href="https://opencollective.com/nest#backer"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
<a href="https://opencollective.com/nest#sponsor"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
<a href="https://paypal.me/kamilmysliwiec"><img src="https://img.shields.io/badge/Donate-PayPal-dc3d53.svg"/></a>
<a href="https://twitter.com/nestframework"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
</p>
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
[![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->

## Description

[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.

## Installation
# nodejs-aws-cart-api

## installation
```bash
$ npm install
npm i && (cd cdk && npm i)
```

## Running the app
create .env file and put there vars from env.example file. Replace values as needed

apply migrations to the database
```bash
# development
$ npm run start

# watch mode
$ npm run start:dev
npm run knex:migrate:latest
```

# production mode
$ npm run start:prod
## build and deploy
```bash
npm run build && (cd cdk && cdk synth && cdk deploy)
```

## Test

To create user and get basic auth token via cli run
```bash
# unit tests
$ npm run test
./get-token.sh
```

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov
## Migrations
generate empty migration
```sh
npm run knex:migrate:make your-new-migration-name
```

## Support
run migrations
```sh
npm run knex:migrate:latest
```

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
generate empty seed migration
```sh
npm run knex:seed:make
```

## Stay in touch
run seed migrations
```sh
npm run knex:seed:run
```

- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
- Website - [https://nestjs.com](https://nestjs.com/)
- Twitter - [@nestframework](https://twitter.com/nestframework)
### Tests
Before running test run postgres in docker container
```bash
docker compose up -d
```
## Unit tests
```bash
npm run test
```

## License
## E2E test
```bash
npm run test:e2e
```

Nest is [MIT licensed](LICENSE).
### Test deploy locally with AWS SAM
1. Install AWS SAM
2. create .env file and put there vars from env.example file. Replace values as needed
3. install dependencies with command
```bash
npm i && (cd cdk && npm i)
```
4. Run database in docker
```bash
docker compose up -d
```
5. Apply migrations
```bash
npm run knex:migrate:latest
```
6.
45 changes: 45 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: "3"
services:
cart-api:
build:
context: .
container_name: cart-api
env_file:
- ./.env
networks:
- rss-aws-shop-backend_cdk-test-network
restart: always
ports:
- ${APP_PORT}:${APP_PORT}


dev-db:
image: postgres:15-alpine
container_name: dev-postgres
restart: always
ports:
- ${DEV_POSTGRES_PORT}:5432
# volumes:
# - ./sql:/docker-entrypoint-initdb.d
environment:
- POSTGRES_USER=${DEV_POSTGRES_USER}
- POSTGRES_PASSWORD=${DEV_POSTGRES_PASSWORD}
- POSTGRES_DB=${DEV_POSTGRES_DB_NAME}
networks:
- rss-aws-shop-backend_cdk-test-network

test-db:
image: postgres:15-alpine
container_name: test-postgres
restart: unless-stopped
ports:
- ${TEST_POSTGRES_PORT}:5432
environment:
- POSTGRES_USER=${TEST_POSTGRES_USER}
- POSTGRES_PASSWORD=${TEST_POSTGRES_PASSWORD}
- POSTGRES_DB=${TEST_POSTGRES_DB_NAME}

networks:
rss-aws-shop-backend_cdk-test-network:
external: true

39 changes: 39 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
NODE_ENV=development
APP_PORT=4000

POSTGRES_HOST=
POSTGRES_PORT=
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_DB_NAME=

DEV_POSTGRES_HOST=dev-db
DEV_POSTGRES_PORT=5432
DEV_POSTGRES_USER=test
DEV_POSTGRES_PASSWORD=KfTyF7prfroyQ/ILBQDNhNULt
DEV_POSTGRES_DB_NAME=devdb

TEST_POSTGRES_HOST=localhost
TEST_POSTGRES_PORT=5433
TEST_POSTGRES_USER=test
TEST_POSTGRES_PASSWORD=tYEvMiY70eClgCnKE/dotU5Zg
TEST_POSTGRES_DB_NAME=testdb

MIGRATION_POSTGRES_HOST=localhost
MIGRATION_POSTGRES_PORT=5432
MIGRATION_POSTGRES_USER=test
MIGRATION_POSTGRES_PASSWORD=KfTyF7prfroyQ/ILBQDNhNULt
MIGRATION_POSTGRES_DB_NAME=devdb

AWS_REGION=
DYNAMO_DB_URL=
DYNAMO_DB_ACCESS_KEY_ID=
DYNAMO_DB_SECRET_ACCESS_KEY=

TEST_AWS_REGION=us-east-1
TEST_DYNAMO_DB_URL=http://dynamodb:8000
TEST_DYNAMO_DB_ACCESS_KEY_ID=test
TEST_DYNAMO_DB_SECRET_ACCESS_KEY=test

GITHUB_ACCOUNT_LOGIN=ALEXANDERSUS
AUTH_PASSWORD=TEST_PASSWORD
21 changes: 21 additions & 0 deletions get-token.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

if [ ! -f .env ]; then
echo ".env file does not exist. Run command:"
echo "cat env.example > .env"
exit 1
fi

source .env

REGISTER_PAYLOAD="{\"name\":\"$GITHUB_ACCOUNT_LOGIN\",\"password\":\"$AUTH_PASSWORD\"}"
LOGIN_PAYLOAD="{\"username\":\"$GITHUB_ACCOUNT_LOGIN\",\"password\":\"$AUTH_PASSWORD\"}"

curl -s --header "Content-Type: application/json" \
--request POST \
--data "$REGISTER_PAYLOAD" "http://localhost:$APP_PORT/api/auth/register" > /dev/null


curl -s --header "Content-Type: application/json" \
--request POST \
--data "$LOGIN_PAYLOAD" "http://localhost:$APP_PORT/api/auth/login"
23 changes: 23 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { pathsToModuleNameMapper, JestConfigWithTsJest } from 'ts-jest';

const jestConfig: JestConfigWithTsJest = {
moduleFileExtensions: ['js', 'json', 'ts'],
rootDir: 'src',
testRegex: '.spec.ts$',
transform: {
'^.+\\.(t|j)s$': 'ts-jest',
},
coverageDirectory: '../coverage',
testEnvironment: 'node',
setupFiles: ['<rootDir>/../test/jest-env-vars.ts'],
moduleNameMapper: pathsToModuleNameMapper(
{
'src/*': ['../src/*'],
},
{
prefix: '<rootDir>',
},
),
};

export default jestConfig;
Loading