From c7a786987cc42acda114ba2dfe2cbd00b03af8d5 Mon Sep 17 00:00:00 2001 From: xdan Date: Sun, 20 Aug 2023 00:58:03 +0300 Subject: [PATCH] Smoke test --- .dockerignore | 1 + .eslintignore | 1 + Makefile | 23 -------------------- nodemon.json | 2 +- package.json | 6 +++++- src/app/app.ts | 45 +++++++++++++++++++++------------------- src/index.ts | 6 ++++-- src/tests/v1/api.test.ts | 14 +++++++++++-- 8 files changed, 48 insertions(+), 50 deletions(-) create mode 100644 .eslintignore delete mode 100644 Makefile diff --git a/.dockerignore b/.dockerignore index 6c907ca..ff7d33e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,6 +8,7 @@ dist compose .nvmrc .eslintrc.json +.eslintignore .prettierrc.json docker-compose.yml .editorconfig diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..1521c8b --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +dist diff --git a/Makefile b/Makefile deleted file mode 100644 index 32bab31..0000000 --- a/Makefile +++ /dev/null @@ -1,23 +0,0 @@ -OUT_DIR := out - -BIN := $$(npm bin) - -DB_PORT ?= 5445 -PORT ?= 8080 - -.PHONY: build -build: - @echo 'Building project...' - @$(BIN)/tsc -b tsconfig.json - -.PHONY: dev -dev: - @$(BIN)/ts-node ./src/index.ts - -.PHONY: start -start: - $(BIN)/nodemon - -.PHONY: lint -lint: - $(BIN)/eslint ./src diff --git a/nodemon.json b/nodemon.json index aafe812..0f9b057 100644 --- a/nodemon.json +++ b/nodemon.json @@ -2,6 +2,6 @@ "verbose": true, "ignore": [".git", "node_modules", "dist"], "watch": ["src"], - "exec": "make dev", + "exec": "npm run dev", "ext": "ts" } diff --git a/package.json b/package.json index d44e2a0..f215121 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/app/app.ts b/src/app/app.ts index b298bc1..8c09b57 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -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); diff --git a/src/index.ts b/src/index.ts index f181aac..9c2e06e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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}`); }); diff --git a/src/tests/v1/api.test.ts b/src/tests/v1/api.test.ts index a8085cf..7ea613d 100644 --- a/src/tests/v1/api.test.ts +++ b/src/tests/v1/api.test.ts @@ -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; @@ -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 { + return Promise.resolve([]); + }, + isReady(): Promise { + return Promise.resolve(); + } + }) + ); }); afterAll(async () => {