Skip to content

Commit b19a23c

Browse files
committed
fix: add auth health check
1 parent 7c79dba commit b19a23c

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

src/server/routes/system/health.ts

Lines changed: 31 additions & 24 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"
@@ -15,7 +14,9 @@ type EngineFeature =
1514
| "SMART_BACKEND_WALLETS";
1615

1716
const ReplySchemaOk = Type.Object({
18-
status: Type.String(),
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(
@@ -54,29 +55,22 @@ export async function healthCheck(fastify: FastifyInstance) {
5455
},
5556
},
5657
handler: async (_, res) => {
57-
if (!(await isDatabaseReachable())) {
58-
throw createCustomError(
59-
"The database is unreachable.",
60-
StatusCodes.SERVICE_UNAVAILABLE,
61-
"FAILED_HEALTHCHECK",
62-
);
63-
}
58+
const db = await isDatabaseReachable();
59+
const redis = await isRedisReachable();
60+
const auth = await isAuthValid();
61+
const isHealthy = db && redis && auth;
6462

65-
if (!(await isRedisReachable())) {
66-
throw createCustomError(
67-
"Redis is unreachable.",
68-
StatusCodes.SERVICE_UNAVAILABLE,
69-
"FAILED_HEALTHCHECK",
70-
);
71-
}
72-
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-
});
63+
res
64+
.status(isHealthy ? StatusCodes.OK : StatusCodes.SERVICE_UNAVAILABLE)
65+
.send({
66+
db,
67+
redis,
68+
auth,
69+
engineVersion: env.ENGINE_VERSION,
70+
engineTier: env.ENGINE_TIER ?? "SELF_HOSTED",
71+
features: getFeatures(),
72+
clientId: thirdwebClientId,
73+
});
8074
},
8175
});
8276
}
@@ -95,3 +89,16 @@ const getFeatures = (): EngineFeature[] => {
9589

9690
return features;
9791
};
92+
93+
async function isAuthValid() {
94+
try {
95+
const resp = await fetch("https://api.thirdweb.com/v2/keys/use", {
96+
headers: {
97+
"x-secret-key": env.THIRDWEB_API_SECRET_KEY,
98+
},
99+
});
100+
return resp.ok;
101+
} catch {
102+
return false;
103+
}
104+
}

0 commit comments

Comments
 (0)