Skip to content

Commit

Permalink
Smoke test
Browse files Browse the repository at this point in the history
  • Loading branch information
xdan committed Aug 24, 2023
1 parent 488d9b7 commit c7a7869
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 50 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dist
compose
.nvmrc
.eslintrc.json
.eslintignore
.prettierrc.json
docker-compose.yml
.editorconfig
Expand Down
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
23 changes: 0 additions & 23 deletions Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"verbose": true,
"ignore": [".git", "node_modules", "dist"],
"watch": ["src"],
"exec": "make dev",
"exec": "npm run dev",
"ext": "ts"
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "tsc -b tsconfig.json",
"dev": "ts-node ./src/index.ts",
"start": "nodemon",
"lint": "eslint ./ && tsc --noemit --noErrorTruncation",
"test": "jest"
},
"repository": {
"type": "git",
Expand Down
45 changes: 24 additions & 21 deletions src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,32 @@ import * as Boom from '@hapi/boom';
import {logger} from './lib/logger';
import {asyncMiddleware} from './lib/async-middlware';
import {loadByBBox} from './middleware/load-by-bbox';
import {DbDataProvider} from './data-provider/db-data-provider/db-data-provider';
import {loadByTile} from './middleware/load-by-tile';
import {DataProvider} from "./data-provider/interface";

const dataProvider = new DbDataProvider();
export const app = express()
.disable('x-powered-by')
.disable('etag')
.use(express.json())
.get('/ping', asyncMiddleware(pingMiddleware.bind(null, dataProvider)))
.post('/v1/bbox', asyncMiddleware(loadByBBox.bind(null, dataProvider)))
.post('/v1/tile', asyncMiddleware(loadByTile.bind(null, dataProvider)))
.use((req: express.Request, res: express.Response, next: express.NextFunction) =>
next(Boom.notFound('Endpoint not found'))
)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.use((err: Error, req: express.Request, res: express.Response, next: unknown) => {
if (Boom.isBoom(err)) {
sendError(res, err);
} else {
logger.error('app', err.stack || err);
sendError(res, Boom.internal());
}
});
export function createApp(dataProvider: DataProvider) {
return (
express()
.disable('x-powered-by')
.disable('etag')
.use(express.json())
.get('/ping', asyncMiddleware(pingMiddleware.bind(null, dataProvider)))
.post('/v1/bbox', asyncMiddleware(loadByBBox.bind(null, dataProvider)))
.post('/v1/tile', asyncMiddleware(loadByTile.bind(null, dataProvider)))
.use((req: express.Request, res: express.Response, next: express.NextFunction) =>
next(Boom.notFound('Endpoint not found'))
)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
.use((err: Error, req: express.Request, res: express.Response, next: unknown) => {
if (Boom.isBoom(err)) {
sendError(res, err);
} else {
logger.error('app', err.stack || err);
sendError(res, Boom.internal());
}
})
);
}

function sendError(res: express.Response, err: Boom.Boom): void {
res.status(err.output.statusCode).json(err.output.payload);
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {app} from "./app/app";
import {createApp} from "./app/app";
import {logger} from "./app/lib/logger";
import dotenv from 'dotenv';
import {DbDataProvider} from "./app/data-provider/db-data-provider/db-data-provider";

dotenv.config();
const dataProvider = new DbDataProvider();

const port = process.env.MAPS_NODEJS_PORT || 8080;
app.listen(port, () => {
createApp(dataProvider).listen(port, () => {
logger.info('', `App has started at http://localhost:${port}`);
});
14 changes: 12 additions & 2 deletions src/tests/v1/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as nock from 'nock';
import {TestServer} from '../test-server';
import {app} from '../../app/app';
import {createApp} from '../../app/app';
import {Feature} from "../../app/lib/geo";

describe('/v2', () => {
let testServer: TestServer;
Expand All @@ -9,7 +10,16 @@ describe('/v2', () => {
nock.disableNetConnect();
nock.enableNetConnect(/(127.0.0.1|localhost)/);

testServer = await TestServer.start(app);
testServer = await TestServer.start(
createApp({
getFeaturesByBBox(): Promise<Feature[]> {
return Promise.resolve([]);
},
isReady(): Promise<void> {
return Promise.resolve();
}
})
);
});

afterAll(async () => {
Expand Down

0 comments on commit c7a7869

Please sign in to comment.