Skip to content

Commit 0eba423

Browse files
authored
feat: add home route and update health route (#301)
* feat: add home route and update health route * undo docker * re-add json path * export json properly * remove swagger-ui package
1 parent 641ad09 commit 0eba423

File tree

8 files changed

+103
-76
lines changed

8 files changed

+103
-76
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"@fastify/cors": "^8.2.1",
3636
"@fastify/express": "^2.3.0",
3737
"@fastify/swagger": "^8.9.0",
38-
"@fastify/swagger-ui": "^1.9.3",
3938
"@fastify/type-provider-typebox": "^3.2.0",
4039
"@fastify/websocket": "^8.2.0",
4140
"@google-cloud/kms": "^4.0.0",

src/db/client.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ export const knex = pg({
1919
},
2020
acquireConnectionTimeout: 10000,
2121
} as Knex.Config);
22+
23+
export const isDatabaseHealthy = async (): Promise<boolean> => {
24+
try {
25+
await prisma.walletDetails.findFirst();
26+
return true;
27+
} catch (error) {
28+
return false;
29+
}
30+
};

src/server/middleware/cors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export const withCors = async (server: FastifyInstance) => {
1818
if (data.includes("thirdweb-preview.com")) {
1919
return new RegExp(/^https?:\/\/.*\.thirdweb-preview\.com$/);
2020
}
21+
if (data.includes("thirdweb-dev.com")) {
22+
return new RegExp(/^https?:\/\/.*\.thirdweb-dev\.com$/);
23+
}
2124

2225
return data;
2326
}),

src/server/middleware/open-api.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import swagger from "@fastify/swagger";
2-
import swaggerUi from "@fastify/swagger-ui";
32
import { FastifyInstance } from "fastify";
43

54
export const withOpenApi = async (server: FastifyInstance) => {
@@ -21,7 +20,7 @@ export const withOpenApi = async (server: FastifyInstance) => {
2120
type: "http",
2221
scheme: "bearer",
2322
bearerFormat: "JWT",
24-
description: "For Secure Server-Server Calls",
23+
description: "For secure server-authenticated calls",
2524
},
2625
},
2726
},
@@ -33,18 +32,8 @@ export const withOpenApi = async (server: FastifyInstance) => {
3332
},
3433
});
3534

36-
await server.register(swaggerUi, {
37-
routePrefix: "/",
38-
initOAuth: {},
39-
uiConfig: {
40-
docExpansion: "none",
41-
// filter: true, // This options enables search bar to allow serach by tags
42-
deepLinking: true,
43-
displayOperationId: false,
44-
layout: "BaseLayout",
45-
},
46-
47-
staticCSP: true,
48-
transformStaticCSP: (header) => header,
35+
// Exports the /json endpoint without the Swagger UI.
36+
await server.get("/json", {}, async (req, res) => {
37+
res.send(server.swagger());
4938
});
5039
};

src/server/routes/health.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Static, Type } from "@sinclair/typebox";
2+
import { FastifyInstance } from "fastify";
3+
import { StatusCodes } from "http-status-codes";
4+
import { isDatabaseHealthy } from "../../db/client";
5+
6+
const ReplySchemaOk = Type.Object({
7+
status: Type.String(),
8+
});
9+
10+
const ReplySchemaError = Type.Object({
11+
error: Type.String(),
12+
});
13+
14+
const ReplySchema = Type.Union([ReplySchemaOk, ReplySchemaError]);
15+
16+
export async function healthCheck(fastify: FastifyInstance) {
17+
fastify.route<{
18+
Reply: Static<typeof ReplySchema>;
19+
}>({
20+
method: "GET",
21+
url: "/health",
22+
schema: {
23+
summary: "Check health",
24+
description: "Check the system health of Engine",
25+
tags: ["System"],
26+
operationId: "checkHealth",
27+
response: {
28+
[StatusCodes.OK]: ReplySchemaOk,
29+
[StatusCodes.SERVICE_UNAVAILABLE]: ReplySchemaError,
30+
},
31+
},
32+
handler: async (req, res) => {
33+
const db = await isDatabaseHealthy();
34+
if (!db) {
35+
return res.status(StatusCodes.SERVICE_UNAVAILABLE).send({
36+
error: "The database is unreachable.",
37+
});
38+
}
39+
40+
res.status(StatusCodes.OK).send({
41+
status: "OK",
42+
});
43+
},
44+
});
45+
}

