Skip to content

Commit

Permalink
feat: add mock server example
Browse files Browse the repository at this point in the history
  • Loading branch information
pimenovoleg authored Sep 6, 2019
2 parents 3877b28 + 5ba607f commit 56a4663
Show file tree
Hide file tree
Showing 54 changed files with 2,099 additions and 64 deletions.
82 changes: 81 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,87 @@
}
}
}
}
},
"mc-mocks-server": {
"root": "apps/mc-mocks-server",
"sourceRoot": "apps/mc-mocks-server/src",
"projectType": "application",
"prefix": "mc-mocks-server",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/apps/mc-mocks-server",
"main": "apps/mc-mocks-server/src/main.ts",
"tsConfig": "apps/mc-mocks-server/tsconfig.app.json",
"aot": false
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "apps/mc-mocks-server/src/environments/environment.ts",
"with": "apps/mc-mocks-server/src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"buildTarget": "mc-mocks-server:build:production"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"apps/mc-mocks-server/tsconfig.app.json",
"apps/mc-mocks-server/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"mc-mocks-users": {
"root": "libs/mocks/features/users",
"sourceRoot": "libs/mocks/features/users/src",
"projectType": "library",
"prefix": "mc-mocks",
"architect": {
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"libs/mocks/features/users/tsconfig.lib.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
},
"defaultProject": "mc-web"
}
5 changes: 5 additions & 0 deletions apps/mc-mocks-server/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"language": "ts",
"collection": "@nestjs/schematics",
"sourceRoot": "src"
}
6 changes: 6 additions & 0 deletions apps/mc-mocks-server/nodemon-debug.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"watch": ["apps/mc-mock-server/src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "node --inspect-brk -r tsconfig-paths/register apps/mc-mocks-server/src/main.ts"
}
5 changes: 5 additions & 0 deletions apps/mc-mocks-server/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"watch": ["dist"],
"ext": "js",
"exec": "node dist/main"
}
18 changes: 18 additions & 0 deletions apps/mc-mocks-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@mc/mocks-server",
"version": "0.0.0-PLACEHOLDER",
"description": "MC Mocks Server",
"keywords": [
"angular",
"nest",
"ptsecurity"
],
"peerDependencies": {
"@nestjs/common": "^6.0.0",
"@nestjs/core": "^6.0.0",
"@nestjs/platform-express": "^6.0.0",
"@nestjs/typeorm": "^6.1.3",
"sqlite": "^3.0.3"
},
"sideEffects": false
}
11 changes: 11 additions & 0 deletions apps/mc-mocks-server/src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const environment = {
production: true,

server: {
host: process.env.HOST || '0.0.0.0',
domainUrl: process.env.DOMAIN_URL || 'http://localhost:4545',
port: process.env.PORT || 4545, // tslint:disable-line
globalPrefix: '/api'
}

};
23 changes: 23 additions & 0 deletions apps/mc-mocks-server/src/environments/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This file can be replaced during build by using the `fileReplacements` array.
// `ng build --prod` replaces `environment.ts` with `environment.prod.ts`.
// The list of file replacements can be found in `angular.json`.

export const environment = {
production: false,

server: {
host: 'localhost',
domainUrl: 'http://localhost:4545',
port: 4545,
globalPrefix: '/api'
}
};

/*
* For easier debugging in development mode, you can import the following file
* to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`.
*
* This import should be commented out in production mode because it will have a negative impact
* on performance if an error is thrown.
*/
// import 'zone.js/dist/zone-error'; // Included with Angular CLI.
17 changes: 17 additions & 0 deletions apps/mc-mocks-server/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';

import { AppModule } from './modules/app/app.module';


async function bootstrap() {
const app = await NestFactory.create(AppModule, {cors: true, logger: true});
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
const port = process.env.port || 4545;
await app.listen(port, () => {
Logger.log(`Listening at http://localhost: ${port}/${globalPrefix}`);
});
}

bootstrap();
22 changes: 22 additions & 0 deletions apps/mc-mocks-server/src/modules/app/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';

