create-tigra 3.3.0 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +151 -0
- package/README.md +264 -56
- package/bin/create-tigra.js +497 -472
- package/lib/patchers/email-verification.patcher.js +72 -10
- package/modules/email-verification/client/hooks/useVerification.ts +4 -1
- package/modules/email-verification/server/__tests__/verification.integration.test.ts +115 -0
- package/modules/email-verification/server/verification.controller.ts +3 -1
- package/modules/email-verification/server/verification.service.ts +44 -13
- package/package.json +5 -3
- package/template/.github/dependabot.yml +47 -0
- package/template/.github/workflows/audit.yml +36 -0
- package/template/.github/workflows/ci.yml +79 -0
- package/template/AGENTS.md +18 -9
- package/template/README.md +79 -0
- package/template/client/.dockerignore +1 -0
- package/template/client/.env.example.production +13 -2
- package/template/client/AGENTS.md +13 -5
- package/template/client/Dockerfile +186 -14
- package/template/client/README.md +92 -21
- package/template/client/next.config.ts +1 -1
- package/template/client/package-lock.json +15983 -14181
- package/template/client/package.json +56 -48
- package/template/client/scripts/check-sharp.mjs +53 -0
- package/template/client/scripts/next-dev.cjs +5 -1
- package/template/client/src/__tests__/proxy.test.ts +92 -0
- package/template/client/src/app/healthz/route.ts +16 -0
- package/template/client/src/app/layout.tsx +9 -3
- package/template/client/src/app/page.tsx +2 -2
- package/template/client/src/app/providers.tsx +23 -4
- package/template/client/src/components/layout/Header.tsx +7 -3
- package/template/client/src/features/admin/hooks/useAdminSessions.ts +2 -1
- package/template/client/src/features/admin/hooks/useAdminUsers.ts +3 -1
- package/template/client/src/features/admin/types/admin.types.ts +2 -3
- package/template/client/src/features/auth/components/AuthInitializer.tsx +35 -18
- package/template/client/src/features/auth/components/ForgotPasswordForm.tsx +107 -0
- package/template/client/src/features/auth/components/LimitNotice.tsx +28 -0
- package/template/client/src/features/auth/components/LoginForm.tsx +13 -2
- package/template/client/src/features/auth/components/RegisterForm.tsx +23 -7
- package/template/client/src/features/auth/hooks/useAuth.ts +11 -1
- package/template/client/src/features/auth/hooks/usePasswordReset.ts +7 -1
- package/template/client/src/features/auth/services/auth.service.ts +8 -3
- package/template/client/src/features/auth/store/authSlice.ts +7 -2
- package/template/client/src/lib/api/__tests__/axios.config.test.ts +183 -0
- package/template/client/src/lib/api/api.types.ts +14 -0
- package/template/client/src/lib/api/axios.config.ts +101 -15
- package/template/client/src/lib/api/hard-redirect.ts +8 -0
- package/template/client/src/lib/api/session-events.ts +29 -0
- package/template/client/src/lib/constants/routes.ts +12 -0
- package/template/client/src/lib/observability/sentry-scrub.ts +1 -1
- package/template/client/src/lib/utils/__tests__/error.test.ts +119 -0
- package/template/client/src/lib/utils/__tests__/form.test.ts +42 -0
- package/template/client/src/lib/utils/__tests__/paths.test.ts +43 -0
- package/template/client/src/lib/utils/__tests__/security.test.ts +40 -0
- package/template/client/src/lib/utils/__tests__/session-marker.test.ts +27 -0
- package/template/client/src/lib/utils/error.ts +81 -0
- package/template/client/src/lib/utils/form.ts +31 -0
- package/template/client/src/lib/utils/paths.ts +18 -0
- package/template/client/src/lib/utils/session-marker.ts +24 -0
- package/template/client/src/{middleware.ts → proxy.ts} +25 -11
- package/template/client/src/store/__tests__/store.test.ts +44 -0
- package/template/client/src/store/index.ts +26 -8
- package/template/client/vitest.config.mts +48 -0
- package/template/server/.env.example +280 -270
- package/template/server/.env.example.production +283 -242
- package/template/server/AGENTS.md +13 -4
- package/template/server/Dockerfile +176 -14
- package/template/server/README.md +108 -0
- package/template/server/docker-compose.yml +4 -2
- package/template/server/docs/deployment.md +63 -0
- package/template/server/docs/logging.md +66 -4
- package/template/server/docs/rate-limits.md +153 -0
- package/template/server/docs/testing.md +81 -0
- package/template/server/eslint.config.mjs +19 -0
- package/template/server/package-lock.json +8784 -8743
- package/template/server/package.json +79 -75
- package/template/server/prisma/migrations/0_init/migration.sql +82 -0
- package/template/server/prisma/migrations/migration_lock.toml +3 -0
- package/template/server/prisma/schema.prisma +0 -3
- package/template/server/scripts/check-heic.mjs +40 -0
- package/template/server/scripts/check-sharp.mjs +53 -0
- package/template/server/src/__tests__/routes.integration.test.ts +280 -0
- package/template/server/src/app.ts +248 -346
- package/template/server/src/config/__tests__/env.test.ts +219 -0
- package/template/server/src/config/__tests__/rate-limit.config.test.ts +158 -0
- package/template/server/src/config/env.ts +326 -182
- package/template/server/src/config/rate-limit.config.ts +157 -106
- package/template/server/src/jobs/__tests__/cleanup-deleted-accounts.job.test.ts +60 -8
- package/template/server/src/jobs/__tests__/cleanup-expired-auth.job.test.ts +44 -0
- package/template/server/src/jobs/__tests__/jobs.integration.test.ts +88 -0
- package/template/server/src/jobs/__tests__/schedule.test.ts +92 -0
- package/template/server/src/jobs/cleanup-deleted-accounts.job.ts +38 -36
- package/template/server/src/jobs/cleanup-expired-auth.job.ts +32 -24
- package/template/server/src/jobs/index.ts +12 -2
- package/template/server/src/jobs/refresh-cloudflare-ranges.job.ts +28 -0
- package/template/server/src/jobs/schedule.ts +50 -0
- package/template/server/src/libs/__tests__/auth.test.ts +98 -7
- package/template/server/src/libs/__tests__/client-ip.test.ts +257 -121
- package/template/server/src/libs/__tests__/cloudflare-ranges.test.ts +79 -0
- package/template/server/src/libs/__tests__/cookies.test.ts +53 -2
- package/template/server/src/libs/__tests__/email-quota.test.ts +81 -0
- package/template/server/src/libs/__tests__/email.test.ts +68 -0
- package/template/server/src/libs/__tests__/error-handler.test.ts +162 -0
- package/template/server/src/libs/__tests__/health-db-down.integration.test.ts +37 -0
- package/template/server/src/libs/__tests__/health-redis-down.integration.test.ts +45 -0
- package/template/server/src/libs/__tests__/health-routes.test.ts +83 -0
- package/template/server/src/libs/__tests__/health.integration.test.ts +118 -0
- package/template/server/src/libs/__tests__/http.test.ts +74 -2
- package/template/server/src/libs/__tests__/ip-block.test.ts +179 -62
- package/template/server/src/libs/__tests__/logger.test.ts +50 -0
- package/template/server/src/libs/__tests__/monitoring.test.ts +185 -0
- package/template/server/src/libs/__tests__/outbound.integration.test.ts +48 -0
- package/template/server/src/libs/__tests__/prisma-client.test.ts +28 -0
- package/template/server/src/libs/__tests__/query-counter.integration.test.ts +53 -0
- package/template/server/src/libs/__tests__/rate-limiting.test.ts +146 -0
- package/template/server/src/libs/__tests__/rate-limits.integration.test.ts +53 -0
- package/template/server/src/libs/__tests__/redis-required.test.ts +30 -0
- package/template/server/src/libs/__tests__/redis.test.ts +38 -2
- package/template/server/src/libs/__tests__/request-id.test.ts +25 -0
- package/template/server/src/libs/__tests__/requestLogger.test.ts +126 -0
- package/template/server/src/libs/__tests__/safe-http.test.ts +138 -0
- package/template/server/src/libs/__tests__/sentry-scrub.test.ts +82 -80
- package/template/server/src/libs/__tests__/shutdown.test.ts +89 -0
- package/template/server/src/libs/__tests__/url-safety.test.ts +130 -80
- package/template/server/src/libs/auth.ts +39 -10
- package/template/server/src/libs/client-ip.ts +158 -77
- package/template/server/src/libs/cloudflare-ranges.ts +132 -0
- package/template/server/src/libs/cookies.ts +53 -14
- package/template/server/src/libs/email-quota.ts +74 -0
- package/template/server/src/libs/email.ts +46 -9
- package/template/server/src/libs/error-handler.ts +111 -0
- package/template/server/src/libs/health-routes.ts +53 -0
- package/template/server/src/libs/http.ts +139 -47
- package/template/server/src/libs/ip-address.ts +37 -0
- package/template/server/src/libs/ip-block.ts +284 -221
- package/template/server/src/libs/logger.ts +41 -12
- package/template/server/src/libs/monitoring.ts +127 -41
- package/template/server/src/libs/observability/sentry.ts +1 -1
- package/template/server/src/libs/origin-check.ts +9 -7
- package/template/server/src/libs/prisma.ts +47 -4
- package/template/server/src/libs/query-counter.ts +71 -59
- package/template/server/src/libs/rate-limiting.ts +110 -0
- package/template/server/src/libs/redis-required.ts +28 -0
- package/template/server/src/libs/redis.ts +27 -1
- package/template/server/src/libs/request-id.ts +18 -0
- package/template/server/src/libs/requestLogger.ts +112 -17
- package/template/server/src/libs/safe-http.ts +101 -0
- package/template/server/src/libs/shutdown.ts +87 -0
- package/template/server/src/libs/storage/__tests__/fixtures/tiny-heic.ts +13 -0
- package/template/server/src/libs/storage/__tests__/image-optimizer.test.ts +59 -0
- package/template/server/src/libs/storage/__tests__/multipart-limits.test.ts +91 -0
- package/template/server/src/libs/storage/__tests__/upload-persistence.test.ts +52 -0
- package/template/server/src/libs/storage/file-storage.service.ts +94 -84
- package/template/server/src/libs/storage/file-validator.ts +48 -46
- package/template/server/src/libs/storage/image-optimizer.service.ts +16 -5
- package/template/server/src/libs/token-hash.ts +18 -0
- package/template/server/src/libs/url-safety.ts +127 -121
- package/template/server/src/libs/validation-errors.ts +86 -0
- package/template/server/src/modules/admin/__tests__/admin-actions.integration.test.ts +255 -0
- package/template/server/src/modules/admin/__tests__/admin.health.test.ts +82 -0
- package/template/server/src/modules/admin/__tests__/admin.repo.test.ts +55 -0
- package/template/server/src/modules/admin/__tests__/admin.schemas.test.ts +38 -0
- package/template/server/src/modules/admin/__tests__/admin.service.test.ts +162 -0
- package/template/server/src/modules/admin/admin.controller.ts +66 -49
- package/template/server/src/modules/admin/admin.repo.ts +49 -10
- package/template/server/src/modules/admin/admin.routes.ts +48 -2
- package/template/server/src/modules/admin/admin.schemas.ts +18 -6
- package/template/server/src/modules/admin/admin.service.ts +66 -4
- package/template/server/src/modules/auth/__tests__/auth.controller.test.ts +39 -0
- package/template/server/src/modules/auth/__tests__/auth.integration.test.ts +9 -3
- package/template/server/src/modules/auth/__tests__/auth.service.dummy-hash.test.ts +34 -0
- package/template/server/src/modules/auth/__tests__/auth.service.test.ts +197 -37
- package/template/server/src/modules/auth/__tests__/session-cleanup.test.ts +39 -0
- package/template/server/src/modules/auth/__tests__/sessions.integration.test.ts +246 -0
- package/template/server/src/modules/auth/__tests__/token-fingerprints.test.ts +119 -0
- package/template/server/src/modules/auth/auth.controller.ts +133 -128
- package/template/server/src/modules/auth/auth.repo.ts +44 -44
- package/template/server/src/modules/auth/auth.schemas.ts +4 -2
- package/template/server/src/modules/auth/auth.service.ts +167 -96
- package/template/server/src/modules/auth/session.repo.ts +12 -0
- package/template/server/src/modules/files/__tests__/files.integration.test.ts +5 -5
- package/template/server/src/modules/users/__tests__/avatar-upload.test.ts +120 -0
- package/template/server/src/modules/users/__tests__/name-rules.test.ts +46 -0
- package/template/server/src/modules/users/__tests__/users.integration.test.ts +295 -0
- package/template/server/src/modules/users/__tests__/users.repo.test.ts +37 -0
- package/template/server/src/modules/users/__tests__/users.service.test.ts +129 -0
- package/template/server/src/modules/users/users.controller.ts +9 -38
- package/template/server/src/modules/users/users.repo.ts +23 -7
- package/template/server/src/modules/users/users.routes.ts +3 -22
- package/template/server/src/modules/users/users.schemas.ts +15 -9
- package/template/server/src/modules/users/users.service.ts +105 -93
- package/template/server/src/scripts/make-admin.ts +48 -0
- package/template/server/src/server.ts +48 -26
- package/template/server/src/shared/errors/AppError.ts +27 -1
- package/template/server/src/shared/errors/errors.ts +42 -5
- package/template/server/src/shared/responses/successResponse.ts +22 -3
- package/template/server/src/shared/types/index.ts +7 -0
- package/template/server/src/test/__tests__/integration-services.test.ts +66 -0
- package/template/server/src/test/integration-helpers.ts +343 -0
- package/template/server/src/test/integration-services.ts +74 -0
- package/template/server/src/test/integration.global-setup.ts +144 -0
- package/template/server/src/test/integration.setup.ts +35 -158
- package/template/server/src/test/setup.ts +3 -10
- package/template/server/tsconfig.build.json +1 -1
- package/template/server/tsconfig.json +1 -0
- package/template/server/vitest.config.ts +17 -4
- package/template/server/vitest.integration.config.ts +18 -13
- package/template/server/src/libs/storage/filename-sanitizer.ts +0 -71
|
@@ -6,11 +6,84 @@
|
|
|
6
6
|
# BuildKit cache mounts (--mount=type=cache) keep the npm download cache warm
|
|
7
7
|
# ACROSS builds, so a lockfile-change rebuild re-installs without re-downloading.
|
|
8
8
|
# Requires the `# syntax` line above; auto-enabled on Docker 23+ / any BuildKit.
|
|
9
|
+
#
|
|
10
|
+
# Every stage uses the same pinned node:24-alpine image (Node 24.22.0,
|
|
11
|
+
# npm 11.19.1, Alpine 3.24). Node 24 is the template's runtime (package.json
|
|
12
|
+
# engines >=24). `npm ci` below fails when package-lock.json was written by an
|
|
13
|
+
# older npm: npm 10 always, and early npm 11 too (11.6.1 leaves out the
|
|
14
|
+
# @emnapi/* entries this npm requires). Write lockfiles with npm 11.19 or newer.
|
|
9
15
|
|
|
10
16
|
# ===================================================================
|
|
11
|
-
# Stage 1:
|
|
17
|
+
# Stage 1: libvips that runs on any x86-64 CPU
|
|
12
18
|
# ===================================================================
|
|
13
|
-
|
|
19
|
+
# Why this stage exists: sharp's prebuilt addon and the libvips it bundles
|
|
20
|
+
# need an x86-64-v2 CPU (SSE4.2/POPCNT). Many VPS hosts present an older
|
|
21
|
+
# virtual CPU (e.g. "QEMU Virtual CPU"), where the prebuilt sharp refuses to
|
|
22
|
+
# load and the server crashes at start. So we compile a pinned libvips release
|
|
23
|
+
# for the generic x86-64 baseline and build sharp's addon against it (stage 2).
|
|
24
|
+
#
|
|
25
|
+
# KEEP THIS STAGE BYTE-IDENTICAL in server/Dockerfile and client/Dockerfile,
|
|
26
|
+
# so one rule set builds both images. Identical text also lets a plain
|
|
27
|
+
# `docker build` host reuse the compiled layer across the two apps. On
|
|
28
|
+
# Coolify (measured): redeploys of the same app on the same server reused it,
|
|
29
|
+
# but the two apps did not share it, and a change to a build-time variable
|
|
30
|
+
# compiled it again (Coolify passes those into the build). An uncached
|
|
31
|
+
# compile takes about 6-7 min on a small shared VPS.
|
|
32
|
+
#
|
|
33
|
+
# Version coupling: every sharp release needs a minimum libvips (the
|
|
34
|
+
# "config.libvips" range in sharp's package.json, e.g. ">=8.18.7" for sharp
|
|
35
|
+
# 0.35.5). When a sharp upgrade needs more than LIBVIPS_VERSION, bump
|
|
36
|
+
# LIBVIPS_VERSION and LIBVIPS_SHA256 (from the libvips release page) in BOTH
|
|
37
|
+
# Dockerfiles. Stage 2 stops the build with a clear message if you forget.
|
|
38
|
+
FROM node:24-alpine@sha256:44fce696f38ba8c987c4db89954051000bbd05ffd8d8659ba15ed4a8602e937f AS libvips
|
|
39
|
+
|
|
40
|
+
ARG LIBVIPS_VERSION=8.18.7
|
|
41
|
+
ARG LIBVIPS_SHA256=5baaead3b0bb20ffdb9e9ff09aa9fda08620923df77b63b436654cb5e0b3bf94
|
|
42
|
+
|
|
43
|
+
# Compilers and codec headers. python3 serves node-gyp in the stage that
|
|
44
|
+
# builds FROM this one. Every -dev package here needs its runtime library in
|
|
45
|
+
# the production stage's apk list (keep the two lists in sync).
|
|
46
|
+
RUN apk add --no-cache \
|
|
47
|
+
build-base python3 curl xz meson ninja pkgconf \
|
|
48
|
+
glib-dev expat-dev libjpeg-turbo-dev libpng-dev libwebp-dev \
|
|
49
|
+
tiff-dev lcms2-dev libheif-dev libexif-dev
|
|
50
|
+
|
|
51
|
+
ENV PKG_CONFIG_PATH=/opt/vips/lib/pkgconfig \
|
|
52
|
+
LD_LIBRARY_PATH=/opt/vips/lib
|
|
53
|
+
|
|
54
|
+
# --auto-features=disabled turns every optional dependency off; only the
|
|
55
|
+
# formats we use are enabled: JPEG/PNG/WebP/HEIF/TIFF (plus the built-in GIF
|
|
56
|
+
# loader, which Next's image optimizer accepts), colour profiles, and EXIF
|
|
57
|
+
# (needed to read orientation and strip metadata). The rarely used PPM,
|
|
58
|
+
# Analyze and Radiance loaders are off to shrink the parser attack surface.
|
|
59
|
+
# -march=x86-64 is the baseline every x86-64 CPU supports (other
|
|
60
|
+
# architectures keep the compiler default). -j 2 bounds memory on small
|
|
61
|
+
# build hosts.
|
|
62
|
+
RUN set -eu; \
|
|
63
|
+
if [ "$(uname -m)" = "x86_64" ]; then \
|
|
64
|
+
export CFLAGS="-march=x86-64 -mtune=generic" CXXFLAGS="-march=x86-64 -mtune=generic"; \
|
|
65
|
+
fi; \
|
|
66
|
+
curl -fsSL --retry 3 -o /tmp/vips.tar.xz \
|
|
67
|
+
"https://github.com/libvips/libvips/releases/download/v${LIBVIPS_VERSION}/vips-${LIBVIPS_VERSION}.tar.xz"; \
|
|
68
|
+
echo "${LIBVIPS_SHA256} /tmp/vips.tar.xz" | sha256sum -c -; \
|
|
69
|
+
tar -xJf /tmp/vips.tar.xz -C /tmp; \
|
|
70
|
+
meson setup /tmp/vips-build "/tmp/vips-${LIBVIPS_VERSION}" \
|
|
71
|
+
--prefix=/opt/vips --libdir=lib --buildtype=release --auto-features=disabled \
|
|
72
|
+
-Djpeg=enabled -Dpng=enabled -Dwebp=enabled -Dheif=enabled -Dtiff=enabled \
|
|
73
|
+
-Dlcms=enabled -Dzlib=enabled -Dexif=enabled \
|
|
74
|
+
-Dexamples=false -Dppm=false -Danalyze=false -Dradiance=false; \
|
|
75
|
+
meson compile -C /tmp/vips-build -j 2; \
|
|
76
|
+
meson install -C /tmp/vips-build; \
|
|
77
|
+
test "$(pkg-config --modversion vips-cpp)" = "${LIBVIPS_VERSION}"; \
|
|
78
|
+
rm -rf /tmp/vips-build "/tmp/vips-${LIBVIPS_VERSION}" /tmp/vips.tar.xz
|
|
79
|
+
|
|
80
|
+
# ===================================================================
|
|
81
|
+
# Stage 2: Production dependencies + CPU-safe sharp addon
|
|
82
|
+
# ===================================================================
|
|
83
|
+
# Built FROM the libvips stage because compiling sharp's addon needs the
|
|
84
|
+
# compilers and every codec -dev package that vips-cpp.pc references. Only
|
|
85
|
+
# /app/node_modules leaves this stage.
|
|
86
|
+
FROM libvips AS dependencies
|
|
14
87
|
|
|
15
88
|
# Install production dependencies only
|
|
16
89
|
WORKDIR /app
|
|
@@ -29,7 +102,7 @@ RUN --mount=type=cache,target=/root/.npm \
|
|
|
29
102
|
fi
|
|
30
103
|
|
|
31
104
|
# Add the Prisma CLI to the production node_modules so the runtime image can
|
|
32
|
-
# run `npx prisma migrate deploy` at container start (see CMD in stage
|
|
105
|
+
# run `npx prisma migrate deploy` at container start (see CMD in stage 4).
|
|
33
106
|
# `prisma` is a devDependency (correct for local dev), so the prod-only install
|
|
34
107
|
# above does NOT include it — without this step, npx would try to download the
|
|
35
108
|
# CLI from the registry on every container boot. Pinned to the exact
|
|
@@ -40,10 +113,60 @@ RUN --mount=type=cache,target=/root/.npm \
|
|
|
40
113
|
RUN --mount=type=cache,target=/root/.npm npm install --no-save --no-audit --no-fund \
|
|
41
114
|
"prisma@$(node -p "require('@prisma/client/package.json').version")"
|
|
42
115
|
|
|
116
|
+
# Build sharp's native addon against /opt/vips. sharp loads
|
|
117
|
+
# node_modules/sharp/src/build/Release/sharp-<platform>-<version>.node BEFORE
|
|
118
|
+
# its prebuilt addon and without the prebuilt's x86-64-v2 check, so this file
|
|
119
|
+
# is what makes sharp work on older CPUs. Keep this block identical in
|
|
120
|
+
# server/Dockerfile and client/Dockerfile.
|
|
121
|
+
# - It runs after every npm install above, so it targets the sharp version
|
|
122
|
+
# actually installed (read from its package.json, never hard-coded).
|
|
123
|
+
# - SHARP_FORCE_GLOBAL_LIBVIPS makes sharp skip its own libvips version
|
|
124
|
+
# check, so the block checks sharp's "config.libvips" range itself.
|
|
125
|
+
# - node-addon-api and node-gyp are build tools sharp requires but does not
|
|
126
|
+
# install. They go to /tmp/sharp-build (exact pins inside sharp's declared
|
|
127
|
+
# ranges; --ignore-scripts, since neither needs an install script and their
|
|
128
|
+
# unlocked transitive packages must not run code here) and never reach the
|
|
129
|
+
# lockfile or the runtime image.
|
|
130
|
+
# - npm_config_nodedir points node-gyp at this image's own Node headers, so
|
|
131
|
+
# nothing is downloaded and the addon matches the runtime Node exactly.
|
|
132
|
+
# - Intermediate build files are dropped; only the .node file stays, also
|
|
133
|
+
# staged in /opt/sharp-addon for stages that copy it explicitly.
|
|
134
|
+
RUN --mount=type=cache,target=/root/.npm \
|
|
135
|
+
set -eu; \
|
|
136
|
+
npm install --prefix /tmp/sharp-build --no-save --no-audit --no-fund --ignore-scripts \
|
|
137
|
+
node-addon-api@8.9.2 node-gyp@12.4.0; \
|
|
138
|
+
if [ ! -d node_modules/sharp ]; then \
|
|
139
|
+
echo "ERROR: node_modules/sharp is missing. If this app no longer uses sharp, delete this build step and the libvips stage." >&2; \
|
|
140
|
+
exit 1; \
|
|
141
|
+
fi; \
|
|
142
|
+
cd node_modules/sharp; \
|
|
143
|
+
version="$(node -p "require('./package.json').version")"; \
|
|
144
|
+
need="$(node -p "require('./package.json').config.libvips")"; \
|
|
145
|
+
have="$(pkg-config --modversion vips-cpp)"; \
|
|
146
|
+
if ! node -e "process.exit(require('semver').satisfies(process.argv[1], process.argv[2]) ? 0 : 1)" "$have" "$need"; then \
|
|
147
|
+
echo "ERROR: sharp ${version} needs libvips ${need}, but this image builds libvips ${have}. Raise LIBVIPS_VERSION and LIBVIPS_SHA256 in BOTH server/Dockerfile and client/Dockerfile." >&2; \
|
|
148
|
+
exit 1; \
|
|
149
|
+
fi; \
|
|
150
|
+
if [ "$(uname -m)" = "x86_64" ]; then \
|
|
151
|
+
export CFLAGS="-march=x86-64 -mtune=generic" CXXFLAGS="-march=x86-64 -mtune=generic"; \
|
|
152
|
+
fi; \
|
|
153
|
+
SHARP_FORCE_GLOBAL_LIBVIPS=1 npm_config_nodedir=/usr/local \
|
|
154
|
+
NODE_PATH=/tmp/sharp-build/node_modules PATH="/tmp/sharp-build/node_modules/.bin:$PATH" \
|
|
155
|
+
node install/build.js; \
|
|
156
|
+
addon="sharp-linuxmusl-$(node -p "process.arch")-${version}.node"; \
|
|
157
|
+
test -f "src/build/Release/${addon}"; \
|
|
158
|
+
mkdir -p /opt/sharp-addon; \
|
|
159
|
+
cp "src/build/Release/${addon}" /opt/sharp-addon/; \
|
|
160
|
+
rm -rf src/build /tmp/sharp-build; \
|
|
161
|
+
mkdir -p src/build/Release; \
|
|
162
|
+
cp "/opt/sharp-addon/${addon}" src/build/Release/
|
|
163
|
+
|
|
43
164
|
# ===================================================================
|
|
44
|
-
# Stage
|
|
165
|
+
# Stage 3: Build (compile TypeScript)
|
|
45
166
|
# ===================================================================
|
|
46
|
-
|
|
167
|
+
# Independent of stages 1-2 (tsc and prisma generate never load sharp), so
|
|
168
|
+
# BuildKit runs it in parallel with the libvips compile.
|
|
169
|
+
FROM node:24-alpine@sha256:44fce696f38ba8c987c4db89954051000bbd05ffd8d8659ba15ed4a8602e937f AS builder
|
|
47
170
|
|
|
48
171
|
# Override NODE_ENV to ensure devDependencies are installed.
|
|
49
172
|
# Coolify may inject NODE_ENV=production as a build arg, which causes npm ci
|
|
@@ -74,12 +197,29 @@ RUN npx prisma generate
|
|
|
74
197
|
RUN npm run build
|
|
75
198
|
|
|
76
199
|
# ===================================================================
|
|
77
|
-
# Stage
|
|
200
|
+
# Stage 4: Production Runtime
|
|
78
201
|
# ===================================================================
|
|
79
|
-
FROM node:
|
|
202
|
+
FROM node:24-alpine@sha256:44fce696f38ba8c987c4db89954051000bbd05ffd8d8659ba15ed4a8602e937f AS production
|
|
80
203
|
|
|
81
|
-
#
|
|
82
|
-
|
|
204
|
+
# dumb-init: proper signal handling.
|
|
205
|
+
# curl: Coolify's own HTTP health check runs curl (then BusyBox wget) inside
|
|
206
|
+
# the container. Without curl it falls back to wget, which tries only the
|
|
207
|
+
# first address `localhost` resolves to (often IPv6 ::1, while the app
|
|
208
|
+
# listens on IPv4), so Coolify marks a healthy app unhealthy and rolls the
|
|
209
|
+
# deploy back. curl tries both addresses.
|
|
210
|
+
# The rest are the shared libraries our libvips links against (one per -dev
|
|
211
|
+
# package in stage 1). Alpine ships WebP's mux/demux libraries separately from
|
|
212
|
+
# libwebp and sharp fails to load without them; the libheif plugins decode
|
|
213
|
+
# HEIC (libde265) and AVIF (aom).
|
|
214
|
+
RUN apk add --no-cache \
|
|
215
|
+
dumb-init curl \
|
|
216
|
+
glib expat libjpeg-turbo libpng libwebp libwebpmux libwebpdemux \
|
|
217
|
+
tiff lcms2 libheif libheif-libde265 libheif-aom libexif
|
|
218
|
+
|
|
219
|
+
# The libvips built in stage 1. sharp's addon links to it without an rpath,
|
|
220
|
+
# so the dynamic loader is told where it lives.
|
|
221
|
+
COPY --from=libvips /opt/vips/lib /opt/vips/lib
|
|
222
|
+
ENV LD_LIBRARY_PATH=/opt/vips/lib
|
|
83
223
|
|
|
84
224
|
# Create non-root user for security
|
|
85
225
|
RUN addgroup -g 1001 -S nodejs && \
|
|
@@ -93,8 +233,9 @@ WORKDIR /app
|
|
|
93
233
|
ENV NODE_ENV=production
|
|
94
234
|
|
|
95
235
|
# Copy production dependencies from dependencies stage.
|
|
96
|
-
# Includes the Prisma CLI (+ its engines) installed in stage
|
|
97
|
-
# CMD below needs for `npx prisma migrate deploy
|
|
236
|
+
# Includes the Prisma CLI (+ its engines) installed in stage 2, which the
|
|
237
|
+
# CMD below needs for `npx prisma migrate deploy`, and the CPU-safe sharp
|
|
238
|
+
# addon built there.
|
|
98
239
|
COPY --from=dependencies --chown=nodejs:nodejs /app/node_modules ./node_modules
|
|
99
240
|
|
|
100
241
|
# Copy generated Prisma client from builder stage (not present in prod deps)
|
|
@@ -107,6 +248,10 @@ COPY --from=builder --chown=nodejs:nodejs /app/package.json ./package.json
|
|
|
107
248
|
# Copy Prisma files (needed for migrations)
|
|
108
249
|
COPY --from=builder --chown=nodejs:nodejs /app/prisma ./prisma
|
|
109
250
|
|
|
251
|
+
# Build-time sharp and HEIC proofs (see each script's header).
|
|
252
|
+
COPY --chown=nodejs:nodejs scripts/check-sharp.mjs ./scripts/check-sharp.mjs
|
|
253
|
+
COPY --chown=nodejs:nodejs scripts/check-heic.mjs ./scripts/check-heic.mjs
|
|
254
|
+
|
|
110
255
|
# Create BOTH upload tiers with correct ownership (must be before USER nodejs).
|
|
111
256
|
# public/ is served as static files at /uploads/; private/ is served ONLY via
|
|
112
257
|
# the auth-gated route GET /api/v1/files/:filename. See file-storage.service.ts.
|
|
@@ -115,15 +260,25 @@ RUN mkdir -p /app/uploads/public /app/uploads/private && chown -R nodejs:nodejs
|
|
|
115
260
|
# Switch to non-root user
|
|
116
261
|
USER nodejs
|
|
117
262
|
|
|
263
|
+
# Prove sharp loads our addon and converts images in THIS image, as the
|
|
264
|
+
# runtime user, before the image can ship. A failure fails the build instead
|
|
265
|
+
# of crashing the server on its first upload or at start.
|
|
266
|
+
RUN node scripts/check-sharp.mjs
|
|
267
|
+
# The avatar upload accepts iPhone HEIC photos: prove a real HEVC-coded HEIC
|
|
268
|
+
# decodes here (the HEIF flag check-sharp sees is also true for AVIF-only builds).
|
|
269
|
+
RUN node scripts/check-heic.mjs
|
|
270
|
+
|
|
118
271
|
# Expose port (matches default PORT in env.ts; override via PORT env var)
|
|
119
272
|
EXPOSE 8000
|
|
120
273
|
|
|
121
274
|
# Health check for container orchestration.
|
|
275
|
+
# 127.0.0.1, never localhost: the server listens on IPv4 (HOST=0.0.0.0), and
|
|
276
|
+
# localhost can resolve to IPv6 ::1 first, which would refuse the probe.
|
|
122
277
|
# start-period is 60s because CMD runs `prisma migrate deploy` BEFORE the server
|
|
123
278
|
# starts listening — a slow migration must not be counted as unhealthy, or
|
|
124
279
|
# Coolify restart-loops the container mid-migration.
|
|
125
280
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=60s --retries=3 \
|
|
126
|
-
CMD node -e "const p=process.env.PORT||8000;require('http').get('http://
|
|
281
|
+
CMD node -e "const p=process.env.PORT||8000;require('http').get('http://127.0.0.1:'+p+'/api/v1/live',(r)=>{process.exit(r.statusCode===200?0:1)})"
|
|
127
282
|
|
|
128
283
|
# Use dumb-init to handle signals properly
|
|
129
284
|
ENTRYPOINT ["dumb-init", "--"]
|
|
@@ -133,7 +288,14 @@ ENTRYPOINT ["dumb-init", "--"]
|
|
|
133
288
|
# (it isn't from CI, which is why migrations belong here and not in a workflow).
|
|
134
289
|
# `migrate deploy` is idempotent: it applies only pending migrations and exits 0
|
|
135
290
|
# when there is nothing to do. Requirements, all satisfied above:
|
|
136
|
-
# - prisma CLI in ./node_modules (installed in stage
|
|
291
|
+
# - prisma CLI in ./node_modules (installed in stage 2, copied with node_modules)
|
|
137
292
|
# - ./prisma/schema.prisma + ./prisma/migrations (copied from builder)
|
|
293
|
+
# Refuses to start when the image holds NO migration at all (prisma/migrations
|
|
294
|
+
# deleted, never committed, or the project switched to `db push`): `migrate
|
|
295
|
+
# deploy` would then succeed with nothing to apply and the server would boot
|
|
296
|
+
# healthy on a database without tables, failing only at the first real query.
|
|
297
|
+
# Exiting instead fails the Coolify deploy with this message in the logs, and
|
|
298
|
+
# the previous version keeps serving. New projects ship 0_init, so this only
|
|
299
|
+
# fires when that safety net was removed.
|
|
138
300
|
# `exec` replaces the shell so node receives signals from dumb-init directly.
|
|
139
|
-
CMD ["sh", "-c", "npx prisma migrate deploy && exec node dist/server.js"]
|
|
301
|
+
CMD ["sh", "-c", "if ! ls prisma/migrations/*/migration.sql >/dev/null 2>&1; then echo 'Refusing to start: no Prisma migrations in this image (prisma/migrations/*/migration.sql), so the database would have no tables. Create them with npm run prisma:migrate:dev and commit prisma/migrations.' >&2; exit 1; fi; npx prisma migrate deploy && exec node dist/server.js"]
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# Server
|
|
2
|
+
|
|
3
|
+
The API: Fastify 5 and TypeScript, Prisma 6 on MySQL 8.4, Redis 7 (ioredis), Zod, Pino, Vitest. Every route lives
|
|
4
|
+
under `/api/v1`.
|
|
5
|
+
|
|
6
|
+
## Layout
|
|
7
|
+
|
|
8
|
+
```text
|
|
9
|
+
server/
|
|
10
|
+
├── src/
|
|
11
|
+
│ ├── app.ts builds the app: plugins, security hooks, routes
|
|
12
|
+
│ ├── server.ts starts it: database, Redis, jobs, graceful shutdown
|
|
13
|
+
│ ├── config/ env.ts (every setting, validated at boot), rate-limit.config.ts (every limit)
|
|
14
|
+
│ ├── modules/<domain>/ auth, users, admin, files: routes → controller → service → repo (+ schemas)
|
|
15
|
+
│ ├── libs/ shared pieces: auth, cookies, client IP, rate limiting, IP blocks, email,
|
|
16
|
+
│ │ HTTP clients, storage, logging, health, shutdown, validation errors
|
|
17
|
+
│ ├── jobs/ background jobs and their scheduler
|
|
18
|
+
│ ├── scripts/ make-admin
|
|
19
|
+
│ ├── shared/ response helpers, error classes, shared types
|
|
20
|
+
│ └── test/ integration test setup and helpers
|
|
21
|
+
├── prisma/ schema.prisma and migrations/ (starting with 0_init)
|
|
22
|
+
├── docs/ deployment, rate limits, testing, logging
|
|
23
|
+
├── postman/ a collection of every route
|
|
24
|
+
├── uploads/ local uploads (a volume at /app/uploads in production)
|
|
25
|
+
├── docker-compose.yml local MySQL and Redis (+ phpMyAdmin and Redis Commander with --profile tools)
|
|
26
|
+
└── Dockerfile the production image
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Commands
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm run dev # the API with reload, on PORT (8000)
|
|
33
|
+
npm test # unit tests, no database
|
|
34
|
+
npm run test:coverage # unit tests with the coverage floor (vitest.config.ts)
|
|
35
|
+
npm run test:integration # every route against a real MySQL and Redis (docs/testing.md)
|
|
36
|
+
npm run lint && npm run typecheck
|
|
37
|
+
npm run build && npm start # production build, then run it
|
|
38
|
+
|
|
39
|
+
npm run prisma:migrate:dev # after changing schema.prisma: create and apply a migration
|
|
40
|
+
npm run prisma:studio # browse the database
|
|
41
|
+
npm run make-admin -- you@example.com
|
|
42
|
+
npm run docker:up | docker:tools | docker:down
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Routes
|
|
46
|
+
|
|
47
|
+
| Route | Who may call it |
|
|
48
|
+
|---|---|
|
|
49
|
+
| `POST /auth/register`, `/auth/login`, `/auth/refresh`, `/auth/logout` | anyone |
|
|
50
|
+
| `POST /auth/forgot-password`, `/auth/reset-password` | anyone (the emailed link carries the token) |
|
|
51
|
+
| `GET /auth/me`, `GET /auth/sessions`, `POST /auth/logout-all` | signed in |
|
|
52
|
+
| `PATCH /users/me`, `PATCH /users/me/password`, `DELETE /users/me` | signed in |
|
|
53
|
+
| `POST /users/avatar`, `DELETE /users/avatar` | signed in |
|
|
54
|
+
| `PATCH /users/:userId`, `/users/:userId/password`, `DELETE /users/:userId`, `POST` and `DELETE /users/:userId/avatar` | the owner, or an admin |
|
|
55
|
+
| `GET /files/:filename` | the file's owner |
|
|
56
|
+
| `GET /admin/stats`, `/admin/health`, `/admin/client-ip` | admin |
|
|
57
|
+
| `GET /admin/users`, `GET /admin/users/:userId`, `PATCH /admin/users/:userId/status` and `/role`, `POST /admin/users/:userId/restore` | admin |
|
|
58
|
+
| `GET /admin/sessions`, `DELETE /admin/sessions/:sessionId` | admin |
|
|
59
|
+
| `GET /admin/blocked-ips`, `POST /admin/blocked-ips`, `DELETE /admin/blocked-ips/:ip` | admin |
|
|
60
|
+
| `GET /health`, `/ready`, `/live` | anyone (never rate limited) |
|
|
61
|
+
|
|
62
|
+
With the email-verification module: `POST /auth/send-verification` and `POST /auth/verify-account`. Public avatars are
|
|
63
|
+
served from `/uploads/`. `postman/collection.json` has every route with example bodies.
|
|
64
|
+
|
|
65
|
+
Every answer has one shape:
|
|
66
|
+
|
|
67
|
+
```json
|
|
68
|
+
{ "success": true, "message": "Profile updated", "data": { } }
|
|
69
|
+
{ "success": false, "error": { "code": "VALIDATION_FAILED", "message": "Please check these fields: email", "fields": [{ "field": "email", "message": "Invalid email address" }] } }
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Wrong input is always `422 VALIDATION_FAILED` with `fields`; "slow down" answers (429, and 403 `IP_BLOCKED`) carry
|
|
73
|
+
`retryAfterSeconds`; an unexpected error is a generic 500, logged in full here.
|
|
74
|
+
|
|
75
|
+
## Adding a feature
|
|
76
|
+
|
|
77
|
+
1. A module in `src/modules/<domain>/`: `<domain>.routes.ts` (paths and Zod schemas), `.controller.ts` (HTTP in and
|
|
78
|
+
out), `.service.ts` (business rules and who may do what), `.repo.ts` (Prisma queries only), `.schemas.ts`.
|
|
79
|
+
2. Register its routes in `src/app.ts` under the `/api/v1` prefix. A new route gets the default limit of 200 per
|
|
80
|
+
minute; give it a `RATE_LIMITS` entry in `src/config/rate-limit.config.ts` only when it needs another number.
|
|
81
|
+
3. A schema change: edit `prisma/schema.prisma`, then `npm run prisma:migrate:dev`. Migrations run while the old
|
|
82
|
+
version still serves, so never drop, rename, retype or tighten a column in the same deploy as the code that
|
|
83
|
+
stops using it (`docs/deployment.md`).
|
|
84
|
+
4. Tests: unit tests next to the code (`__tests__/`), and the route's contract (401/403, 422, success) in
|
|
85
|
+
`src/__tests__/routes.integration.test.ts`. That file fails when a registered route is never called.
|
|
86
|
+
5. Fetching a URL a user gave you: `safeHttpClient` (`src/libs/safe-http.ts`), never `fetch`. Recurring work:
|
|
87
|
+
`startRecurringJob` (`src/jobs/schedule.ts`), never `setInterval`.
|
|
88
|
+
|
|
89
|
+
`AGENTS.md` in this folder has the full set of rules.
|
|
90
|
+
|
|
91
|
+
## Configuration
|
|
92
|
+
|
|
93
|
+
`src/config/env.ts` validates every setting at boot and stops with the reason when one is wrong. `.env.example`
|
|
94
|
+
documents development, `.env.example.production` documents production. In production the server also refuses to
|
|
95
|
+
start:
|
|
96
|
+
|
|
97
|
+
- with a placeholder `JWT_SECRET`, or a `CORS_ORIGIN` that is not a list of bare origins;
|
|
98
|
+
- without `REDIS_URL`, or when Redis is not reachable within 10 s (the limits, the lockout and the IP blocks would
|
|
99
|
+
be off);
|
|
100
|
+
- without any migration in the image;
|
|
101
|
+
- with the placeholder Resend key or sender, when email is set up.
|
|
102
|
+
|
|
103
|
+
## Guides
|
|
104
|
+
|
|
105
|
+
- `docs/deployment.md`: what a deploy does, safe migrations, rollback, build time.
|
|
106
|
+
- `docs/rate-limits.md`: every limit, the login lockout, IP blocks, email caps, overrides, Redis outages.
|
|
107
|
+
- `docs/testing.md`: the unit and integration suites, writing tests, CI, coverage.
|
|
108
|
+
- `docs/logging.md`: request logs, redaction, health checks.
|
|
@@ -14,7 +14,9 @@ name: {{PROJECT_NAME}}
|
|
|
14
14
|
|
|
15
15
|
services:
|
|
16
16
|
mysql:
|
|
17
|
-
|
|
17
|
+
# 8.4 is MySQL's long-term release (8.0 reached end of life in April 2026);
|
|
18
|
+
# keep it on the version production runs, so local behaves the same.
|
|
19
|
+
image: mysql:8.4
|
|
18
20
|
container_name: {{PROJECT_NAME}}-mysql
|
|
19
21
|
restart: unless-stopped
|
|
20
22
|
# Local-dev resource bounds. mem_limit is a cgroup ceiling (a runaway container is
|
|
@@ -26,7 +28,7 @@ services:
|
|
|
26
28
|
- --innodb-buffer-pool-size=128M # default is fine for dev; keeps RSS small
|
|
27
29
|
- --performance-schema=OFF # saves ~hundreds of MB on its own
|
|
28
30
|
- --skip-name-resolve # no reverse-DNS per connection (faster, leaner)
|
|
29
|
-
- --max-connections=60 # well above
|
|
31
|
+
- --max-connections=60 # well above Prisma's pool (connection_limit in DATABASE_URL) + headroom
|
|
30
32
|
- --innodb-flush-log-at-trx-commit=2 # dev durability/throughput trade — not for prod
|
|
31
33
|
ports:
|
|
32
34
|
- '127.0.0.1:${MYSQL_PORT:-{{MYSQL_PORT}}}:3306'
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Deploying
|
|
2
|
+
|
|
3
|
+
The server and the client deploy as two Coolify applications from their own Dockerfiles. The setup checklist is in
|
|
4
|
+
the main README ("Deploying to Coolify"); this page explains what a deploy does and the rules that follow from it.
|
|
5
|
+
|
|
6
|
+
## What a deploy does
|
|
7
|
+
|
|
8
|
+
1. Coolify builds the image. The server's build compiles libvips (see "Build time" below), installs the exact
|
|
9
|
+
lockfile, builds the code and proves that sharp works in the final image.
|
|
10
|
+
2. Coolify starts a **new** container next to the one that is serving. Its start command:
|
|
11
|
+
- refuses to start when the image has no Prisma migrations;
|
|
12
|
+
- runs `prisma migrate deploy`, which applies the migrations the database does not have yet. A database it cannot
|
|
13
|
+
reach stops the start here;
|
|
14
|
+
- starts the server. In production the server also refuses to start when it cannot reach Redis within 10 s.
|
|
15
|
+
3. Coolify waits for the image's Docker `HEALTHCHECK` (server: `/api/v1/live`, client: `/healthz`), then sends traffic
|
|
16
|
+
to the new container and stops the old one. The server finishes its in-flight requests and closes within 8 s
|
|
17
|
+
(Docker's limit is 10 s).
|
|
18
|
+
|
|
19
|
+
If the new container never becomes healthy (a failed migration, no Redis, a crash), Coolify keeps the old container
|
|
20
|
+
serving, and the deploy shows as failed.
|
|
21
|
+
|
|
22
|
+
## Migrations: expand, deploy, contract
|
|
23
|
+
|
|
24
|
+
Between steps 2 and 3 the **old** code runs against the **new** schema, and nothing can roll a migration back: a
|
|
25
|
+
redeploy of an older commit only runs `migrate deploy` again, which moves forward. So every migration must work with
|
|
26
|
+
the code that is still running:
|
|
27
|
+
|
|
28
|
+
1. **Expand.** Add tables, columns (nullable or with a default) and indexes. Deploy.
|
|
29
|
+
2. **Move the code** to the new shape, and backfill data if needed. Deploy.
|
|
30
|
+
3. **Contract**, in a later deploy: drop or rename only what no running code uses any more.
|
|
31
|
+
|
|
32
|
+
Never in the same deploy as the code that stops using it:
|
|
33
|
+
|
|
34
|
+
- drop a column or a table;
|
|
35
|
+
- rename one: Prisma writes a rename as drop + add, losing the data unless you edit the migration. To rename only in
|
|
36
|
+
the code, keep the database name with `@map` / `@@map`;
|
|
37
|
+
- change a column's type, or make it required without a default.
|
|
38
|
+
|
|
39
|
+
The old container would read or write the old shape for the length of the switch and fail.
|
|
40
|
+
|
|
41
|
+
Before a risky migration, back up the database (on Coolify: the database's Backups tab, run a backup now). To undo
|
|
42
|
+
one, write a new forward migration, or restore the backup, losing what was written since.
|
|
43
|
+
|
|
44
|
+
## Rolling back the code
|
|
45
|
+
|
|
46
|
+
Redeploy the previous commit, or use Coolify's rollback on the Deployments tab. That is safe as long as the schema
|
|
47
|
+
still suits the old code, which is what expand / deploy / contract guarantees.
|
|
48
|
+
|
|
49
|
+
## Build time: libvips
|
|
50
|
+
|
|
51
|
+
Both images compile libvips, the image library behind sharp, from source. sharp's prebuilt binaries need x86-64-v2
|
|
52
|
+
CPU features that some VPS CPUs lack; the images build libvips for baseline x86-64, so they run on any x86-64 server.
|
|
53
|
+
|
|
54
|
+
Uncached, the compile takes about 6 to 7 minutes of the server's CPU per application (measured on a shared VPS:
|
|
55
|
+
6.2 to 6.6 minutes, about 8.5 minutes for the whole server deploy), and other apps on the same server can slow down
|
|
56
|
+
meanwhile. Docker keeps the compiled stage in that server's build cache, and a later deploy that reuses it finishes
|
|
57
|
+
in 1 to 2 minutes. Expect the long build on the first deploy of each application to a server (the two do not share
|
|
58
|
+
it on Coolify), after a change to a build-time variable (the client's `NEXT_PUBLIC_*` values), after the server's
|
|
59
|
+
build cache was cleaned up, and when the base image or `LIBVIPS_VERSION` changes. Other deploys may hit the cache,
|
|
60
|
+
but do not count on it.
|
|
61
|
+
|
|
62
|
+
The fix for later: build the libvips stage once into a base image in a registry (it needs CI to publish it) and
|
|
63
|
+
start both Dockerfiles from it. Until then, deploy when the server is quiet if its other apps are sensitive to load.
|
|
@@ -2,20 +2,46 @@
|
|
|
2
2
|
|
|
3
3
|
## Overview
|
|
4
4
|
|
|
5
|
-
The server uses a single-line request/response logging system built on Pino, providing
|
|
5
|
+
The server uses a single-line request/response logging system built on Pino, providing informative logs for every API request, color-coded in a development terminal.
|
|
6
6
|
|
|
7
7
|
## Log Format
|
|
8
8
|
|
|
9
9
|
Each completed request produces a single log line:
|
|
10
10
|
|
|
11
11
|
```
|
|
12
|
-
GET /api/v1/auth/me 200 OK (12ms)
|
|
12
|
+
GET /api/v1/auth/me 200 OK (12ms) [ip=203.0.113.7 reqId=<uuid>]
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
Format: `METHOD
|
|
15
|
+
Format: `METHOD PATH STATUS STATUS_MESSAGE (DURATION) [ip=CLIENT_IP reqId=REQUEST_ID]`
|
|
16
|
+
|
|
17
|
+
`PATH` is the URL without its query string or fragment: queries carry search terms and email
|
|
18
|
+
addresses (`/admin/users?search=jane@example.com`), and links from emails carry tokens, so no
|
|
19
|
+
request log keeps them. Route params (ids) stay.
|
|
20
|
+
|
|
21
|
+
In production (JSON logs) the same parts are also separate fields on the line, so log search can
|
|
22
|
+
filter on them instead of parsing `msg`:
|
|
23
|
+
|
|
24
|
+
```json
|
|
25
|
+
{"level":30,"time":1760918400000,"reqId":"<uuid>","method":"GET","path":"/api/v1/auth/me","statusCode":200,"durationMs":12,"ip":"203.0.113.7","msg":"GET /api/v1/auth/me 200 OK (12ms) [ip=203.0.113.7 reqId=<uuid>]"}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
| Field | Meaning |
|
|
29
|
+
|---|---|
|
|
30
|
+
| `reqId` | the request id (also the `x-request-id` response header) |
|
|
31
|
+
| `method` | HTTP method |
|
|
32
|
+
| `path` | URL without query or fragment |
|
|
33
|
+
| `statusCode` | response status |
|
|
34
|
+
| `durationMs` | time from the route's preHandler to the response, in ms |
|
|
35
|
+
| `ip` | resolved client IP (Cloudflare or proxy aware, `src/libs/client-ip.ts`) |
|
|
36
|
+
|
|
37
|
+
Development logs (pino-pretty) show only the line, without the field list under it.
|
|
38
|
+
|
|
39
|
+
`REQUEST_ID` is also sent back as the `x-request-id` response header. An inbound `X-Request-Id` (from a proxy in front) is kept only when it is at most 128 letters, digits, `.`, `_` or `-`; anything else gets a fresh UUID (`src/libs/request-id.ts`).
|
|
16
40
|
|
|
17
41
|
## Color Coding
|
|
18
42
|
|
|
43
|
+
Colors are added only when stdout is a terminal and `NODE_ENV` is not `production` (`shouldColorize()` in `src/libs/requestLogger.ts`). Production and container logs are plain text: escape codes would otherwise show up as `\u001b[34m` junk inside the JSON `msg` field.
|
|
44
|
+
|
|
19
45
|
### HTTP Methods (ANSI colors)
|
|
20
46
|
- **Blue**: GET (read operations)
|
|
21
47
|
- **Green**: POST (create operations)
|
|
@@ -44,6 +70,42 @@ Health check endpoints are excluded from logging to reduce noise:
|
|
|
44
70
|
- `/api/v1/ready`
|
|
45
71
|
- `/api/v1/live`
|
|
46
72
|
|
|
73
|
+
## Health checks
|
|
74
|
+
|
|
75
|
+
Public, never rate limited or IP-blocked (`src/libs/health-routes.ts`):
|
|
76
|
+
|
|
77
|
+
| Route | Answer | Use |
|
|
78
|
+
|---|---|---|
|
|
79
|
+
| `GET /api/v1/health` | `{ status }`: `healthy` / `degraded` → 200, `unhealthy` → 503 | uptime monitors |
|
|
80
|
+
| `GET /api/v1/ready` | `{ ready, timestamp }`, 503 while the database is down | load balancers |
|
|
81
|
+
| `GET /api/v1/live` | `{ alive, timestamp }` | the Docker `HEALTHCHECK` |
|
|
82
|
+
|
|
83
|
+
`unhealthy` means the database is down. `degraded` means the database is slow (100 ms or more),
|
|
84
|
+
Redis is down or slow (so rate limits and IP blocks are off), or the heap is above 85% of its limit.
|
|
85
|
+
|
|
86
|
+
The public answer never says which check failed. Admins get the full report at
|
|
87
|
+
`GET /api/v1/admin/health`: per check status and timing, memory (heap used and limit, RSS),
|
|
88
|
+
uptime, `checkedAt`, `version` (from `package.json`) and `commit` (the first 12 characters of
|
|
89
|
+
`SOURCE_COMMIT` when the environment provides it, otherwise `null`).
|
|
90
|
+
|
|
91
|
+
The database and Redis are checked at most once per 5 s; every probe in between gets the same
|
|
92
|
+
answer (`checkedAt` says when it was taken). A failing check logs the error at `error`
|
|
93
|
+
(database) or `warn` (Redis).
|
|
94
|
+
|
|
95
|
+
## Secrets never reach the logs
|
|
96
|
+
|
|
97
|
+
Pino's redaction (`src/libs/logger.ts`) removes these keys wherever they appear in a logged object, up to three
|
|
98
|
+
levels deep, and the secret headers of a logged request:
|
|
99
|
+
|
|
100
|
+
```text
|
|
101
|
+
keys password newPassword currentPassword passwordHash token accessToken refreshToken
|
|
102
|
+
access_token refresh_token secret clientSecret apiKey api_key authorization cookie
|
|
103
|
+
headers authorization cookie set-cookie proxy-authorization x-api-key x-origin-verify
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Request lines never contain the query string, outbound request logs show URLs without keys or `user:password@`,
|
|
107
|
+
and email addresses in email logs are masked. Logging a secret under another name is still on you: name it plainly.
|
|
108
|
+
|
|
47
109
|
## Configuration
|
|
48
110
|
|
|
49
111
|
### Development
|
|
@@ -51,7 +113,7 @@ Health check endpoints are excluded from logging to reduce noise:
|
|
|
51
113
|
- Set `LOG_LEVEL=debug` in `.env` for verbose output
|
|
52
114
|
|
|
53
115
|
### Production
|
|
54
|
-
- Uses structured JSON logs (Pino default)
|
|
116
|
+
- Uses structured JSON logs (Pino default), with no color codes
|
|
55
117
|
- Optimized for log aggregation tools (ELK, CloudWatch, Datadog)
|
|
56
118
|
- Set `LOG_LEVEL=info` or `LOG_LEVEL=warn` in `.env`
|
|
57
119
|
|