diff --git a/.github/workflows/deploy-to-production.yaml b/.github/workflows/deploy-to-production.yaml index 3ea9355..01214f3 100644 --- a/.github/workflows/deploy-to-production.yaml +++ b/.github/workflows/deploy-to-production.yaml @@ -24,7 +24,11 @@ jobs: port: 22 script: | cd optimism + echo ${{ secrets.EMAIL_USER }} > email_user + echo ${{ secrets.EMAIL_PASS }} > email_pass git pull docker-compose build --build-arg OPTIMISM_API_CLIENT_SIDE_URL=${{ secrets.API_BASE_URL }} - docker-compose down - docker-compose up -d + docker compose down + docker compose up -d + rm email_user + rm email_pass diff --git a/.github/workflows/test-on-new-commit.yaml b/.github/workflows/test-on-new-commit.yaml new file mode 100644 index 0000000..68590ee --- /dev/null +++ b/.github/workflows/test-on-new-commit.yaml @@ -0,0 +1,23 @@ +name: Run the tests whenever there's a new checkin to develop + +on: + push: + branches: [ develop ] + + # Allow it to be run manually from the Github Actions tab + workflow_dispatch: + +jobs: + # We should add a job to run the tests first... + + test: + name: Run the API tests + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Build the Docker image + run: docker build --build-arg=NODE_ENVIRONMENT=testing --build-arg=TEST_FOLDER=test -t test-api ./api + - name: Run the tests + run: docker run --rm=true -t test-api /home/node/app/node_modules/mocha/bin/mocha --recursive --exit --timeout 3500 diff --git a/api/Dockerfile b/api/Dockerfile index 6ce39c9..3a207a5 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -4,10 +4,14 @@ FROM node:20 EXPOSE 3001 +# Arguments to allow us to run production or test from the same Dockerfile +ARG NODE_ENVIRONMENT=production +ARG TEST_FOLDER=no-test + # This might be handy during debugging -ENV NPM_CONFIG_LOGLEVEL info +ENV NPM_CONFIG_LOGLEVEL=info -ENV NODE_ENV=production +ENV NODE_ENV=$NODE_ENVIRONMENT # Install global npm modules to the non-root user (called `node` in the default Node container) ENV NPM_CONFIG_PREFIX=/home/node/.npm-global @@ -31,8 +35,11 @@ COPY routes routes COPY migrations migrations COPY seeds seeds COPY knexfile.js knexfile.js +COPY $TEST_FOLDER test # How we want to start the server +# Default to just starting the server, for the test build we'll override this +# see https://github.com/DoESLiverpool/optimism/issues/32 for details CMD ["node", "/home/node/app/app.js"] # Now we're at the end, set the user we want to run as diff --git a/api/app.js b/api/app.js index 4dd4e08..8abdbd7 100644 --- a/api/app.js +++ b/api/app.js @@ -1,10 +1,25 @@ const express = require('express'); const cors = require('cors'); const app = express(); +const fs = require('fs'); require('dotenv').config(); const port = process.env.OPTIMISM_API_PORT || 3001; +// Read in the email config from the secrets +var email_user = "NEED TO SET EMAIL USER"; +var email_pass = "NEED TO SET EMAIL PASS"; +const email_user_path = "/run/secrets/email_user"; +const email_pass_path = "/run/secrets/email_pass"; +if (fs.existsSync(email_user_path)) +{ + email_user = fs.readFileSync(email_user_path, { encoding: 'utf8' }).trim(); +} +if (fs.existsSync(email_pass_path)) +{ + email_pass = fs.readFileSync(email_pass_path, { encoding: 'utf8' }).trim(); +} + const resourceRoutes = require('./routes/resources'); const resourceTypeRoutes = require('./routes/resourceTypes'); const bookingRoutes = require('./routes/bookings'); diff --git a/api/no-test/.keep b/api/no-test/.keep new file mode 100644 index 0000000..e69de29 diff --git a/api/package-lock.json b/api/package-lock.json index 305d1c7..49d7fc6 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "license": "AGPL-3.0-or-later", "dependencies": { + "@vscode/sqlite3": "^5.0.8", "axios": "^0.21.1", "better-sqlite3": "^11.10.0", "chai": "^4.3.4", @@ -21,6 +22,7 @@ "mocha": "^8.4.0", "moment": "^2.27.0", "mustache-express": "^1.3.0", + "nodemailer": "^6.8.0", "nunjucks": "^3.2.2", "pg": "^8.6.0", "supertest": "^6.1.3" @@ -79,6 +81,15 @@ "resolved": "https://registry.npmjs.org/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz", "integrity": "sha512-sL/cEvJWAnClXw0wHk85/2L0G6Sj8UB0Ctc1TEMbKSsmpRosqhwj9gWgFRZSrBr2f9tiXISwNhCPmlfqUqyb9Q==" }, + "node_modules/@vscode/sqlite3": { + "version": "5.0.8", + "resolved": "https://registry.npmjs.org/@vscode/sqlite3/-/sqlite3-5.0.8.tgz", + "integrity": "sha512-6wvQdMjpi1kwYI5mfzm98siEQb2mlBKX4xdNtJFj/uNqb6wqd3JOhk+5FL7geR0hduXE5lHjv+q69jtsEtUJDA==", + "hasInstallScript": true, + "dependencies": { + "node-addon-api": "^4.2.0" + } + }, "node_modules/a-sync-waterfall": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/a-sync-waterfall/-/a-sync-waterfall-1.0.1.tgz", @@ -443,6 +454,7 @@ "dependencies": { "anymatch": "~3.1.1", "braces": "~3.0.2", + "fsevents": "~2.3.1", "glob-parent": "~5.1.0", "is-binary-path": "~2.1.0", "is-glob": "~4.0.1", @@ -1366,7 +1378,8 @@ "node_modules/ini": { "version": "1.3.8", "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz", - "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==" + "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==", + "dev": true }, "node_modules/interpret": { "version": "2.2.0", @@ -2081,6 +2094,7 @@ "dependencies": { "anymatch": "~3.1.2", "braces": "~3.0.2", + "fsevents": "~2.3.2", "glob-parent": "~5.1.2", "is-binary-path": "~2.1.0", "is-glob": "~4.0.1", @@ -3791,6 +3805,14 @@ "resolved": "https://registry.npmjs.org/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz", "integrity": "sha512-sL/cEvJWAnClXw0wHk85/2L0G6Sj8UB0Ctc1TEMbKSsmpRosqhwj9gWgFRZSrBr2f9tiXISwNhCPmlfqUqyb9Q==" }, + "@vscode/sqlite3": { + "version": "5.0.8", + "resolved": "https://registry.npmjs.org/@vscode/sqlite3/-/sqlite3-5.0.8.tgz", + "integrity": "sha512-6wvQdMjpi1kwYI5mfzm98siEQb2mlBKX4xdNtJFj/uNqb6wqd3JOhk+5FL7geR0hduXE5lHjv+q69jtsEtUJDA==", + "requires": { + "node-addon-api": "^4.2.0" + } + }, "a-sync-waterfall": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/a-sync-waterfall/-/a-sync-waterfall-1.0.1.tgz", @@ -4735,7 +4757,8 @@ "ini": { "version": "1.3.8", "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz", - "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==" + "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==", + "dev": true }, "interpret": { "version": "2.2.0", diff --git a/api/package.json b/api/package.json index 6c4c331..17744aa 100644 --- a/api/package.json +++ b/api/package.json @@ -18,6 +18,7 @@ }, "homepage": "https://github.com/DoESLiverpool/optimism#readme", "dependencies": { + "@vscode/sqlite3": "^5.0.8", "axios": "^0.21.1", "better-sqlite3": "^11.10.0", "chai": "^4.3.4", @@ -30,6 +31,7 @@ "mocha": "^8.4.0", "moment": "^2.27.0", "mustache-express": "^1.3.0", + "nodemailer": "^6.8.0", "nunjucks": "^3.2.2", "pg": "^8.6.0", "supertest": "^6.1.3" diff --git a/docker-compose.yml b/docker-compose.yml index d18f63c..1c34cd2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,6 +14,9 @@ services: - 127.0.0.1:3001:3001 links: - db + secrets: + - email_user + - email_pass restart: always website: build: @@ -26,3 +29,9 @@ services: links: - api restart: always +secrets: + email_user: + file: email_user + email_pass: + file: email_pass + diff --git a/website/Dockerfile b/website/Dockerfile index 34b2f85..304cdf6 100644 --- a/website/Dockerfile +++ b/website/Dockerfile @@ -4,7 +4,7 @@ FROM node:16 EXPOSE 3001 # This might be handy during debugging -ENV NPM_CONFIG_LOGLEVEL info +ENV NPM_CONFIG_LOGLEVEL=info ENV NODE_ENV=production # FIXME Get this from a better place rather than hard-coded