Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
AleDore committed Mar 22, 2024
2 parents dada0f2 + b1ad899 commit 2db2ac0
Show file tree
Hide file tree
Showing 7 changed files with 540 additions and 11 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:

cdc-ms:
container_name: cdc-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:
- cdc-be

volumes:
certs:
driver: local

networks:
cdc-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 @@ -30,6 +30,9 @@
"@pagopa/winston-ts": "^2.2.0",
"@pagopa/ts-commons": "^12.4.1",
"@pagopa/data-indexer-commons": "^0.0.6",
"body-parser": "^1.20.2",
"dotenv": "^8.2.0",
"express": "^4.18.2",
"fp-ts": "^2.16.1",
"io-ts": "^2.2.21",
"io-ts-types": "^0.5.19",
Expand All @@ -38,6 +41,8 @@
"devDependencies": {
"@pagopa/eslint-config": "^3.0.0",
"@types/jest": "^29.5.11",
"@types/express": "4.17.20",
"@types/body-parser": "^1.19.4",
"eslint-plugin-prettier": "^5.0.1",
"jest": "^29.7.0",
"prettier": "^3.1.1",
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}`,
);
});
};
11 changes: 4 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import { defaultLog, useWinston, withConsole } from "@pagopa/winston-ts";
import { createApp } from "./app";

useWinston(withConsole());

const main = () => defaultLog.taskEither.info("Initializing App");

void main();
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)}`);
}),
);
10 changes: 10 additions & 0 deletions src/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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 2db2ac0

Please sign in to comment.