Skip to main content

API Server Hosting

It is possible to deploy each client and server separately in Frourio.

The API server is started with the following commands:

npm install --prefix server
npm run build:server
npm run start:server

Below are some examples. Of course, you can deploy in ways not described here, just give it a try!

Dockerfile

Dockerfile
FROM node:15

RUN mkdir /src
RUN mkdir /src/server

WORKDIR /src

COPY /server/package.json /server/package-lock.json ./server/
RUN npm install --prefix server

COPY . .

EXPOSE 8080
CMD npm run build:server && npm run start:server

Dedicated Server (with PM2)

question in create-frourio-appchoice
Daemon process managerPM2
Serverless serviceDedicated server

Step 1. add GitHub Actions Workflow

info

If you selected Dedicated server as the Serverless service when you created the app, please SKIP to Step 2!

If you did not select GitHub Pages when you created the app, please refer to the following workflow to add a deployment workflow.

.github/workflows/deploy-server.yml

.github/workflows/deploy-server.yml
name: Deploy server

on:
push:
branches:
- main

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
- uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.API_DEPLOY_SSH_KEY }}
name: id_rsa
# If known_hosts needs to be set, use this.
# known_hosts: ${{ secrets.API_DEPLOY_KNOWN_HOSTS }}
- run: |
npm install -g pm2@^4.5.1
npx pm2 deploy production setup || true
npx pm2 deploy production update
env:
API_DEPLOY_USER: ${{ secrets.API_DEPLOY_USER }}
API_DEPLOY_HOST: ${{ secrets.API_DEPLOY_HOST }}
API_DEPLOY_REPO: https://github.com/${{ github.repository }}.git
API_DATABASE_URL: ${{ secrets.API_DATABASE_URL }}
API_JWT_SECRET: ${{ secrets.API_JWT_SECRET }}
API_USER_ID: ${{ secrets.API_USER_ID }}
API_USER_PASS: ${{ secrets.API_USER_PASS }}
API_ORIGIN: ${{ secrets.API_ORIGIN }}
API_BASE_PATH: ${{ secrets.API_BASE_PATH }}
API_SERVER_PORT: ${{ secrets.API_SERVER_PORT }}
API_UPLOAD_DIR: ${{ secrets.API_UPLOAD_DIR }}

Step 2. add Secrets to Repository

Add following secrets to your GitHub repository.

  • API_BASE_PATH: Your API basepath.
    e.g. /api
  • API_DATABASE_URL: Production URL for MySQL.
    e.g. mysql://mysql-instance1.123456789012.us-east-1.rds.amazonaws.com:3306
  • API_DEPLOY_HOST: Dedicated server host.
    e.g. ec2-public-ipv4-address.compute-1.amazonaws.com
  • API_DEPLOY_USER: SSH username.
    e.g. ci
  • API_DEPLOY_SSH_KEY: SSH private key that can connect to the above host.
    e.g.
    1. Run ssh-keygen -t rsa -b 4096 -m PEM -f frourio-ci.key on your local machine.
    2. Copy contents of frourio-ci.key and paste it to this secrets value.
    3. Send frourio-ci.key.pub to your host machine, and append it to ~/.ssh/known_hosts on remote host.
  • API_UPLOAD_DIR: The directory to upload user contents, for example icons.
    e.g. /mnt/efs-1/upload, /srv/upload
    • In default sample, it is used to save uploaded icons.

AWS Lambda (Static Hosting)

question in create-frourio-appchoice
API server hostingServerless
Serverless serviceAWS Lambda

This is complicated to configure, so you should choose the above choices in create-frourio-app to configure it automatically.

The following is the description that appears when you select AWS Lambda in create-frourio-app.

caution

Little difficult option for beginners. You can find more concrete example here.

To develop serverless applications, you should consider how huge are the modules and bundled files. Please note that AWS Lambda has the size limit 250MB.

At least, you should prepare following for deploying to AWS Lambda.

  • Lambda function to respond to user requests.
    • Set this Lambda's name to GitHub Actions Secrets LAMBDA_FUNCTION_NAME_SERVER
  • Lambda function to run migrations.
    • Set this Lambda's name to GitHub Actions Secrets LAMBDA_FUNCTION_NAME_MIGRATION
    • NOTE: Recommended to set longer time limit.
  • Amazon S3 to upload deployment artifacts like built bundle scripts and node_modules.
    • Set this S3's bucket name to GitHub Actions Secrets S3_BUCKET
    • Optional: To specify the S3 key prefix, also add S3_PREFIX to secrets. e.g. deployments/
    • To elaborate, the key ${S3_BUCKET}/${S3_PREFIX}deployment_server.zip is used to store the data.