src/server/routes/home.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Static, Type } from "@sinclair/typebox";
2+
import { FastifyInstance } from "fastify";
3+
import { StatusCodes } from "http-status-codes";
4+
5+
const ReplySchema = Type.Object({
6+
message: Type.String(),
7+
});
8+
9+
export async function home(fastify: FastifyInstance) {
10+
fastify.route<{
11+
Reply: Static<typeof ReplySchema>;
12+
}>({
13+
method: "GET",
14+
url: "/",
15+
schema: {
16+
summary: "/",
17+
description: "Instructions to manage your Engine",
18+
tags: ["System"],
19+
operationId: "home",
20+
response: {
21+
[StatusCodes.OK]: ReplySchema,
22+
},
23+
},
24+
handler: async (req, res) => {
25+
return res.status(StatusCodes.OK).send({
26+
message:
27+
"Engine is set up successfully. Manage your Engine from https://thirdweb.com/dashboard/engine.",
28+
});
29+
},
30+
});
31+
}

src/server/routes/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ import { createRelayer } from "./relayer/create";
8282
import { getAllRelayers } from "./relayer/getAll";
8383
import { revokeRelayer } from "./relayer/revoke";
8484

85+
// System
86+
import { healthCheck } from "./health";
87+
import { home } from "./home";
88+
8589
export const withRoutes = async (fastify: FastifyInstance) => {
8690
// Wallet
8791
await fastify.register(createWallet);
@@ -173,10 +177,7 @@ export const withRoutes = async (fastify: FastifyInstance) => {
173177
await fastify.register(erc1155Routes);
174178
await fastify.register(marketplaceV3Routes);
175179

176-
// Health
177-
fastify.get("/health", async () => {
178-
return {
179-
status: "OK",
180-
};
181-
});
180+
// System
181+
await fastify.register(home);
182+
await fastify.register(healthCheck);
182183
};

yarn.lock

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,11 +1321,6 @@
13211321
"@ethersproject/properties" "^5.7.0"
13221322
"@ethersproject/strings" "^5.7.0"
13231323

1324-
"@fastify/accept-negotiator@^1.0.0":
1325-
version "1.1.0"
1326-
resolved "https://registry.yarnpkg.com/@fastify/accept-negotiator/-/accept-negotiator-1.1.0.tgz#c1c66b3b771c09742a54dd5bc87c582f6b0630ff"
1327-
integrity sha512-OIHZrb2ImZ7XG85HXOONLcJWGosv7sIvM2ifAPQVhg9Lv7qdmMBNVaai4QTdyuaqbKM5eO6sLSQOYI7wEQeCJQ==
1328-
13291324
"@fastify/ajv-compiler@^3.5.0":
13301325
version "3.5.0"
13311326
resolved "https://registry.yarnpkg.com/@fastify/ajv-compiler/-/ajv-compiler-3.5.0.tgz#459bff00fefbf86c96ec30e62e933d2379e46670"
@@ -1384,41 +1379,6 @@
13841379
dependencies:
13851380
fast-json-stringify "^5.7.0"
13861381

1387-
"@fastify/send@^2.0.0":
1388-
version "2.1.0"
1389-
resolved "https://registry.yarnpkg.com/@fastify/send/-/send-2.1.0.tgz#1aa269ccb4b0940a2dadd1f844443b15d8224ea0"
1390-
integrity sha512-yNYiY6sDkexoJR0D8IDy3aRP3+L4wdqCpvx5WP+VtEU58sn7USmKynBzDQex5X42Zzvw2gNzzYgP90UfWShLFA==
1391-
dependencies:
1392-
"@lukeed/ms" "^2.0.1"
1393-
escape-html "~1.0.3"
1394-
fast-decode-uri-component "^1.0.1"
1395-
http-errors "2.0.0"
1396-
mime "^3.0.0"
1397-
1398-
"@fastify/static@^6.0.0":
1399-
version "6.10.2"
1400-
resolved "https://registry.yarnpkg.com/@fastify/static/-/static-6.10.2.tgz#dfaaccfa191a4ba85ea8e3926853c9e6d979e67f"
1401-
integrity sha512-UoaMvIHSBLCZBYOVZwFRYqX2ufUhd7FFMYGDeSf0Z+D8jhYtwljjmuQGuanUP8kS4y/ZEV1a8mfLha3zNwsnnQ==
1402-
dependencies:
1403-
"@fastify/accept-negotiator" "^1.0.0"
1404-
"@fastify/send" "^2.0.0"
1405-
content-disposition "^0.5.3"
1406-
fastify-plugin "^4.0.0"
1407-
glob "^8.0.1"
1408-
p-limit "^3.1.0"
1409-
readable-stream "^4.0.0"
1410-
1411-
"@fastify/swagger-ui@^1.9.3":
1412-
version "1.9.3"
1413-
resolved "https://registry.yarnpkg.com/@fastify/swagger-ui/-/swagger-ui-1.9.3.tgz#1ec03ea2595cb2e7d6de6ae7c949bebcff8370a5"
1414-
integrity sha512-YYqce4CydjDIEry6Zo4JLjVPe5rjS8iGnk3fHiIQnth9sFSLeyG0U1DCH+IyYmLddNDg1uWJOuErlVqnu/jI3w==
1415-
dependencies:
1416-
"@fastify/static" "^6.0.0"
1417-
fastify-plugin "^4.0.0"
1418-
openapi-types "^12.0.2"
1419-
rfdc "^1.3.0"
1420-
yaml "^2.2.2"
1421-
14221382
"@fastify/swagger@^8.9.0":
14231383
version "8.9.0"
14241384
resolved "https://registry.yarnpkg.com/@fastify/swagger/-/swagger-8.9.0.tgz#8c22152785c4b8ba653217274866aac2e4314369"
@@ -1561,11 +1521,6 @@
15611521
dependencies:
15621522
"@lit-labs/ssr-dom-shim" "^1.0.0"
15631523

1564-
"@lukeed/ms@^2.0.1":
1565-
version "2.0.1"
1566-
resolved "https://registry.yarnpkg.com/@lukeed/ms/-/ms-2.0.1.tgz#3c2bbc258affd9cc0e0cc7828477383c73afa6ee"
1567-
integrity sha512-Xs/4RZltsAL7pkvaNStUQt7netTkyxrS0K+RILcVr3TRMS/ToOg4I6uNfhB9SlGsnWBym4U+EaXq0f0cEMNkHA==
1568-
15691524
"@magic-ext/connect@^6.7.2":
15701525
version "6.7.2"
15711526
resolved "https://registry.yarnpkg.com/@magic-ext/connect/-/connect-6.7.2.tgz#9b479d2a3b0740e63915c7c7af461af5f3bfbf2a"
@@ -4743,7 +4698,7 @@ concat-map@0.0.1:
47434698
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
47444699
integrity sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==
47454700

4746-
content-disposition@0.5.4, content-disposition@^0.5.3:
4701+
content-disposition@0.5.4:
47474702
version "0.5.4"
47484703
resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.4.tgz#8b82b4efac82512a02bb0b1dcec9d2c5e8eb5bfe"
47494704
integrity sha512-FveZTNuGw04cxlAiWbzi6zTAL/lhehaWbTtgluJh4/E95DqMwTmha3KZN1aAWA8cFIhHzMZUvLevkw5Rqk+tSQ==
@@ -6109,7 +6064,7 @@ glob@^7.0.5, glob@^7.1.3:
61096064
once "^1.3.0"
61106065
path-is-absolute "^1.0.0"
61116066

6112-
glob@^8.0.0, glob@^8.0.1:
6067+
glob@^8.0.0:
61136068
version "8.1.0"
61146069
resolved "https://registry.yarnpkg.com/glob/-/glob-8.1.0.tgz#d388f656593ef708ee3e34640fdfb99a9fd1c33e"
61156070
integrity sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ==
@@ -7266,11 +7221,6 @@ mime@2.6.0:
72667221
resolved "https://registry.yarnpkg.com/mime/-/mime-2.6.0.tgz#a2a682a95cd4d0cb1d6257e28f83da7e35800367"
72677222
integrity sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==
72687223

7269-
mime@^3.0.0:
7270-
version "3.0.0"
7271-
resolved "https://registry.yarnpkg.com/mime/-/mime-3.0.0.tgz#b374550dca3a0c18443b0c950a6a58f1931cf7a7"
7272-
integrity sha512-jSCU7/VB1loIWBZe14aEYHU/+1UMEHoaO7qxCOVJOw9GgH72VAWppxNcjU+x9a2k3GSIBXNKxXQFqRvvZ7vr3A==
7273-
72747224
mimic-response@^1.0.0:
72757225
version "1.0.1"
72767226
resolved "https://registry.yarnpkg.com/mimic-response/-/mimic-response-1.0.1.tgz#4923538878eef42063cb8a3e3b0798781487ab1b"
@@ -7597,7 +7547,7 @@ once@^1.3.0, once@^1.3.1, once@^1.4.0:
75977547
dependencies:
75987548
wrappy "1"
75997549

7600-
openapi-types@^12.0.0, openapi-types@^12.0.2:
7550+
openapi-types@^12.0.0:
76017551
version "12.1.3"
76027552
resolved "https://registry.yarnpkg.com/openapi-types/-/openapi-types-12.1.3.tgz#471995eb26c4b97b7bd356aacf7b91b73e777dd3"
76037553
integrity sha512-N4YtSYJqghVu4iek2ZUvcN/0aqH1kRDuNqzcycDxhOUpg7GdvLa2F3DgS6yBNhInhv2r/6I0Flkn7CqL8+nIcw==
@@ -7656,7 +7606,7 @@ p-limit@^2.2.0:
76567606
dependencies:
76577607
p-try "^2.0.0"
76587608

7659-
p-limit@^3.0.2, p-limit@^3.1.0:
7609+
p-limit@^3.0.2:
76607610
version "3.1.0"
76617611
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-3.1.0.tgz#e1daccbe78d0d1388ca18c64fea38e3e57e3706b"
76627612
integrity sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==

0 commit comments

Comments
 (0)