Skip to content

Commit

Permalink
add docker configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
jprusik committed Nov 10, 2023
1 parent 26a4ff2 commit ec97d3b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 15 deletions.
19 changes: 19 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ignore everything
*

# except:
!**package*.json

!api/.env
!api/app.ts
!api/constants.ts
!api/ssl.crt
!api/ssl.key
!api/tsconfig.json

!client/babel.config.js
!client/docs
!client/docusaurus.config.ts
!client/sidebars.js
!client/src
!client/static
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ARG NODE_VERSION=18

FROM node:${NODE_VERSION}-alpine as build
WORKDIR /usr/app
COPY . .
RUN npm ci
RUN npm run build

FROM node:${NODE_VERSION}-alpine
WORKDIR /usr/app
COPY --from=build /usr/app/client/build ./client/build
COPY --from=build /usr/app/api/build ./api/build
COPY --from=build /usr/app/api/node_modules ./api/node_modules
COPY --from=build /usr/app/api/.env ./.env
CMD ["node", "api/build/app.js"]
18 changes: 18 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ARG NODE_VERSION=18

FROM node:${NODE_VERSION}-alpine as build
WORKDIR /usr/app
COPY . .
RUN npm ci
RUN npm run build

FROM node:${NODE_VERSION}-alpine
WORKDIR /usr/app
COPY --from=build /usr/app/client/build ./client/build
COPY --from=build /usr/app/api/build ./api/build
COPY --from=build /usr/app/api/node_modules ./api/node_modules
COPY --from=build /usr/app/api/ssl.crt ./api/ssl.crt
COPY --from=build /usr/app/api/ssl.key ./api/ssl.key
COPY --from=build /usr/app/api/.env ./.env
CMD ["node", "api/build/app.js"]
EXPOSE 443
38 changes: 23 additions & 15 deletions api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ const express = require("express");
import { Request, Response, NextFunction } from "express-serve-static-core";
import { DEFAULT_COOKIE_SETTINGS, QUERY_PARAMS, ROUTES } from "./constants";

const port = process.env.SERVE_PORT || 443;
const insecurePort = process.env.SERVE_INSECURE_PORT || 80;
const sslCertFileName = process.env.SSL_CERT;
const sslKeyFileName = process.env.SSL_KEY;
const staticFilesPath = process.env.STATIC_FILES_DIR || "client/build";

const app = express();

app.use(express.static(process.env.STATIC_FILES_DIR));
if (staticFilesPath) {
app.use(express.static(`${__dirname}/../../${process.env.STATIC_FILES_DIR}`));
} else {
console.warn("The static files folder could not be found.");
}

// for handling form content-types
app.use(express.urlencoded({ extended: false }));
Expand All @@ -17,17 +27,16 @@ app.use(helmet());

app.disable("x-powered-by");

const port = process.env.SERVE_PORT;

// error handler
app.use(
(error: Error, request: Request, response: Response, next: NextFunction) => {
console.error(error.stack);
response.status(500).send("Something broke!");
}
},
);

function handlePost(request: Request, response: Response, route: string) {
console.log("POST received for:", route);
const referrerURL = request.get("Referrer") || "";
let referrerQueryParams = "";

Expand Down Expand Up @@ -55,27 +64,24 @@ function handlePost(request: Request, response: Response, route: string) {
app
.route(ROUTES.LOGIN)
.post((request: Request, response: Response) =>
handlePost(request, response, ROUTES.LOGIN)
handlePost(request, response, ROUTES.LOGIN),
);

app
.route(ROUTES.PAYMENT)
.post((request: Request, response: Response) =>
handlePost(request, response, ROUTES.PAYMENT)
handlePost(request, response, ROUTES.PAYMENT),
);

app
.route(ROUTES.IDENTITY)
.post((request: Request, response: Response) =>
handlePost(request, response, ROUTES.IDENTITY)
handlePost(request, response, ROUTES.IDENTITY),
);

try {
const cert = fs.readFileSync(
`${__dirname}/../${process.env.SSL_CERT}`,
"utf8"
);
const key = fs.readFileSync(`${__dirname}/../${process.env.SSL_KEY}`, "utf8");
const cert = fs.readFileSync(`${__dirname}/../${sslCertFileName}`, "utf8");
const key = fs.readFileSync(`${__dirname}/../${sslKeyFileName}`, "utf8");

const https = require("https");
const credentials = { key, cert };
Expand All @@ -85,7 +91,9 @@ try {
console.log(`SSL-enabled app server listening on port ${port}`);
});
} catch {
app.listen(port, () => {
console.log(`app server listening on port ${port}`);
});
console.log("local certs were not found");
}

app.listen(insecurePort, () => {
console.log(`app server listening on (insecure) port ${insecurePort}`);
});

0 comments on commit ec97d3b

Please sign in to comment.