diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +node_modules diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0adddb3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,57 @@ +# base node image +FROM node:16-bullseye-slim as base + +# Install openssl for Prisma +RUN apt-get update && apt-get install -y openssl + +# set for base and all that inherit from it +ENV NODE_ENV=production + +# Install all node_modules, including dev dependencies +FROM base as deps + +RUN mkdir /app +WORKDIR /app + +ADD package.json package-lock.json ./ +RUN npm install --production=false + +# Setup production node_modules +FROM base as production-deps + +RUN mkdir /app +WORKDIR /app + +COPY --from=deps /app/node_modules /app/node_modules +ADD package.json package-lock.json ./ +RUN npm prune --production + +# Build the app +FROM base as build + +RUN mkdir /app +WORKDIR /app + +COPY --from=deps /app/node_modules /app/node_modules + +ADD prisma . +RUN npx prisma generate + +ADD . . +RUN npm run build + +# Finally, build the production image with minimal footprint +FROM base + +ENV NODE_ENV=production + +RUN mkdir /app +WORKDIR /app + +COPY --from=production-deps /app/node_modules /app/node_modules +COPY --from=build /app/node_modules/.prisma /app/node_modules/.prisma +COPY --from=build /app/build /app/build +COPY --from=build /app/public /app/public +ADD . . + +CMD ["npm", "run", "start"] diff --git a/fly.toml b/fly.toml index aea7dca..6699d99 100644 --- a/fly.toml +++ b/fly.toml @@ -7,10 +7,17 @@ kill_timeout = 5 processes = [] [env] + DATABASE_URL = "file:/data/sqlite.db" + PORT = "8080" + NODE_ENV = "production" [experimental] - allowed_public_ports = [] - auto_rollback = true + cmd = "start_with_migrations.sh" + entrypoint = "sh" + +[[mounts]] + destination = "/data" + source = "data" [[services]] http_checks = [] diff --git a/prisma/migrations/20220211120752_init/migration.sql b/prisma/migrations/20220211120752_init/migration.sql new file mode 100644 index 0000000..06b265e --- /dev/null +++ b/prisma/migrations/20220211120752_init/migration.sql @@ -0,0 +1,32 @@ +-- CreateTable +CREATE TABLE "Team" ( + "id" TEXT NOT NULL PRIMARY KEY, + "icon" TEXT NOT NULL, + "description" TEXT +); + +-- CreateTable +CREATE TABLE "User" ( + "id" TEXT NOT NULL PRIMARY KEY, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL, + "username" TEXT NOT NULL, + "icon" TEXT NOT NULL, + "passwordHash" TEXT NOT NULL, + "teamId" TEXT NOT NULL, + CONSTRAINT "User_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "Team" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +-- CreateTable +CREATE TABLE "Expense" ( + "id" TEXT NOT NULL PRIMARY KEY, + "userId" TEXT NOT NULL, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL, + "description" TEXT NOT NULL, + "amount" REAL NOT NULL, + CONSTRAINT "Expense_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +-- CreateIndex +CREATE UNIQUE INDEX "User_username_key" ON "User"("username"); diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..e5e5c47 --- /dev/null +++ b/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "sqlite" \ No newline at end of file diff --git a/start_with_migrations.sh b/start_with_migrations.sh new file mode 100644 index 0000000..42b81ba --- /dev/null +++ b/start_with_migrations.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +set -ex +npx prisma migrate deploy +npm run start