@@ -5,7 +5,6 @@ import { isDatabaseReachable } from "../../../shared/db/client";
5
5
import { env } from "../../../shared/utils/env" ;
6
6
import { isRedisReachable } from "../../../shared/utils/redis/redis" ;
7
7
import { thirdwebClientId } from "../../../shared/utils/sdk" ;
8
- import { createCustomError } from "../../middleware/error" ;
9
8
10
9
type EngineFeature =
11
10
| "KEYPAIR_AUTH"
@@ -14,8 +13,10 @@ type EngineFeature =
14
13
| "HETEROGENEOUS_WALLET_TYPES"
15
14
| "SMART_BACKEND_WALLETS" ;
16
15
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 ( ) ,
19
20
engineVersion : Type . Optional ( Type . String ( ) ) ,
20
21
engineTier : Type . Optional ( Type . String ( ) ) ,
21
22
features : Type . Array (
@@ -30,15 +31,9 @@ const ReplySchemaOk = Type.Object({
30
31
clientId : Type . String ( ) ,
31
32
} ) ;
32
33
33
- const ReplySchemaError = Type . Object ( {
34
- error : Type . String ( ) ,
35
- } ) ;
36
-
37
- const responseBodySchema = Type . Union ( [ ReplySchemaOk , ReplySchemaError ] ) ;
38
-
39
34
export async function healthCheck ( fastify : FastifyInstance ) {
40
35
fastify . route < {
41
- Reply : Static < typeof responseBodySchema > ;
36
+ Reply : Static < typeof ReplySchema > ;
42
37
} > ( {
43
38
method : "GET" ,
44
39
url : "/system/health" ,
@@ -49,34 +44,27 @@ export async function healthCheck(fastify: FastifyInstance) {
49
44
tags : [ "System" ] ,
50
45
operationId : "checkHealth" ,
51
46
response : {
52
- [ StatusCodes . OK ] : ReplySchemaOk ,
53
- [ StatusCodes . SERVICE_UNAVAILABLE ] : ReplySchemaError ,
47
+ [ StatusCodes . OK ] : ReplySchema ,
48
+ [ StatusCodes . SERVICE_UNAVAILABLE ] : ReplySchema ,
54
49
} ,
55
50
} ,
56
51
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 ;
72
56
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
+ } ) ;
80
68
} ,
81
69
} ) ;
82
70
}
@@ -95,3 +83,16 @@ const getFeatures = (): EngineFeature[] => {
95
83
96
84
return features ;
97
85
} ;
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