Skip to content

Commit 6652e44

Browse files
authored
fix: add auth health check (#843)
* fix: add auth health check * fix reply schema for 5xx
1 parent 7c79dba commit 6652e44

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

src/server/routes/system/health.ts

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { isDatabaseReachable } from "../../../shared/db/client";
55
import { env } from "../../../shared/utils/env";
66
import { isRedisReachable } from "../../../shared/utils/redis/redis";
77
import { thirdwebClientId } from "../../../shared/utils/sdk";
8-
import { createCustomError } from "../../middleware/error";
98

109
type EngineFeature =
1110
| "KEYPAIR_AUTH"
@@ -14,8 +13,10 @@ type EngineFeature =
1413
| "HETEROGENEOUS_WALLET_TYPES"
1514
| "SMART_BACKEND_WALLETS";
1615

17-
const ReplySchemaOk = Type.Object({
18-
status: Type.String(),
16+
const ReplySchema = Type.Object({
17+
db: Type.Boolean(),
18+
redis: Type.Boolean(),
19+
auth: Type.Boolean(),
1920
engineVersion: Type.Optional(Type.String()),
2021
engineTier: Type.Optional(Type.String()),
2122
features: Type.Array(
@@ -30,15 +31,9 @@ const ReplySchemaOk = Type.Object({
3031
clientId: Type.String(),
3132
});
3233

33-
const ReplySchemaError = Type.Object({
34-
error: Type.String(),
35-
});
36-
37-
const responseBodySchema = Type.Union([ReplySchemaOk, ReplySchemaError]);
38-
3934
export async function healthCheck(fastify: FastifyInstance) {
4035
fastify.route<{
41-
Reply: Static<typeof responseBodySchema>;
36+
Reply: Static<typeof ReplySchema>;
4237
}>({
4338
method: "GET",
4439
url: "/system/health",
@@ -49,34 +44,27 @@ export async function healthCheck(fastify: FastifyInstance) {
4944
tags: ["System"],
5045
operationId: "checkHealth",
5146
response: {
52-
[StatusCodes.OK]: ReplySchemaOk,
53-
[StatusCodes.SERVICE_UNAVAILABLE]: ReplySchemaError,
47+
[StatusCodes.OK]: ReplySchema,
48+
[StatusCodes.SERVICE_UNAVAILABLE]: ReplySchema,
5449
},
5550
},
5651
handler: async (_, res) => {
57-
if (!(await isDatabaseReachable())) {
58-
throw createCustomError(
59-
"The database is unreachable.",
60-
StatusCodes.SERVICE_UNAVAILABLE,
61-
"FAILED_HEALTHCHECK",
62-
);
63-
}
64-
65-
if (!(await isRedisReachable())) {
66-
throw createCustomError(
67-
"Redis is unreachable.",
68-
StatusCodes.SERVICE_UNAVAILABLE,
69-
"FAILED_HEALTHCHECK",
70-
);
71-
}
52+
const db = await isDatabaseReachable();
53+
const redis = await isRedisReachable();
54+
const auth = await isAuthValid();
55+
const isHealthy = db && redis && auth;
7256

73-
res.status(StatusCodes.OK).send({
74-
status: "OK",
75-
engineVersion: env.ENGINE_VERSION,
76-
engineTier: env.ENGINE_TIER ?? "SELF_HOSTED",
77-
features: getFeatures(),
78-
clientId: thirdwebClientId,
79-
});
57+
res
58+
.status(isHealthy ? StatusCodes.OK : StatusCodes.SERVICE_UNAVAILABLE)
59+
.send({
60+
db,
61+
redis,
62+
auth,
63+
engineVersion: env.ENGINE_VERSION,
64+
engineTier: env.ENGINE_TIER ?? "SELF_HOSTED",
65+
features: getFeatures(),
66+
clientId: thirdwebClientId,
67+
});
8068
},
8169
});
8270
}
@@ -95,3 +83,16 @@ const getFeatures = (): EngineFeature[] => {
9583

9684
return features;
9785
};
86+
87+
async function isAuthValid() {
88+
try {
89+
const resp = await fetch("https://api.thirdweb.com/v2/keys/use", {
90+
headers: {
91+
"x-secret-key": env.THIRDWEB_API_SECRET_KEY,
92+
},
93+
});
94+
return resp.ok;
95+
} catch {
96+
return false;
97+
}
98+
}

0 commit comments

Comments
 (0)