remix-intro-demo

Stars
13

Remix Fly Stack

Before you get started

Note apps on Fly require a globally unique name, we've used the name of the current directory, plus 4 random characters, you can change this at anytime BEFORE you deploy.

Fly Setup

  1. Install Fly

  2. Sign up and log in to Fly

flyctl auth signup

The Database

In development, it's better to use a local database, The easiest way to do this is using Docker. To start your postgres database, first make sure you have docker running, then run the following command:

docker-compose up

That may take a moment to start up as it needs to get the postgres image from the Docker registry, after it's done, you'll need to migrate your database. With the database ready to accept connections, open a new tab and run this:

npx prisma migrate deploy

When this finishes successfully, it will say:

"All migrations have been successfully applied."

If you'd prefer not to use Docker, you can also use Fly's Wireguard VPN to connect to a development database (or even your production database). You can find the instructions to set up Wireguard here, and the instructions for creating a development database here.

Development

With your postgres database up and running in one tab and setup with tables for your data model via prisma, you're ready to start the dev server. Run this in a new tab in your terminal:

npm run dev

This starts your app in development mode, rebuilding assets on file changes.

This is a pretty simple note-taking app, but it's a good example of how you can build a full stack app with Prisma and Remix. The main functionality is creating users, logging in and out, and creating and deleting notes.

Relevant code:

Deployment

This Remix Stack comes with two GitHub actions that handle automatically deploying your app to production and staging environments.

Prior to your first deployment, you'll need to do a few thing:

  • Create a new GitHub Repository

  • Create two apps on Fly, one for staging and one for production:

    fly create remix-intro-demo-9494-staging
    fly create remix-intro-demo-9494
    
  • Make sure you have a FLY_API_TOKEN added to your GitHub repo, to do this, go to your user settings on Fly and create a new token, then add it to your repo secrets with the name FLY_API_TOKEN. Finally you'll need to add a SESSION_SECRET to your fly app secrets, to do this you can run the following commands:

    fly secrets set SESSION_SECRET=$(openssl rand -hex 32) -c fly.staging.toml
    fly secrets set SESSION_SECRET=$(openssl rand -hex 32) -c fly.production.toml
    

    If you don't have openssl installed, you can also use 1password to generate a random secret, just replace $(openssl rand -hex 32) with the generated secret.

  • Create a database for both your staging and production environments. Run the following for both of your environments and follow the prompts (your App name is "remix-intro-demo-9494-db"):

    fly postgres create
    

    afterwards, you'll need to connect your database to each of your apps

    fly postgres attach --postgres-app remix-intro-demo-9494-db --app remix-intro-demo-9494
    

    Fly will take care of setting the DATABASE_URL secret for you.

Now that every is set up you can commit and push your changes to your repo. Every commit to your main branch will trigger a deployment to your production environment, and every commit to your dev branch will trigger a deployment to your staging environment.