create-mercato-app 0.6.5-develop.5116.1.f0af9e5080 → 0.6.5-develop.5155.1.148d10a46d
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/package.json +2 -2
- package/template/.railwayignore +29 -0
- package/template/Dockerfile +1 -0
- package/template/gitignore +2 -1
- package/template/railway.toml +10 -0
- package/template/railway.worker.toml +8 -0
- package/template/scripts/railway-start.sh +15 -0
- package/template/scripts/railway-worker.sh +15 -0
- package/template/src/app/api/healthz/__tests__/route.test.ts +115 -0
- package/template/src/app/api/healthz/route.ts +118 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-mercato-app",
|
|
3
|
-
"version": "0.6.5-develop.
|
|
3
|
+
"version": "0.6.5-develop.5155.1.148d10a46d",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Create a new Open Mercato application",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"tar": "^7.5.16"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@types/node": "^25.9.
|
|
24
|
+
"@types/node": "^25.9.2",
|
|
25
25
|
"esbuild": "^0.28.0",
|
|
26
26
|
"tsx": "^4.22.4",
|
|
27
27
|
"typescript": "^6.0.3"
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
.git/
|
|
2
|
+
.railway/
|
|
3
|
+
.mercato/railway.json*
|
|
4
|
+
.next/
|
|
5
|
+
.turbo/
|
|
6
|
+
.yarn/cache/
|
|
7
|
+
.yarn/unplugged/
|
|
8
|
+
node_modules/
|
|
9
|
+
coverage/
|
|
10
|
+
test-results/
|
|
11
|
+
playwright-report/
|
|
12
|
+
storage/
|
|
13
|
+
data/
|
|
14
|
+
*.db
|
|
15
|
+
*.db-shm
|
|
16
|
+
*.db-wal
|
|
17
|
+
*.sqlite
|
|
18
|
+
*.sqlite3
|
|
19
|
+
.env
|
|
20
|
+
.env.*
|
|
21
|
+
!.env.example
|
|
22
|
+
*.pem
|
|
23
|
+
*.key
|
|
24
|
+
id_rsa
|
|
25
|
+
id_ed25519
|
|
26
|
+
npm-debug.log*
|
|
27
|
+
yarn-debug.log*
|
|
28
|
+
yarn-error.log*
|
|
29
|
+
.pnpm-debug.log*
|
package/template/Dockerfile
CHANGED
|
@@ -98,6 +98,7 @@ COPY --from=builder /app/next.config.ts ./next.config.ts
|
|
|
98
98
|
COPY --from=builder /app/postcss.config.mjs ./postcss.config.mjs
|
|
99
99
|
COPY --from=builder /app/components.json ./components.json
|
|
100
100
|
COPY --from=builder /app/tsconfig.json ./tsconfig.json
|
|
101
|
+
COPY --from=builder /app/scripts ./scripts
|
|
101
102
|
COPY docker/scripts/init-or-migrate.sh /app/docker/scripts/init-or-migrate.sh
|
|
102
103
|
RUN chmod +x /app/docker/scripts/init-or-migrate.sh
|
|
103
104
|
|
package/template/gitignore
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
set -euo pipefail
|
|
3
|
+
|
|
4
|
+
export CACHE_STRATEGY="${CACHE_STRATEGY:-redis}"
|
|
5
|
+
export CACHE_REDIS_URL="${CACHE_REDIS_URL:-${REDIS_URL:-}}"
|
|
6
|
+
export QUEUE_STRATEGY="${QUEUE_STRATEGY:-async}"
|
|
7
|
+
export NEXT_PUBLIC_QUEUE_STRATEGY="${NEXT_PUBLIC_QUEUE_STRATEGY:-async}"
|
|
8
|
+
|
|
9
|
+
sh ./docker/scripts/init-or-migrate.sh
|
|
10
|
+
|
|
11
|
+
if [ ! -d ".mercato/generated" ]; then
|
|
12
|
+
yarn generate
|
|
13
|
+
fi
|
|
14
|
+
|
|
15
|
+
exec yarn start
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
set -euo pipefail
|
|
3
|
+
|
|
4
|
+
export CACHE_STRATEGY="${CACHE_STRATEGY:-redis}"
|
|
5
|
+
export CACHE_REDIS_URL="${CACHE_REDIS_URL:-${REDIS_URL:-}}"
|
|
6
|
+
export QUEUE_STRATEGY=async
|
|
7
|
+
export NEXT_PUBLIC_QUEUE_STRATEGY=async
|
|
8
|
+
export AUTO_SPAWN_WORKERS=false
|
|
9
|
+
export OM_AUTO_SPAWN_WORKERS=false
|
|
10
|
+
|
|
11
|
+
if [ ! -d ".mercato/generated" ]; then
|
|
12
|
+
yarn generate
|
|
13
|
+
fi
|
|
14
|
+
|
|
15
|
+
exec yarn mercato queue worker --all
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
jest.mock('@/bootstrap', () => ({
|
|
2
|
+
bootstrap: jest.fn(),
|
|
3
|
+
}))
|
|
4
|
+
|
|
5
|
+
jest.mock('@open-mercato/shared/lib/di/container', () => ({
|
|
6
|
+
createRequestContainer: jest.fn(),
|
|
7
|
+
}))
|
|
8
|
+
|
|
9
|
+
import { createHealthcheckHandler } from '../route'
|
|
10
|
+
|
|
11
|
+
type ProbeOptions = {
|
|
12
|
+
database?: () => Promise<unknown>
|
|
13
|
+
redis?: () => Promise<boolean>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function createContainer(options: ProbeOptions = {}) {
|
|
17
|
+
const database = options.database ?? (async () => [{ '?column?': 1 }])
|
|
18
|
+
const redis = options.redis ?? (async () => false)
|
|
19
|
+
|
|
20
|
+
return async () => ({
|
|
21
|
+
resolve: (name: string) => {
|
|
22
|
+
if (name === 'em') {
|
|
23
|
+
return {
|
|
24
|
+
getConnection: () => ({ execute: database }),
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (name === 'cache') {
|
|
28
|
+
return { has: redis }
|
|
29
|
+
}
|
|
30
|
+
throw new Error(`Unexpected dependency: ${name}`)
|
|
31
|
+
},
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
describe('GET /api/healthz', () => {
|
|
36
|
+
const now = () => new Date('2026-06-05T10:00:00.000Z')
|
|
37
|
+
const logger = { error: jest.fn() }
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
logger.error.mockClear()
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('returns 200 when database and Redis probes succeed', async () => {
|
|
44
|
+
const handler = createHealthcheckHandler({
|
|
45
|
+
createContainer: createContainer() as never,
|
|
46
|
+
now,
|
|
47
|
+
logger,
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
const response = await handler()
|
|
51
|
+
|
|
52
|
+
expect(response.status).toBe(200)
|
|
53
|
+
expect(await response.json()).toEqual({
|
|
54
|
+
status: 'ok',
|
|
55
|
+
ts: '2026-06-05T10:00:00.000Z',
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it.each([
|
|
60
|
+
['database', { database: async () => { throw new Error('database unavailable') } }],
|
|
61
|
+
['redis', { redis: async () => { throw new Error('redis unavailable') } }],
|
|
62
|
+
])('returns 503 when the %s probe fails', async (_component, options) => {
|
|
63
|
+
const handler = createHealthcheckHandler({
|
|
64
|
+
createContainer: createContainer(options) as never,
|
|
65
|
+
now,
|
|
66
|
+
logger,
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
const response = await handler()
|
|
70
|
+
const body = await response.json()
|
|
71
|
+
|
|
72
|
+
expect(response.status).toBe(503)
|
|
73
|
+
expect(body).toEqual({
|
|
74
|
+
status: 'degraded',
|
|
75
|
+
ts: '2026-06-05T10:00:00.000Z',
|
|
76
|
+
})
|
|
77
|
+
expect(Object.keys(body)).toEqual(['status', 'ts'])
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('returns 503 when a probe exceeds the timeout', async () => {
|
|
81
|
+
const handler = createHealthcheckHandler({
|
|
82
|
+
createContainer: createContainer({
|
|
83
|
+
database: () => new Promise(() => undefined),
|
|
84
|
+
}) as never,
|
|
85
|
+
now,
|
|
86
|
+
timeoutMs: 5,
|
|
87
|
+
logger,
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
const response = await handler()
|
|
91
|
+
|
|
92
|
+
expect(response.status).toBe(503)
|
|
93
|
+
expect(await response.json()).toEqual({
|
|
94
|
+
status: 'degraded',
|
|
95
|
+
ts: '2026-06-05T10:00:00.000Z',
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('returns 503 when Redis is configured but the cache fell back without a probe', async () => {
|
|
100
|
+
const handler = createHealthcheckHandler({
|
|
101
|
+
createContainer: createContainer() as never,
|
|
102
|
+
cacheStrategy: () => 'redis',
|
|
103
|
+
now,
|
|
104
|
+
logger,
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
const response = await handler()
|
|
108
|
+
|
|
109
|
+
expect(response.status).toBe(503)
|
|
110
|
+
expect(await response.json()).toEqual({
|
|
111
|
+
status: 'degraded',
|
|
112
|
+
ts: '2026-06-05T10:00:00.000Z',
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
})
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { CacheStrategy } from '@open-mercato/cache'
|
|
2
|
+
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
3
|
+
import { bootstrap } from '@/bootstrap'
|
|
4
|
+
import { createRequestContainer } from '@open-mercato/shared/lib/di/container'
|
|
5
|
+
|
|
6
|
+
bootstrap()
|
|
7
|
+
|
|
8
|
+
export const dynamic = 'force-dynamic'
|
|
9
|
+
|
|
10
|
+
const HEALTHCHECK_TIMEOUT_MS = 1500
|
|
11
|
+
const HEALTHCHECK_CACHE_KEY = '__open_mercato_healthcheck__'
|
|
12
|
+
|
|
13
|
+
type HealthcheckDependencies = {
|
|
14
|
+
createContainer: typeof createRequestContainer
|
|
15
|
+
now: () => Date
|
|
16
|
+
timeoutMs: number
|
|
17
|
+
logger: Pick<Console, 'error'>
|
|
18
|
+
cacheStrategy: () => string | undefined
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type HealthcheckProbe = {
|
|
22
|
+
name: 'database' | 'redis'
|
|
23
|
+
run: () => Promise<unknown>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function withTimeout(probe: HealthcheckProbe, timeoutMs: number): Promise<void> {
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
const timer = setTimeout(() => {
|
|
29
|
+
reject(new Error(`${probe.name} healthcheck timed out`))
|
|
30
|
+
}, timeoutMs)
|
|
31
|
+
|
|
32
|
+
probe.run().then(
|
|
33
|
+
() => {
|
|
34
|
+
clearTimeout(timer)
|
|
35
|
+
resolve()
|
|
36
|
+
},
|
|
37
|
+
(error: unknown) => {
|
|
38
|
+
clearTimeout(timer)
|
|
39
|
+
reject(error)
|
|
40
|
+
},
|
|
41
|
+
)
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function createHealthcheckHandler(
|
|
46
|
+
overrides: Partial<HealthcheckDependencies> = {},
|
|
47
|
+
): () => Promise<Response> {
|
|
48
|
+
const dependencies: HealthcheckDependencies = {
|
|
49
|
+
createContainer: createRequestContainer,
|
|
50
|
+
now: () => new Date(),
|
|
51
|
+
timeoutMs: HEALTHCHECK_TIMEOUT_MS,
|
|
52
|
+
logger: console,
|
|
53
|
+
cacheStrategy: () => process.env.CACHE_STRATEGY,
|
|
54
|
+
...overrides,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return async function healthcheck(): Promise<Response> {
|
|
58
|
+
const timestamp = dependencies.now().toISOString()
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
const container = await dependencies.createContainer()
|
|
62
|
+
const em = container.resolve<EntityManager>('em')
|
|
63
|
+
const cache = container.resolve<CacheStrategy>('cache')
|
|
64
|
+
const probes: HealthcheckProbe[] = [
|
|
65
|
+
{
|
|
66
|
+
name: 'database',
|
|
67
|
+
run: () => em.getConnection().execute('SELECT 1'),
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: 'redis',
|
|
71
|
+
run: async () => {
|
|
72
|
+
if (dependencies.cacheStrategy() === 'redis') {
|
|
73
|
+
if (!cache.healthcheck) {
|
|
74
|
+
throw new Error('Redis cache healthcheck is unavailable')
|
|
75
|
+
}
|
|
76
|
+
await cache.healthcheck()
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
await cache.has(HEALTHCHECK_CACHE_KEY)
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
]
|
|
83
|
+
|
|
84
|
+
const results = await Promise.allSettled(
|
|
85
|
+
probes.map((probe) => withTimeout(probe, dependencies.timeoutMs)),
|
|
86
|
+
)
|
|
87
|
+
const failed = results
|
|
88
|
+
.map((result, index) => ({ result, probe: probes[index] }))
|
|
89
|
+
.filter((entry) => entry.result.status === 'rejected')
|
|
90
|
+
|
|
91
|
+
if (failed.length === 0) {
|
|
92
|
+
return Response.json({ status: 'ok', ts: timestamp })
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
for (const entry of failed) {
|
|
96
|
+
dependencies.logger.error('[healthz] Infrastructure probe failed', {
|
|
97
|
+
component: entry.probe.name,
|
|
98
|
+
error: entry.result.status === 'rejected'
|
|
99
|
+
? entry.result.reason instanceof Error
|
|
100
|
+
? entry.result.reason.message
|
|
101
|
+
: String(entry.result.reason)
|
|
102
|
+
: 'unknown',
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
} catch (error: unknown) {
|
|
106
|
+
dependencies.logger.error('[healthz] Healthcheck setup failed', {
|
|
107
|
+
error: error instanceof Error ? error.message : String(error),
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return Response.json(
|
|
112
|
+
{ status: 'degraded', ts: timestamp },
|
|
113
|
+
{ status: 503 },
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const GET = createHealthcheckHandler()
|