Skip to content

Commit

Permalink
Call auf OSRM API
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisSt committed May 12, 2023
1 parent 63fee53 commit 3fdd659
Show file tree
Hide file tree
Showing 13 changed files with 563 additions and 18 deletions.
117 changes: 107 additions & 10 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions backend/package.json
Expand Up @@ -20,9 +20,13 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/axios": "^2.0.0",
"@nestjs/common": "^9.0.0",
"@nestjs/swagger": "^6.3.0",
"@nestjs/core": "^9.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^9.0.0",
"axios": "^1.4.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0"
},
Expand Down
4 changes: 3 additions & 1 deletion backend/src/app.module.ts
@@ -1,9 +1,11 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { RoutesModule } from './routes/routes.module';
import {HttpModule} from '@nestjs/axios'

@Module({
imports: [],
imports: [RoutesModule, HttpModule],
controllers: [AppController],
providers: [AppService],
})
Expand Down
3 changes: 3 additions & 0 deletions backend/src/environment.ts
@@ -0,0 +1,3 @@
export const environment = {
osrm_url:'http://router.project-osrm.org'
}
10 changes: 10 additions & 0 deletions backend/src/main.ts
@@ -1,8 +1,18 @@
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);

const config = new DocumentBuilder()
.setTitle('Individuelle Berner Stadtspaziergänge')
.setDescription('API Routenfinder')
.setVersion('1.0')
.addTag('spaziergaenge')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
await app.listen(3000);
}
bootstrap();
3 changes: 3 additions & 0 deletions backend/src/routes/dto/route.dto.ts
@@ -0,0 +1,3 @@
export class RouteDto{

}
20 changes: 20 additions & 0 deletions backend/src/routes/routes.controller.spec.ts
@@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { RoutesController } from './routes.controller';
import { RoutesService } from './routes.service';

describe('RoutesController', () => {
let controller: RoutesController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [RoutesController],
providers: [RoutesService],
}).compile();

controller = module.get<RoutesController>(RoutesController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
33 changes: 33 additions & 0 deletions backend/src/routes/routes.controller.ts
@@ -0,0 +1,33 @@
import { Controller, Get, Query, HttpStatus } from '@nestjs/common';
import { RoutesService } from './routes.service';
import { ApiParam } from '@nestjs/swagger';

export interface RouteQueryParams {
coordinates: string,
mode: string
}

@Controller('routes')
export class RoutesController {

constructor(private readonly routesService: RoutesService) {}

@Get('health')
health(){
return HttpStatus.OK;
}

@Get()
@ApiParam({
name: 'routeQueryParams',
description: 'coordinates, mode',
})
findRoute(@Query() routeQueryParams: RouteQueryParams) {
console.log(routeQueryParams)
try {
return this.routesService.getRoute(routeQueryParams.coordinates, routeQueryParams.mode);
} catch(error){
throw new Error(error);
}
}
}
11 changes: 11 additions & 0 deletions backend/src/routes/routes.module.ts
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { RoutesService } from './routes.service';
import { RoutesController } from './routes.controller';
import {HttpModule} from '@nestjs/axios'

@Module({
imports: [RoutesModule, HttpModule],
controllers: [RoutesController],
providers: [RoutesService]
})
export class RoutesModule {}
18 changes: 18 additions & 0 deletions backend/src/routes/routes.service.spec.ts
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { RoutesService } from './routes.service';

describe('RoutesService', () => {
let service: RoutesService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [RoutesService],
}).compile();

service = module.get<RoutesService>(RoutesService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
21 changes: 21 additions & 0 deletions backend/src/routes/routes.service.ts
@@ -0,0 +1,21 @@
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { firstValueFrom, map } from 'rxjs';
import { environment } from 'src/environment';

@Injectable()
export class RoutesService {
osrm_url = environment.osrm_url;

constructor(private httpService: HttpService){}

async getRoute(coordinates: any, mode: any) {
console.log('params: ' + coordinates + ' mode ' + mode)
console.log(`${this.osrm_url}/route/v1/${mode}/${coordinates}?geometries=geojson`)
const routes$ = this.httpService.get(`${this.osrm_url}/route/v1/${mode}/${coordinates}?geometries=geojson`);

return this.httpService.get(`${this.osrm_url}/route/v1/${mode}/${coordinates}?geometries=geojson`).pipe(map((response) => {
return response.data;
}))
}
}

0 comments on commit 3fdd659

Please sign in to comment.