describe('AppController', () => {
let appController: AppController;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService]
}).compile();

appController = app.get<AppController>(AppController);
});

describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});
13 changes: 13 additions & 0 deletions apps/mc-mocks-server/src/modules/app/app.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';


@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello(): string {
return this.appService.getHello();
}
}
25 changes: 25 additions & 0 deletions apps/mc-mocks-server/src/modules/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { UsersModule } from '@libs/mocks/users';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { VulnerabilitiesModule } from '../vulnerabilities/vulnerabilities.module';


@Module({
imports: [
TypeOrmModule.forRoot({
type: 'sqlite',
database: 'tmp/db.sql',
entities: [`${__dirname}/**/*.entity{.ts,.js}`, `**/*.entity{.ts, .js}`],
logging: true,
synchronize: true
}),
UsersModule,
VulnerabilitiesModule
],
controllers: [AppController],
providers: [AppService]
})
export class AppModule {}
8 changes: 8 additions & 0 deletions apps/mc-mocks-server/src/modules/app/app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Post, Put } from '@nestjs/common';
import { VulnerabilitiesService } from './vulnerabilities.service';
import { VulnerabilityDto } from './vulnerability.dto';


@Controller('vulnerabilities')
export class VulnerabilitiesController {
constructor(private vulnerabilitiesService: VulnerabilitiesService) {}

@Get()
@HttpCode(HttpStatus.OK)
findAll() {
return this.vulnerabilitiesService.getList();
}

@Get(':id')
@HttpCode(HttpStatus.PARTIAL_CONTENT)
find(@Param()id: string) {
return this.vulnerabilitiesService.getOne(id);
}

@Post()
@HttpCode(HttpStatus.CREATED)
create(@Body() vulnerabilityDto: Partial<VulnerabilityDto>) {
return this.vulnerabilitiesService.create(vulnerabilityDto);
}

@Put(':id')
@HttpCode(HttpStatus.ACCEPTED)
update(@Param()id: string, @Body()item) {
return this.vulnerabilitiesService.update(id, item);
}

@Delete(':id')
@HttpCode(HttpStatus.NO_CONTENT)
remove(@Param()id: string) {
return this.vulnerabilitiesService.remove(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { VulnerabilitiesController } from './vulnerabilities.controller';
import { VulnerabilitiesService } from './vulnerabilities.service';
import { Vulnerability } from './vulnerability.entity';


@Module({
imports: [TypeOrmModule.forFeature([Vulnerability])],
controllers: [VulnerabilitiesController],
providers: [VulnerabilitiesService]
})
export class VulnerabilitiesModule {
constructor(private vulnerabilitiesService: VulnerabilitiesService) {
this.vulnerabilitiesService.count().then((res: number) => {
if (res === 0) {
this.vulnerabilitiesService.generateInitial();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import * as faker from 'faker';
import { Repository } from 'typeorm';

import { Vulnerability } from './vulnerability.entity';


@Injectable()
export class VulnerabilitiesService {
constructor(@InjectRepository(Vulnerability) private readonly repository: Repository<Vulnerability>) {}

async getList(): Promise<Vulnerability[] | []> {
return this.repository.find();
}

async getOne(id: string): Promise<Vulnerability | null> {
return this.repository.findOneOrFail(id);
}

async create(vulnerability: Partial<Vulnerability>) {
return this.repository.save(vulnerability);
}

async update(id: string, item: Partial<Vulnerability>) {
return this.repository.update({id}, item);
}

async remove(id: string) {
return this.repository.delete({id});
}

async count(): Promise<number> {
return this.repository.count();
}

generateInitial() {
const data = [];

for (let i = 0; i < 15; i += 1) {
data.push({
name: faker.hacker.abbreviation(),
description: faker.hacker.phrase(),
records: [{
name: faker.hacker.noun(),
description: faker.hacker.adjective()
}]
});
}

return this.repository.save(data);
}
}
Loading

0 comments on commit 56a4663

Please sign in to comment.