Skip to content

Commit 4eace41

Browse files
feat(_official-jokes-tutorial): update to be in line with latest template (#319)
1 parent df2ccc5 commit 4eace41

File tree

10 files changed

+88
-67
lines changed

10 files changed

+88
-67
lines changed

_official-jokes/.dockerignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
fly.toml
21
/node_modules
32
*.log
43
.DS_Store

_official-jokes/Dockerfile

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
11
# base node image
22
FROM node:16-bullseye-slim as base
33

4-
# Install openssl for Prisma
5-
RUN apt-get update && apt-get install -y openssl
6-
4+
# set for base and all layer that inherit from it
75
ENV NODE_ENV production
86

7+
# Install openssl for Prisma
8+
RUN apt-get update && apt-get install -y openssl sqlite3
9+
910
# Install all node_modules, including dev dependencies
1011
FROM base as deps
1112

12-
RUN mkdir /app
13-
WORKDIR /app
13+
WORKDIR /myapp
1414

15-
ADD package.json package-lock.json ./
16-
RUN npm install --production=false
15+
ADD package.json .npmrc ./
16+
RUN npm install --include=dev
1717

1818
# Setup production node_modules
1919
FROM base as production-deps
2020

21-
RUN mkdir /app
22-
WORKDIR /app
21+
WORKDIR /myapp
2322

24-
COPY --from=deps /app/node_modules /app/node_modules
25-
ADD package.json package-lock.json ./
26-
RUN npm prune --production
23+
COPY --from=deps /myapp/node_modules /myapp/node_modules
24+
ADD package.json .npmrc ./
25+
RUN npm prune --omit=dev
2726

2827
# Build the app
2928
FROM base as build
3029

31-
RUN mkdir /app
32-
WORKDIR /app
30+
WORKDIR /myapp
3331

34-
COPY --from=deps /app/node_modules /app/node_modules
32+
COPY --from=deps /myapp/node_modules /myapp/node_modules
3533

3634
ADD prisma .
3735
RUN npx prisma generate
@@ -42,16 +40,22 @@ RUN npm run build
4240
# Finally, build the production image with minimal footprint
4341
FROM base
4442

45-
ENV NODE_ENV production
43+
ENV DATABASE_URL=file:/data/sqlite.db
44+
ENV PORT="8080"
45+
ENV NODE_ENV="production"
4646

47-
RUN mkdir /app
48-
WORKDIR /app
47+
# add shortcut for connecting to database CLI
48+
RUN echo "#!/bin/sh\nset -x\nsqlite3 \$DATABASE_URL" > /usr/local/bin/database-cli && chmod +x /usr/local/bin/database-cli
4949

50-
COPY --from=production-deps /app/node_modules /app/node_modules
51-
COPY --from=build /app/node_modules/.prisma /app/node_modules/.prisma
52-
COPY --from=build /app/build /app/build
53-
COPY --from=build /app/public /app/public
54-
ADD . .
50+
WORKDIR /myapp
51+
52+
COPY --from=production-deps /myapp/node_modules /myapp/node_modules
53+
COPY --from=build /myapp/node_modules/.prisma /myapp/node_modules/.prisma
54+
55+
COPY --from=build /myapp/build /myapp/build
56+
COPY --from=build /myapp/public /myapp/public
57+
COPY --from=build /myapp/package.json /myapp/package.json
58+
COPY --from=build /myapp/start.sh /myapp/start.sh
59+
COPY --from=build /myapp/prisma /myapp/prisma
5560

56-
ENV PORT 8080
57-
CMD ["npm", "run", "start"]
61+
ENTRYPOINT [ "./start.sh" ]

_official-jokes/app/root.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function Document({ children, title }: PropsWithChildren<{ title?: string }>) {
4343
<html lang="en">
4444
<head>
4545
<meta charSet="utf-8" />
46-
<meta name="viewport" content="width=device-width,initial-scale=1" />
46+
<meta name="viewport" content="width=device-width, initial-scale=1" />
4747
<meta name="keywords" content="Remix,jokes" />
4848
<meta
4949
name="twitter:image"
Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
11
import { PrismaClient } from "@prisma/client";
22

3-
let db: PrismaClient;
3+
import { singleton } from "./singleton.server";
44

5-
declare global {
6-
var __db__: PrismaClient | undefined;
7-
}
8-
9-
// This is needed because in development we don't want to restart
10-
// the server with every change, but we want to make sure we don't
11-
// create a new connection to the DB with every change either.
12-
// In production, we'll have a single connection to the DB.
13-
if (process.env.NODE_ENV === "production") {
14-
db = new PrismaClient();
15-
} else {
16-
if (!global.__db__) {
17-
global.__db__ = new PrismaClient();
18-
}
19-
db = global.__db__;
20-
db.$connect();
21-
}
5+
// Hard-code a unique key, so we can look up the client when this module gets re-imported
6+
const db = singleton("prisma", () => new PrismaClient());
7+
db.$connect();
228

239
export { db };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Since the dev server re-requires the bundle, do some shenanigans to make
2+
// certain things persist across that 😆
3+
// Borrowed/modified from https://github.com/jenseng/abuse-the-platform/blob/2993a7e846c95ace693ce61626fa072174c8d9c7/app/utils/singleton.ts
4+
5+
export const singleton = <Value>(
6+
name: string,
7+
valueFactory: () => Value
8+
): Value => {
9+
const g = global as any;
10+
g.__singletons ??= {};
11+
g.__singletons[name] ??= valueFactory();
12+
return g.__singletons[name];
13+
};

_official-jokes/fly.toml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
1-
# fly.toml file generated for remix-jokes on 2021-11-21T03:55:55-07:00
2-
31
app = "remix-jokes"
2+
43
kill_signal = "SIGINT"
54
kill_timeout = 5
65
processes = []
76

8-
[env]
9-
DATABASE_URL = "file:/data/sqlite.db"
10-
PORT = "8080"
11-
127
[experimental]
8+
allowed_public_ports = []
139
auto_rollback = true
14-
cmd = "start_with_migrations.sh"
10+
cmd = "start.sh"
1511
entrypoint = "sh"
1612

17-
[[mounts]]
18-
destination = "/data"
13+
[mounts]
1914
source = "data"
15+
destination = "/data"
2016

2117
[[services]]
22-
http_checks = []
2318
internal_port = 8080
2419
processes = ["app"]
2520
protocol = "tcp"
2621
script_checks = []
22+
2723
[services.concurrency]
2824
hard_limit = 25
2925
soft_limit = 20
3026
type = "connections"
3127

3228
[[services.ports]]
33-
force_https = true
3429
handlers = ["http"]
3530
port = 80
31+
force_https = true
3632

3733
[[services.ports]]
3834
handlers = ["tls", "http"]
3935
port = 443
4036

41-
[[services.tcp_checks]]
37+
[[services.tcp_checks]]
4238
grace_period = "1s"
4339
interval = "15s"
4440
restart_limit = 0
4541
timeout = "2s"
42+
43+
[[services.http_checks]]
44+
interval = "10s"
45+
grace_period = "5s"
46+
method = "get"
47+
path = "/healthcheck"
48+
protocol = "http"
49+
timeout = "2s"
50+
tls_skip_verify = false
51+
[services.http_checks.headers]

_official-jokes/package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"dependencies": {
1111
"@prisma/client": "^4.12.0",
12+
"@remix-run/css-bundle": "*",
1213
"@remix-run/node": "*",
1314
"@remix-run/react": "*",
1415
"@remix-run/serve": "*",
@@ -24,13 +25,10 @@
2425
"@types/react": "^18.0.35",
2526
"@types/react-dom": "^18.0.11",
2627
"eslint": "^8.38.0",
27-
"prisma": "^4.12.0",
28-
"ts-node": "^10.9.1",
29-
"tsconfig-paths": "^4.2.0",
30-
"typescript": "^4.9.5"
28+
"typescript": "^5.0.4"
3129
},
3230
"engines": {
33-
"node": ">=14"
31+
"node": ">=14.0.0"
3432
},
3533
"prisma": {
3634
"seed": "ts-node --require tsconfig-paths/register prisma/seed.ts"

_official-jokes/remix.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
/** @type {import('@remix-run/dev').AppConfig} */
22
module.exports = {
33
future: {
4+
v2_dev: true,
45
v2_errorBoundary: true,
6+
v2_headers: true,
57
v2_meta: true,
68
v2_normalizeFormMethod: true,
79
v2_routeConvention: true,
810
},
911
ignoredRouteFiles: ["**/.*"],
12+
serverModuleFormat: "cjs",
1013
// appDirectory: "app",
1114
// assetsBuildDirectory: "public/build",
1215
// serverBuildPath: "build/index.js",

_official-jokes/start.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh -ex
2+
3+
# This file is how Fly starts the server (configured in fly.toml). Before starting
4+
# the server though, we need to run any prisma migrations that haven't yet been
5+
# run, which is why this file exists in the first place.
6+
# Learn more: https://community.fly.io/t/sqlite-not-getting-setup-properly/4386
7+
8+
# allocate swap space
9+
fallocate -l 512M /swapfile
10+
chmod 0600 /swapfile
11+
mkswap /swapfile
12+
echo 10 > /proc/sys/vm/swappiness
13+
swapon /swapfile
14+
echo 1 > /proc/sys/vm/overcommit_memory
15+
16+
npx prisma migrate deploy
17+
npm run start

_official-jokes/start_with_migrations.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)