From ec254512ef4a1159861a3b0c1624d87d2ed5a24a Mon Sep 17 00:00:00 2001 From: andriibiloussigma Date: Mon, 18 Nov 2024 10:27:49 +0100 Subject: [PATCH] fixes docker-compose --- client/Dockerfile | 11 +++--- client/infra/nginx/conf/default.conf | 18 +++++++++ client/src/components/RequireAuth.js | 2 +- client/src/pages/Home.js | 2 +- client/src/pages/Login.js | 2 +- client/src/pages/Passport.js | 2 +- .../SmallBusinessLoan/SmallBusinessLoan.js | 2 +- .../pages/SmallBusinessLoan/SubmittedLoan.js | 4 +- .../TrafficTicket/SubmittedTrafficTicket.js | 2 +- .../src/pages/TrafficTicket/TrafficTicket.js | 2 +- .../pages/TrafficTicket/WitnessStatement.js | 2 +- docker-compose.yml | 39 ++++++------------- server/Dockerfile | 2 +- server/controllers/jwtController.js | 11 +++--- server/controllers/loanController.js | 3 +- server/controllers/trafficController.js | 9 +++-- server/server.js | 27 ++++++------- 17 files changed, 72 insertions(+), 68 deletions(-) create mode 100644 client/infra/nginx/conf/default.conf diff --git a/client/Dockerfile b/client/Dockerfile index 1966ddd..94f502b 100644 --- a/client/Dockerfile +++ b/client/Dockerfile @@ -1,5 +1,5 @@ # Use a Node.js base image for building -FROM node:20.14 AS builder +FROM --platform=linux/amd64 node:20.14 AS builder # Set the working directory WORKDIR /app @@ -21,8 +21,7 @@ ENV REACT_APP_API_URL=$REACT_APP_API_URL RUN npm run build # Use Nginx to serve the frontend build -FROM nginx:1.23.2-alpine -COPY --from=builder /app/build /usr/share/nginx/html - -# Expose necessary ports -EXPOSE 80 +FROM --platform=linux/amd64 nginx:1.23.2-alpine +COPY --from=builder /app/build /var/www/app +COPY ./infra/nginx/conf/ /etc/nginx/conf.d +EXPOSE 80 443 diff --git a/client/infra/nginx/conf/default.conf b/client/infra/nginx/conf/default.conf new file mode 100644 index 0000000..c2600f3 --- /dev/null +++ b/client/infra/nginx/conf/default.conf @@ -0,0 +1,18 @@ +server { + listen 80; + server_tokens off; + + root /var/www/app; + + location / { + try_files $uri /index.html; + } + + location /api/ { + rewrite ^/api/(.*)$ /$1 break; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Real-Port $remote_port; + proxy_set_header Host $host; + proxy_pass http://backend:5000; + } +} \ No newline at end of file diff --git a/client/src/components/RequireAuth.js b/client/src/components/RequireAuth.js index b4f30ff..ffa20d8 100644 --- a/client/src/components/RequireAuth.js +++ b/client/src/components/RequireAuth.js @@ -19,7 +19,7 @@ function RequireAuth() { async function isLoggedIn() { try { - let response = await axios.get(`${process.env.REACT_APP_API_URL}/api/auth/isLoggedIn`); + let response = await axios.get(`${process.env.REACT_APP_API_URL}/auth/isLoggedIn`); // Only set states if the component is mounted, otherwise return null. if (!mountedRef.current) return null; diff --git a/client/src/pages/Home.js b/client/src/pages/Home.js index da27bf6..b5cbe8a 100644 --- a/client/src/pages/Home.js +++ b/client/src/pages/Home.js @@ -17,7 +17,7 @@ function Home({ text, footerText }) { // stored for making Docusign API calls. async function getUserInfo() { try { - let response = await axios.get(`${process.env.REACT_APP_API_URL}/api/auth/login`); + let response = await axios.get(`${process.env.REACT_APP_API_URL}/auth/login`); // If the user revoked consent after logging in, check to make // sure they still have consent diff --git a/client/src/pages/Login.js b/client/src/pages/Login.js index 48bfce0..7cfcf3f 100644 --- a/client/src/pages/Login.js +++ b/client/src/pages/Login.js @@ -15,7 +15,7 @@ function Login({ text, githubText, btsText }) { async function handleLogin() { try { setSubmitted(true); - let response = await axios.get(`${process.env.REACT_APP_API_URL}/api/auth/login`); + let response = await axios.get(`${process.env.REACT_APP_API_URL}/auth/login`); // If user has never logged in before, redirect to consent screen if (response.status === 210) { diff --git a/client/src/pages/Passport.js b/client/src/pages/Passport.js index c2dc9ac..0b4a78e 100644 --- a/client/src/pages/Passport.js +++ b/client/src/pages/Passport.js @@ -22,7 +22,7 @@ function Passport({ text, formText, btsText }) { // Send request to server try { - const response = await sendRequest(`${process.env.REACT_APP_API_URL}/api/passportApplication`, body); + const response = await sendRequest(`${process.env.REACT_APP_API_URL}/passportApplication`, body); console.log(response.data); // Redirect to success screen diff --git a/client/src/pages/SmallBusinessLoan/SmallBusinessLoan.js b/client/src/pages/SmallBusinessLoan/SmallBusinessLoan.js index 5334c22..0479702 100644 --- a/client/src/pages/SmallBusinessLoan/SmallBusinessLoan.js +++ b/client/src/pages/SmallBusinessLoan/SmallBusinessLoan.js @@ -22,7 +22,7 @@ function SmallBusinessLoan({ text, formText, btsText, userFlowText }) { // Send request to server try { - const response = await sendRequest(`${process.env.REACT_APP_API_URL}/api/loanApplication`, body); + const response = await sendRequest(`${process.env.REACT_APP_API_URL}/loanApplication`, body); // Received URL for embedded signing, redirect user if (response.status === 200) { diff --git a/client/src/pages/SmallBusinessLoan/SubmittedLoan.js b/client/src/pages/SmallBusinessLoan/SubmittedLoan.js index 29a3d27..bf0199f 100644 --- a/client/src/pages/SmallBusinessLoan/SubmittedLoan.js +++ b/client/src/pages/SmallBusinessLoan/SubmittedLoan.js @@ -24,8 +24,8 @@ function SubmittedLoan({ text }) { // GETs the loan amount that the user inputted in their loan application, // and sets the lender name accordingly. async function getLoanAmount() { - try{ - let response = await axios.get(`${process.env.REACT_APP_API_URL}/api/loanApplication/submitted`); + try { + let response = await axios.get(`${process.env.REACT_APP_API_URL}/loanApplication/submitted`); // Only set states if the component is mounted, otherwise return null. if (!mountedRef.current) return null; diff --git a/client/src/pages/TrafficTicket/SubmittedTrafficTicket.js b/client/src/pages/TrafficTicket/SubmittedTrafficTicket.js index c7508a8..2309f25 100644 --- a/client/src/pages/TrafficTicket/SubmittedTrafficTicket.js +++ b/client/src/pages/TrafficTicket/SubmittedTrafficTicket.js @@ -33,7 +33,7 @@ function SubmittedTrafficTicket({ text }) { // description of the page accordingly. async function getUserChoice() { try { - let response = await axios.get(`${process.env.REACT_APP_API_URL}/api/trafficTicket/submitted`); + let response = await axios.get(`${process.env.REACT_APP_API_URL}/trafficTicket/submitted`); // Only set states if the component is mounted, otherwise return null. if (!mountedRef.current) return null; diff --git a/client/src/pages/TrafficTicket/TrafficTicket.js b/client/src/pages/TrafficTicket/TrafficTicket.js index ee9f211..04727d7 100644 --- a/client/src/pages/TrafficTicket/TrafficTicket.js +++ b/client/src/pages/TrafficTicket/TrafficTicket.js @@ -22,7 +22,7 @@ function TrafficTicket({ text, formText, btsText, userFlowText }) { // Send request to server try { - const response = await sendRequest(`${process.env.REACT_APP_API_URL}/api/trafficTicket`, body); + const response = await sendRequest(`${process.env.REACT_APP_API_URL}/trafficTicket`, body); // Received URL for embedded signing, redirect user if (response.status === 200) { diff --git a/client/src/pages/TrafficTicket/WitnessStatement.js b/client/src/pages/TrafficTicket/WitnessStatement.js index 3a40f1e..002c8ef 100644 --- a/client/src/pages/TrafficTicket/WitnessStatement.js +++ b/client/src/pages/TrafficTicket/WitnessStatement.js @@ -32,7 +32,7 @@ function WitnessStatement({ text, formText, btsText }) { // Send request to server try { - const response = await sendRequest(`${process.env.REACT_APP_API_URL}/api/trafficTicket/sms`, body); + const response = await sendRequest(`${process.env.REACT_APP_API_URL}/trafficTicket/sms`, body); console.log(response.data); // Set submitted to true to rerender page. diff --git a/docker-compose.yml b/docker-compose.yml index 347c75d..0b85437 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,40 +1,23 @@ version: "3.9" services: - backend: - build: - context: ./server - dockerfile: Dockerfile - container_name: my-backend - ports: - - "5000:5000" - env_file: - - ./server/.env - volumes: - - ./server:/app - - ./server/assets/public:/app/assets/public:ro - networks: - - mynetwork - frontend: build: context: ./client dockerfile: Dockerfile args: - REACT_APP_API_URL: "http://localhost:5000" - container_name: my-frontend + REACT_APP_API_URL: "http://localhost/api" + container_name: frontend ports: - - "3000:80" - environment: - - REACT_APP_API_URL=http://localhost:5000 - - REACT_APP_NODE_ENV=development - depends_on: - - backend + - 80:80 + - 443:443 env_file: - ./client/.env - networks: - - mynetwork -networks: - mynetwork: - driver: bridge + backend: + build: + context: ./server + dockerfile: Dockerfile + container_name: backend + env_file: + - ./server/.env diff --git a/server/Dockerfile b/server/Dockerfile index 895c4e1..faac338 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -1,5 +1,5 @@ # Use a Node.js base image -FROM node:20.14 +FROM --platform=linux/amd64 node:20.14 # Set the working directory WORKDIR /app diff --git a/server/controllers/jwtController.js b/server/controllers/jwtController.js index 6ec633d..60d5d7d 100644 --- a/server/controllers/jwtController.js +++ b/server/controllers/jwtController.js @@ -61,7 +61,7 @@ const getUserInfo = async (req) => { const targetAccountId = process.env.targetAccountId; const baseUriSuffix = '/restapi'; eSignApi.setOAuthBasePath(oAuthBasePath); - + console.log("Seesion Access token", req.session.accessToken) const results = await eSignApi.getUserInfo(req.session.accessToken); let accountInfo; @@ -74,7 +74,7 @@ const getUserInfo = async (req) => { if (typeof accountInfo === 'undefined') { throw new Error(`Target account ${targetAccountId} not found!`); } - + console.log("accountInfo", accountInfo) req.session.accountId = accountInfo.accountId; req.session.basePath = accountInfo.baseUri + baseUriSuffix; } catch (error) { @@ -85,15 +85,14 @@ const getUserInfo = async (req) => { const login = async (req, res, next) => { try { req.session.isLoggedIn = true; - await checkToken(req); - await getUserInfo(req); + try { await checkToken(req); await getUserInfo(req); } catch (e) { console.log(e) } res.status(200).send('Successfully logged in.'); } catch (error) { if (error.message === 'Consent required') { const consent_scopes = scopes + '%20impersonation'; const consent_url = `${process.env.DS_OAUTH_SERVER}/oauth/auth?response_type=code&` + - `scope=${consent_scopes}&client_id=${process.env.INTEGRATION_KEY}&` + - `redirect_uri=${process.env.REDIRECT_URI_HOME}`; + `scope=${consent_scopes}&client_id=${process.env.INTEGRATION_KEY}&` + + `redirect_uri=${process.env.REDIRECT_URI_HOME}`; res.status(210).send(consent_url); } else { req.session.isLoggedIn = false; diff --git a/server/controllers/loanController.js b/server/controllers/loanController.js index 94ea8cb..5a26c42 100644 --- a/server/controllers/loanController.js +++ b/server/controllers/loanController.js @@ -38,7 +38,8 @@ const defaultBrandLanguage = 'en'; const createController = async (req, res, next) => { // Check the access token, which will also update the token // if it is expired - await checkToken(req); + + try { await checkToken(req); } catch (e) { console.log(e) } // Construct arguments const { body } = req; diff --git a/server/controllers/trafficController.js b/server/controllers/trafficController.js index 197aa57..4ff5e8b 100644 --- a/server/controllers/trafficController.js +++ b/server/controllers/trafficController.js @@ -34,9 +34,12 @@ const contestedClerkName = text.contestedClerkName; * embedded signing session. */ const createController = async (req, res, next) => { + console.log("Create controller!") // Check the access token, which will also update the token // if it is expired - await checkToken(req); + console.log("REQ", req) + + try { await checkToken(req); } catch (e) { console.log(e) } // Construct arguments const { body } = req; @@ -67,7 +70,7 @@ const createController = async (req, res, next) => { accountId: req.session.accountId, envelopeArgs: envelopeArgs, }; - + console.log("args", args) let results = null; // Send the envelope to signer @@ -105,7 +108,7 @@ const createController = async (req, res, next) => { // Set results results = { envelopeId: envelopeId, redirectUrl: viewUrl }; } catch (error) { - console.log('Error sending the envelope.'); + console.log('Error sending the envelope.', error); next(error); } diff --git a/server/server.js b/server/server.js index 846b6ba..e574480 100644 --- a/server/server.js +++ b/server/server.js @@ -19,8 +19,8 @@ const trafficRouter = require('./routes/trafficRouter'); const maxSessionAge = 1000 * 60 * 60 * 24 * 1; // One day const corsOptions = { - origin: 'http://localhost:3000', - credentials: true, + origin: ['http://frontend:3000', 'http://localhost:3000', 'http://localhost:80'], + credentials: true, }; const app = express() @@ -38,12 +38,12 @@ const app = express() }) ); - app.use(cors(corsOptions)); +app.use(cors()); - app.use((req, res, next) => { - res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin'); - next(); - }); +app.use((req, res, next) => { + res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin'); + next(); +}); app.get('/', (req, res) => { @@ -57,10 +57,10 @@ app.get('/check-session', (req, res) => { }); // Routing -app.use('/api/auth', authRouter); -app.use('/api/passportApplication', passportRouter); -app.use('/api/loanApplication', loanRouter); -app.use('/api/trafficTicket', trafficRouter); +app.use('/auth', authRouter); +app.use('/passportApplication', passportRouter); +app.use('/loanApplication', loanRouter); +app.use('/trafficTicket', trafficRouter); @@ -99,7 +99,7 @@ app.use((err, req, res, next) => { -console.log('process.env.NODE_ENV',process.env.NODE_ENV) +console.log('process.env.NODE_ENV', process.env.NODE_ENV) // Serve static assets if in production if (process.env.NODE_ENV === 'production') { @@ -111,6 +111,7 @@ if (process.env.NODE_ENV === 'production') { const port = process.env.PORT_NUMBER; -app.listen(port, () => { +console.log('port on server', port) +app.listen(port, '0.0.0.0', () => { console.log(`Server started and listening on port ${port}`); });