-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from pagopa/IOPLT-386_refactor_for_info_endpoint
[#IOPLT-386] Refactor CDC app as HTTP server
- Loading branch information
Showing
7 changed files
with
540 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`, | ||
); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`); | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
Oops, something went wrong.