From 53ca710f5f04a65082d6237c75da359e2d280bfc Mon Sep 17 00:00:00 2001 From: cpotteck Date: Mon, 3 Aug 2020 17:55:47 -0400 Subject: [PATCH] adapted web app for web deployment on GoogleCloud --- .gcloudignore | 17 +++++++++++++++++ Dockerfile | 21 +++++++++++++++++++++ app.yaml | 2 ++ index.js | 4 +++- main.js | 1 + public/serverScripts.js | 2 +- 6 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 .gcloudignore create mode 100644 Dockerfile create mode 100644 app.yaml diff --git a/.gcloudignore b/.gcloudignore new file mode 100644 index 0000000..a3f0c76 --- /dev/null +++ b/.gcloudignore @@ -0,0 +1,17 @@ +# This file specifies files that are *not* uploaded to Google Cloud Platform +# using gcloud. It follows the same syntax as .gitignore, with the addition of +# "#!include" directives (which insert the entries of the given .gitignore-style +# file at that point). +# +# For more information, run: +# $ gcloud topic gcloudignore +# +.gcloudignore +# If you would like to upload your .git directory, .gitignore file or files +# from your .gitignore file, remove the corresponding line +# below: +.git +.gitignore + +# Node.js dependencies: +node_modules/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c75ff90 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM python:3.7 + +RUN apt-get update + +RUN curl -sL https://deb.nodesource.com/setup_lts.x | bash - +RUN DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs + +WORKDIR /app + +COPY . /app + +RUN apt-get install python3-pip -y +RUN pip3 install --upgrade pip +RUN pip3 install -r requirements.txt + +RUN rm -rf node_modules/* +RUN npm install + +EXPOSE 8080 + +CMD ["npm", "start"] \ No newline at end of file diff --git a/app.yaml b/app.yaml new file mode 100644 index 0000000..bba6d72 --- /dev/null +++ b/app.yaml @@ -0,0 +1,2 @@ +runtime: custom +env: flex \ No newline at end of file diff --git a/index.js b/index.js index ff29762..f6c4cbd 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ const express = require('express'); const python = require('python-shell'); const app = express(); -const PORT = process.env.PORT || 3000; +const PORT = process.env.PORT || 8080; app.use(express.static('public')); app.use(express.json()); @@ -21,10 +21,12 @@ app.post('/math', (req, res) => { const option = { args: [input, isRad, isBinary], + scriptPath: '/app/', // args: [input, isRad, isBinary, isBinaryInput], }; // TODO change to always listening python shell instead of creating and deleting everytime + const pyPath = path.join('', 'calculate.py'); /* eslint-disable-next-line */ const pyshell = new python('calculate.py', option); diff --git a/main.js b/main.js index d040bc8..5971a3c 100644 --- a/main.js +++ b/main.js @@ -16,6 +16,7 @@ function createWindow() { }); window.loadURL('http://localhost:3000/'); + window.loadFile('./public/index.html'); } app.on('ready', createWindow); diff --git a/public/serverScripts.js b/public/serverScripts.js index 6c77de7..e9eeea4 100644 --- a/public/serverScripts.js +++ b/public/serverScripts.js @@ -62,7 +62,7 @@ async function calc() { dlist.appendChild(option); // calls node to perform the calculation over the route - const response = await fetch('http://localhost:3000/math', { + const response = await fetch('/math', { method: 'POST', body: JSON.stringify(input), headers: {