Skip to content

Commit

Permalink
[#IOPLT-386] Refactor app as HTTP server
Browse files Browse the repository at this point in the history
  • Loading branch information
AleDore committed Mar 19, 2024
1 parent 5aea729 commit e4bf1af
Show file tree
Hide file tree
Showing 7 changed files with 522 additions and 7 deletions.
31 changes: 31 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
version: "2.2"

services:

data-ti-ms:
container_name: data-ti-ms
restart: always
build:
context: ./
dockerfile: Dockerfile
environment:
- NODE_TLS_REJECT_UNAUTHORIZED=0
expose:
- "3000"
- "80"
ports:
- "3000:3000"
- "80:80"
command: ["node", "dist/index.js"]
networks:
- data-be

volumes:
certs:
driver: local

networks:
data-be:
driver: bridge
driver_opts:
com.docker.network.driver.mtu: 1450
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"@pagopa/eslint-config": "^1.3.1",
"@pagopa/openapi-codegen-ts": "^11.1.0",
"@types/jest": "^29.5.10",
"@types/express": "4.17.20",
"@types/body-parser": "^1.19.4",
"danger": "^10.3.0",
"dependency-check": "^4.1.0",
"eslint-plugin-prettier": "^3.3.1",
Expand All @@ -43,6 +45,9 @@
"@pagopa/fp-ts-kafkajs": "^1.0.1",
"@pagopa/ts-commons": "^10.7.1",
"@pagopa/winston-ts": "^2.2.0",
"body-parser": "^1.20.2",
"dotenv": "^8.2.0",
"express": "^4.18.2",
"fp-ts": "^2.10.5",
"io-ts": "^2.2.21",
"kafkajs": "^2.2.4",
Expand Down
33 changes: 33 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable no-console */
import express from "express";
import * as bodyParser from "body-parser";
import { getConfigOrThrow } from "./utils/configReader";

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const createApp = async () => {
const config = getConfigOrThrow();
const app = express();
// Parse the incoming request body. This is needed by Passport spid strategy.
app.use(
bodyParser.json({
verify: (_req, res: express.Response, buf, _encoding: BufferEncoding) => {
// eslint-disable-next-line functional/immutable-data
res.locals.body = buf;
}
})
);

// Parse an urlencoded body.
app.use(bodyParser.urlencoded({ extended: true }));

app.get("/info", (_: express.Request, res) =>
res.status(200).json({ status: "OK" })
);

app.listen(config.SERVER_PORT, () => {
// eslint-disable-next-line no-console
console.log(
`Data Transformer and Indexer app listening on port ${config.SERVER_PORT}`
);
});
};
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createApp } from "./app";

createApp()
.then(console.log)
.catch(console.error);
44 changes: 44 additions & 0 deletions src/utils/configReader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as t from "io-ts";

import * as E from "fp-ts/lib/Either";
import { pipe } from "fp-ts/lib/function";
import { NonNegativeIntegerFromString } from "@pagopa/ts-commons/lib/numbers";
import { readableReport } from "./logging";

const DEFAULT_SERVER_PORT = "80";
// global app configuration
export type IConfig = t.TypeOf<typeof IConfig>;
export const IConfig = t.type({
SERVER_PORT: NonNegativeIntegerFromString,
isProduction: t.boolean
});

// No need to re-evaluate this object for each call
const errorOrConfig: t.Validation<IConfig> = IConfig.decode({
...process.env,
SERVER_PORT: process.env.PORT || DEFAULT_SERVER_PORT,
isProduction: process.env.NODE_ENV === "production"
});

/**
* Read the application configuration and check for invalid values.
* Configuration is eagerly evalued when the application starts.
*
* @returns either the configuration values or a list of validation errors
*/
export const getConfig = (): t.Validation<IConfig> => errorOrConfig;

/**
* Read the application configuration and check for invalid values.
* If the application is not valid, raises an exception.
*
* @returns the configuration values
* @throws validation errors found while parsing the application configuration
*/
export const getConfigOrThrow = (): IConfig =>
pipe(
errorOrConfig,
E.getOrElse(errors => {
throw new Error(`Invalid configuration: ${readableReport(errors)}`);
})
);
8 changes: 8 additions & 0 deletions src/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as t from "io-ts";
export const readableReport = (errors: t.Errors): string =>
errors
.map(
e =>
`Context=${JSON.stringify(e.context)}|Value=${e.value}|Msg=${e.message}`
)
.join("\n");
Loading

0 comments on commit e4bf1af

Please sign in to comment.