Skip to content

Commit

Permalink
setup for fly.io deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderGi committed Apr 14, 2024
1 parent d20bc85 commit 1e491b9
Show file tree
Hide file tree
Showing 28 changed files with 991 additions and 427 deletions.
8 changes: 4 additions & 4 deletions .badges/lines-of-code.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Node Modules
node_modules

# Cache
.firebase

# Secrets
.env
.data

# Test Logs
test.*.log
tap.info

# OS Files
.DS_Store
21 changes: 15 additions & 6 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
.glitchdotcom.json
.node-gyp
# Third Party
node_modules
.data
.DS_Store
.env
public/lib
migrations

# Cache
.firebase

# Secrets
.env
.data

# Test Logs
test.*.log
tap.info
tap.info

# OS Files
.DS_Store
4 changes: 1 addition & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ jobs:
run: docker run -d -p 4444:4444 --network="host" --shm-size=2g selenium/standalone-chrome:latest
- name: Install dependencies
run: npm ci
- name: Create Service Account key for Firebase
run: echo '${{ secrets.FIREBASE_SERVICE_ACCOUNT }}' > gcloud.json
- name: Run tests with chrome
run: |
SELENIUM_REMOTE_URL="http://localhost:4444/wd/hub" SELENIUM_BROWSER=chrome npm test
env:
TOKEN: ${{ secrets.TOKEN }}
GOOGLE_APPLICATION_CREDENTIALS: 'gcloud.json'
FIREBASE_SERVICE_ACCOUNT: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }}
MERCHANTID: ${{ secrets.MERCHANTID }}
MERCHANTPUBLIC: ${{ secrets.MERCHANTPUBLIC }}
MERCHANTPRIVATE: ${{ secrets.MERCHANTPRIVATE }}
19 changes: 13 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
.glitchdotcom.json
.node-gyp
# Node Modules
node_modules
.data
.DS_Store
.env

# Cache
.firebase

# Secrets
.env
.data

# Test Logs
test.*.log
tap.info
tap.info

# OS Files
.DS_Store
24 changes: 18 additions & 6 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
.glitchdotcom.json
.node-gyp
# Third Party
node_modules
.data
.DS_Store
.env
public/lib
migrations
server/super_admin

# Minified Files
**/*.min.js

# Cache
.firebase

# Secrets
.env
.data

# Test Logs
test.*.log
tap.info
**/*.min.js

# OS Files
.DS_Store
46 changes: 46 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=18.17.0
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="Node.js"

# Node.js app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3

# Install node modules
COPY --link package-lock.json package.json ./
RUN npm ci

# Copy application code
COPY --link . .


# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app /app

# Setup a volume for data storage
RUN mkdir -p /data
VOLUME /data

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
ENV PORT=3000
ENV DB_FILE="/data/sqlite.db"
ENV ATT_CODE_DIR="/data/att_codes"
CMD [ "npm", "run", "start" ]
54 changes: 24 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,45 @@ URL: https://attendancescannerqr.web.app

## Branches

- `master` - production branch - clone of Glitch branch (Glitch is currently used for hosting the server)
- `main` - main development branch - latest stable version of the code, hosted on Github

## Local Development

### Setup

1. `git clone https://github.com/clr-li/AttendanceScanner.git`
2. `git remote add glitch [INSERT GLITCH API URL HERE AND REMOVE THESE BRACKETS]`
3. Add the `.data` directory and `.env` file (can be found on Glitch)
- These are already `.gitignore`'d to avoid leaking API keys and secrets and user data
4. `npm ci` to install the necessary dependencies from the package lock
5. `npm run dev` (this will run the local server using the local .env file; `npm start` is only for Glitch)
- After running `npm run dev`, use a browser to go to the localhost port printed (this should automatically open on most systems)
2. Add the `.data` directory and `.env` file
- These are `.gitignore`'d to avoid leaking API keys, secrets and user data
3. `npm ci` to install the necessary dependencies from the package lock
4. `npm run dev` (this will run the local server using the local .env file; `npm start` is only for production)
5. Use a browser to go to the localhost port printed (this should automatically open on most systems)

