Skip to content
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
11 changes: 11 additions & 0 deletions backend/.docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

cd /home/api

if [ ! -f ".env" ]; then
cp .env.example .env
fi

yarn install
yarn typeorm migration:run
yarn start:dev
12 changes: 12 additions & 0 deletions backend/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
8 changes: 8 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
API_PORT=3333

DB_TYPE=postgres
DB_HOST=database
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=docker
DB_DATABASE=placestovisit
25 changes: 25 additions & 0 deletions backend/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'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',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
36 changes: 36 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# compiled output
/dist
/node_modules

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS
.DS_Store

# Tests
/coverage
/.nyc_output

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

.env
4 changes: 4 additions & 0 deletions backend/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
13 changes: 13 additions & 0 deletions backend/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Nest Framework",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
"args": ["${workspaceFolder}/src/main.ts"],
"autoAttachChildProcesses": true
}
]
}
5 changes: 5 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM node:14.15.2-alpine3.12

WORKDIR /home/api

CMD yarn start:docker:dev
17 changes: 17 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Descrição

API Rest com Nest.js

## Rodar a aplicação

#### Crie os containers com Docker

```
$ docker-compose up
```

#### Accesse no browser

```
http://localhost:3333/places
```
25 changes: 25 additions & 0 deletions backend/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3.9"

services:

api:
container_name: nest_api
build: .
entrypoint: ./.docker/entrypoint.sh
environment:
- CHOKIDAR_USEPOLLING=true
ports:
- "3333:3333"
volumes:
- .:/home/api

database:
container_name: nest_db
image: postgres:13
restart: always
environment:
POSTGRES_DB: placestovisit
POSTGRES_USER: postgres
POSTGRES_PASSWORD: docker
ports:
- "5433:5432"
4 changes: 4 additions & 0 deletions backend/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"collection": "@nestjs/schematics",
"sourceRoot": "src"
}
23 changes: 23 additions & 0 deletions backend/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as path from 'path';
import { ConfigService } from './src/config/config.service';
import { Configuration } from './src/config/config.keys';

const config = new ConfigService();

export default {
type: config.get(Configuration.DB_TYPE),
host: config.get(Configuration.DB_HOST),
port: config.get(Configuration.DB_PORT),
username: config.get(Configuration.DB_USERNAME),
password: config.get(Configuration.DB_PASSWORD),
database: config.get(Configuration.DB_DATABASE),
entities: [
path.resolve(__dirname, 'src', 'modules', '**', '*.entity{.ts,.js}'),
],
migrations: [
path.resolve(__dirname, 'src', 'database', 'migrations', '*{.ts,.js}'),
],
cli: {
migrationsDir: './src/database/migrations',
},
};
79 changes: 79 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"name": "lugares-backend",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:ver": "jest --verbose",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"typeorm": "ts-node ./node_modules/typeorm/cli.js"
},
"dependencies": {
"@nestjs/common": "^7.5.1",
"@nestjs/core": "^7.5.1",
"@nestjs/platform-express": "^7.5.1",
"@nestjs/typeorm": "^7.1.5",
"class-transformer": "^0.3.2",
"class-validator": "^0.13.1",
"pg": "^8.5.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.3",
"typeorm": "^0.2.30"
},
"devDependencies": {
"@nestjs/cli": "^7.5.1",
"@nestjs/schematics": "^7.1.3",
"@nestjs/testing": "^7.5.1",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.8",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.6",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.6.1",
"@typescript-eslint/parser": "^4.6.1",
"dotenv": "^8.2.0",
"eslint": "^7.12.1",
"eslint-config-prettier": "7.2.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.6.3",
"prettier": "^2.1.2",
"supertest": "^6.0.0",
"ts-jest": "^26.4.3",
"ts-loader": "^8.0.8",
"ts-node": "^9.0.0",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.0.5"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
17 changes: 17 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Module } from '@nestjs/common';
import { Configuration } from './config/config.keys';
import { ConfigModule } from './config/config.module';
import { ConfigService } from './config/config.service';
import { DatabaseModule } from './database/database.module';
import { PlaceModule } from './modules/place/place.module';

@Module({
imports: [ConfigModule, DatabaseModule, PlaceModule],
})
export class AppModule {
static port: number | string;

constructor(private readonly _configService: ConfigService) {
AppModule.port = this._configService.get(Configuration.API_PORT);
}
}
12 changes: 12 additions & 0 deletions backend/src/common/test/TestUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Place } from '../../modules/place/place.entity';

export class TestUtil {
static giveMeAValidPlace(): Place {
const place = new Place();
place.country = 'Valid Country';
place.place = 'Valid place';
place.goal = 1641009600; // 2022-01-01T00:00:00
place.flag = 'https://www.valid.com/flag.svg';
return place;
}
}
10 changes: 10 additions & 0 deletions backend/src/config/config.keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export enum Configuration {
API_PORT = 'API_PORT',

DB_TYPE = 'DB_TYPE',
DB_PORT = 'DB_PORT',
DB_HOST = 'DB_HOST',
DB_USERNAME = 'DB_USERNAME',
DB_PASSWORD = 'DB_PASSWORD',
DB_DATABASE = 'DB_DATABASE',
}
13 changes: 13 additions & 0 deletions backend/src/config/config.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { ConfigService } from './config.service';

@Module({
providers: [
{
provide: ConfigService,
useValue: new ConfigService(),
},
],
exports: [ConfigService],
})
export class ConfigModule {}
31 changes: 31 additions & 0 deletions backend/src/config/config.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as path from 'path';
import * as fs from 'fs';
import { parse } from 'dotenv';

export class ConfigService {
private readonly envConfig: { [key: string]: string };

constructor() {
const isDevelopmentEnv = process.env.NODE_ENV !== 'production';

if (isDevelopmentEnv) {
const envFilePath = path.resolve('.env');
const existsPath = fs.existsSync(envFilePath);

if (!existsPath) {
console.log(envFilePath, '.env file does not exist');
process.exit(0);
}

this.envConfig = parse(fs.readFileSync(envFilePath));
} else {
this.envConfig = {
PORT: process.env.PORT,
};
}
}

get(key: string): string {
return this.envConfig[key];
}
}
8 changes: 8 additions & 0 deletions backend/src/database/database.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { databaseProviders } from './database.service';

@Module({
imports: [...databaseProviders],
exports: [...databaseProviders],
})
export class DatabaseModule {}
Loading