### Workflow
### Common Commands

0. Start with `git pull`, and make sure you are on the right branch, e.g. `git checkout main`

  For small changes:

1. Make changes to the local `main` branch
2. `git add` and `git commit` any changes locally
3. `git push origin` to push to the Github `main` branch

  For larger changes:

1. `git checkout -b [NAME OF NEW DEVELOPMENT BRANCH]` to create a separate branch
2. Make changes and `git add` and `git commit` locally
3. `git push origin` and create a pull request for others to review and merge into `main` after review

&nbsp;&nbsp;Then to deploy Github `main` to Glitch and Firebase:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4. `npm run deploy`<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- Or to only deploy to Glitch: `npm run glitch`<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- Or to only deploy static files to Firebase: `npm run fire`
- To run the server locally: `npm run dev`
- To run formatting and build fonts and other resources: `npm run build`
- To run tests and linting: `npm test`
- To deploy both server and static files: `npm run deploy`
- To only deploy static files: `npm run deploy:fire`
- To run the server locally with production settings: `npm run docker:build && npm run docker:run`

### Update database

- To update the database schema, change the `schema.sql` file accordingly (note this file should only contain DDL statements). If you are running the `npm run dev` server, a new migration file will automatically be created in the `migration` folder and applied locally. Otherwise, you can run `sam make` to create it and `sam migrate` to apply it locally. Run `sam status` to verify your changes and optionally inspect the autocreated migration file. Once you are satisfied everything is in order, `npm run deploy` changes like normal and the production server will automatically apply the new migration file.
- To purge the Braintree payment vault test data, login to the Braintree sandbox, click the gear icon and select "Purge Test Data"

### Manage Deployment

- Configure server: [fly.io](https://fly.io/apps/attendqr)
- [Memory usage and requests dashboard](https://fly-metrics.net/d/fly-app/fly-app?orgId=726754)
- [View logs](https://fly-metrics.net/d/fly-logs/fly-logs?orgId=726754&var-app=attendqr)
- Configure firebase: [firebase console](https://console.firebase.google.com/u/0/project/attendancescannerqr/overview)
- [Configure auth](https://console.firebase.google.com/u/0/project/attendancescannerqr/authentication/users)
- [Docs for static file hosting](https://firebase.google.com/docs/hosting)
- Configure Braintree: [sandbox](https://sandbox.braintreegateway.com/login)
- [Docs for testing](https://developers.braintreepayments.com/start/hello-server/node)
- [Docs for production](https://developer.paypal.com/braintree/docs/start/go-live/node)

## Automated Testing

1. Before running tests, run `npm install` (freshly installs dependencies from package.json) or `npm ci` (install specific dependency versions from package-lock) to make sure dependencies are installed.
Expand Down Expand Up @@ -90,7 +88,3 @@ We use [Selenium](https://www.npmjs.com/package/selenium-webdriver) for cross br
#### Setup Tests of the OAuth Flow

By default tests mock the Firebase Authentication layer (to run faster and not require storing Google account credentials). To test with a real Google account, run tests with an account email (`[email protected]`) and password (`TEST_PASSWORD=xxxx`) in the `.env` file. The tests will attempt to automatically login for these test, but they may still require manual input during the login phase if your account has MFA enabled or other security settings that interfere. Currently only supported for Google Chrome.

## Glitch Development

We should not edit directly on Glitch except to change .gitignored content like the production `.data` or `.env`. If the changes only apply to Glitch and not local development, just change directly. Otherwise, make sure leave a note somewhere.
26 changes: 26 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# fly.toml app configuration file generated for attendqr on 2024-04-13T17:42:28-07:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'attendqr'
primary_region = 'sea'

[build]

[[mounts]]
source = 'data'
destination = '/data'

[http_service]
internal_port = 3000
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ['app']

[[vm]]
memory = '1gb'
cpu_kind = 'shared'
cpus = 1
Loading

0 comments on commit 1e491b9

Please sign in to comment.