Skip to content

Commit

Permalink
refactor: clearer folder organization (#792)
Browse files Browse the repository at this point in the history
  • Loading branch information
arcoraven authored Dec 6, 2024
1 parent af85f61 commit 5d405f0
Show file tree
Hide file tree
Showing 449 changed files with 978 additions and 1,000 deletions.
17 changes: 0 additions & 17 deletions .vscode/settings.json

This file was deleted.

1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ COPY --from=build /app/package.json .
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/src/prisma/* ./src/prisma/
COPY --from=build /app/dist ./dist
COPY --from=build /app/scripts ./dist/scripts

# Replace the schema path in the package.json file
RUN sed -i 's_"schema": "./src/prisma/schema.prisma"_"schema": "./dist/prisma/schema.prisma"_g' package.json
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"dev:run": "npx nodemon --watch 'src/**/*.ts' --exec 'npx tsx ./src/index.ts' --files src/index.ts",
"build": "rm -rf dist && tsc -p ./tsconfig.json --outDir dist",
"build:docker": "docker build . -f Dockerfile -t prod",
"generate:sdk": "npx tsx ./src/scripts/generate-sdk && cd ./sdk && yarn build",
"prisma:setup:dev": "npx tsx ./src/scripts/setup-db.ts",
"generate:sdk": "npx tsx ./scripts/generate-sdk && cd ./sdk && yarn build",
"prisma:setup:dev": "npx tsx ./scripts/setup-db.ts",
"prisma:setup:prod": "npx tsx ./dist/scripts/setup-db.js",
"start": "yarn prisma:setup:prod && yarn start:migrations && yarn start:run",
"start:migrations": "npx tsx ./dist/scripts/apply-migrations.js",
Expand Down
10 changes: 7 additions & 3 deletions src/scripts/apply-migrations.ts → scripts/apply-migrations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { logger } from "../utils/logger";
import { acquireLock, releaseLock, waitForLock } from "../utils/redis/lock";
import { redis } from "../utils/redis/redis";
import { logger } from "../src/shared/utils/logger";
import {
acquireLock,
releaseLock,
waitForLock,
} from "../src/shared/utils/redis/lock";
import { redis } from "../src/shared/utils/redis/redis";

const MIGRATION_LOCK_TTL_SECONDS = 60;

Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions src/scripts/setup-db.ts → scripts/setup-db.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { execSync } from "child_process";
import { prisma } from "../db/client";
import { execSync } from "node:child_process";
import { prisma } from "../src/shared/db/client";

const main = async () => {
const [{ exists: hasWalletsTable }]: [{ exists: boolean }] =
Expand All @@ -14,8 +14,8 @@ const main = async () => {

const schema =
process.env.NODE_ENV === "production"
? `./dist/prisma/schema.prisma`
: `./src/prisma/schema.prisma`;
? "./dist/prisma/schema.prisma"
: "./src/prisma/schema.prisma";

if (hasWalletsTable) {
execSync(`yarn prisma migrate reset --force --schema ${schema}`, {
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import "./polyfill";
import "./tracer";

import { initServer } from "./server";
import { env } from "./utils/env";
import { logger } from "./utils/logger";
import "./utils/tracer";
import { env } from "./shared/utils/env";
import { logger } from "./shared/utils/logger";
import { initWorker } from "./worker";
import { CancelRecycledNoncesQueue } from "./worker/queues/cancelRecycledNoncesQueue";
import { MigratePostgresTransactionsQueue } from "./worker/queues/migratePostgresTransactionsQueue";
Expand Down
2 changes: 1 addition & 1 deletion src/polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as crypto from "crypto";
import * as crypto from "node:crypto";

if (typeof globalThis.crypto === "undefined") {
(globalThis as any).crypto = crypto;
Expand Down
10 changes: 5 additions & 5 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import fastify, { type FastifyInstance } from "fastify";
import * as fs from "node:fs";
import path from "node:path";
import { URL } from "node:url";
import { clearCacheCron } from "../utils/cron/clearCacheCron";
import { env } from "../utils/env";
import { logger } from "../utils/logger";
import { metricsServer } from "../utils/prometheus";
import { withServerUsageReporting } from "../utils/usage";
import { clearCacheCron } from "../shared/utils/cron/clearCacheCron";
import { env } from "../shared/utils/env";
import { logger } from "../shared/utils/logger";
import { metricsServer } from "../shared/utils/prometheus";
import { withServerUsageReporting } from "../shared/utils/usage";
import { updateTxListener } from "./listeners/updateTxListener";
import { withAdminRoutes } from "./middleware/adminRoutes";
import { withAuth } from "./middleware/auth";
Expand Down
6 changes: 3 additions & 3 deletions src/server/listeners/updateTxListener.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { knex } from "../../db/client";
import { TransactionDB } from "../../db/transactions/db";
import { logger } from "../../utils/logger";
import { knex } from "../../shared/db/client";
import { TransactionDB } from "../../shared/db/transactions/db";
import { logger } from "../../shared/utils/logger";
import { toTransactionSchema } from "../schemas/transaction";
import { subscriptionsData } from "../schemas/websocket";
import {
Expand Down
2 changes: 1 addition & 1 deletion src/server/middleware/adminRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Queue } from "bullmq";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { timingSafeEqual } from "node:crypto";
import { env } from "../../utils/env";
import { env } from "../../shared/utils/env";
import { CancelRecycledNoncesQueue } from "../../worker/queues/cancelRecycledNoncesQueue";
import { MigratePostgresTransactionsQueue } from "../../worker/queues/migratePostgresTransactionsQueue";
import { MineTransactionQueue } from "../../worker/queues/mineTransactionQueue";
Expand Down
30 changes: 15 additions & 15 deletions src/server/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ import {
type ThirdwebAuthUser,
} from "@thirdweb-dev/auth/fastify";
import { AsyncWallet } from "@thirdweb-dev/wallets/evm/wallets/async";
import { createHash } from "crypto";
import { createHash } from "node:crypto";
import type { FastifyInstance } from "fastify";
import type { FastifyRequest } from "fastify/types/request";
import jsonwebtoken, { type JwtPayload } from "jsonwebtoken";
import { validate as uuidValidate } from "uuid";
import { getPermissions } from "../../db/permissions/getPermissions";
import { createToken } from "../../db/tokens/createToken";
import { revokeToken } from "../../db/tokens/revokeToken";
import { WebhooksEventTypes } from "../../schema/webhooks";
import { THIRDWEB_DASHBOARD_ISSUER, handleSiwe } from "../../utils/auth";
import { getAccessToken } from "../../utils/cache/accessToken";
import { getAuthWallet } from "../../utils/cache/authWallet";
import { getConfig } from "../../utils/cache/getConfig";
import { getWebhooksByEventType } from "../../utils/cache/getWebhook";
import { getKeypair } from "../../utils/cache/keypair";
import { env } from "../../utils/env";
import { logger } from "../../utils/logger";
import { sendWebhookRequest } from "../../utils/webhook";
import { Permission } from "../schemas/auth";
import { getPermissions } from "../../shared/db/permissions/getPermissions";
import { createToken } from "../../shared/db/tokens/createToken";
import { revokeToken } from "../../shared/db/tokens/revokeToken";
import { WebhooksEventTypes } from "../../shared/schemas/webhooks";
import { THIRDWEB_DASHBOARD_ISSUER, handleSiwe } from "../../shared/utils/auth";
import { getAccessToken } from "../../shared/utils/cache/accessToken";
import { getAuthWallet } from "../../shared/utils/cache/authWallet";
import { getConfig } from "../../shared/utils/cache/getConfig";
import { getWebhooksByEventType } from "../../shared/utils/cache/getWebhook";
import { getKeypair } from "../../shared/utils/cache/keypair";
import { env } from "../../shared/utils/env";
import { logger } from "../../shared/utils/logger";
import { sendWebhookRequest } from "../../shared/utils/webhook";
import { Permission } from "../../shared/schemas/auth";
import { ADMIN_QUEUES_BASEPATH } from "./adminRoutes";
import { OPENAPI_ROUTES } from "./openApi";

Expand Down
2 changes: 1 addition & 1 deletion src/server/middleware/cors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FastifyInstance } from "fastify";
import { getConfig } from "../../utils/cache/getConfig";
import { getConfig } from "../../shared/utils/cache/getConfig";
import { ADMIN_QUEUES_BASEPATH } from "./adminRoutes";

const STANDARD_METHODS = "GET,POST,DELETE,PUT,PATCH,HEAD,PUT,PATCH,POST,DELETE";
Expand Down
2 changes: 1 addition & 1 deletion src/server/middleware/engineMode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FastifyInstance } from "fastify";
import { env } from "../../utils/env";
import { env } from "../../shared/utils/env";

export function withEnforceEngineMode(server: FastifyInstance) {
if (env.ENGINE_MODE === "sandbox") {
Expand Down
4 changes: 2 additions & 2 deletions src/server/middleware/error.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { FastifyInstance } from "fastify";
import { ReasonPhrases, StatusCodes } from "http-status-codes";
import { ZodError } from "zod";
import { env } from "../../utils/env";
import { parseEthersError } from "../../utils/ethers";
import { env } from "../../shared/utils/env";
import { parseEthersError } from "../../shared/utils/ethers";

export type CustomError = {
message: string;
Expand Down
2 changes: 1 addition & 1 deletion src/server/middleware/logs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FastifyInstance } from "fastify";
import { stringify } from "thirdweb/utils";
import { logger } from "../../utils/logger";
import { logger } from "../../shared/utils/logger";
import { ADMIN_QUEUES_BASEPATH } from "./adminRoutes";
import { OPENAPI_ROUTES } from "./openApi";

Expand Down
4 changes: 2 additions & 2 deletions src/server/middleware/prometheus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { env } from "../../utils/env";
import { recordMetrics } from "../../utils/prometheus";
import { env } from "../../shared/utils/env";
import { recordMetrics } from "../../shared/utils/prometheus";

export function withPrometheus(server: FastifyInstance) {
if (!env.METRICS_ENABLED) {
Expand Down
4 changes: 2 additions & 2 deletions src/server/middleware/rateLimit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { env } from "../../utils/env";
import { redis } from "../../utils/redis/redis";
import { env } from "../../shared/utils/env";
import { redis } from "../../shared/utils/redis/redis";
import { createCustomError } from "./error";
import { OPENAPI_ROUTES } from "./openApi";

Expand Down
2 changes: 1 addition & 1 deletion src/server/middleware/websocket.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import WebSocketPlugin from "@fastify/websocket";
import type { FastifyInstance } from "fastify";
import { logger } from "../../utils/logger";
import { logger } from "../../shared/utils/logger";

export async function withWebSocket(server: FastifyInstance) {
await server.register(WebSocketPlugin, {
Expand Down
14 changes: 7 additions & 7 deletions src/server/routes/admin/nonces.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import {
Address,
type Address,
eth_getTransactionCount,
getAddress,
getRpcClient,
Expand All @@ -12,10 +12,10 @@ import {
lastUsedNonceKey,
recycledNoncesKey,
sentNoncesKey,
} from "../../../db/wallets/walletNonce";
import { getChain } from "../../../utils/chain";
import { redis } from "../../../utils/redis/redis";
import { thirdwebClient } from "../../../utils/sdk";
} from "../../../shared/db/wallets/walletNonce";
import { getChain } from "../../../shared/utils/chain";
import { redis } from "../../../shared/utils/redis/redis";
import { thirdwebClient } from "../../../shared/utils/sdk";
import { AddressSchema } from "../../schemas/address";
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
import { walletWithAddressParamSchema } from "../../schemas/wallet";
Expand Down
14 changes: 7 additions & 7 deletions src/server/routes/admin/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Static, Type } from "@sinclair/typebox";
import { Queue } from "bullmq";
import { FastifyInstance } from "fastify";
import { type Static, Type } from "@sinclair/typebox";
import type { Queue } from "bullmq";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { stringify } from "thirdweb/utils";
import { TransactionDB } from "../../../db/transactions/db";
import { getConfig } from "../../../utils/cache/getConfig";
import { maybeDate } from "../../../utils/primitiveTypes";
import { redis } from "../../../utils/redis/redis";
import { TransactionDB } from "../../../shared/db/transactions/db";
import { getConfig } from "../../../shared/utils/cache/getConfig";
import { maybeDate } from "../../../shared/utils/primitiveTypes";
import { redis } from "../../../shared/utils/redis/redis";
import { MineTransactionQueue } from "../../../worker/queues/mineTransactionQueue";
import { SendTransactionQueue } from "../../../worker/queues/sendTransactionQueue";
import { createCustomError } from "../../middleware/error";
Expand Down
10 changes: 5 additions & 5 deletions src/server/routes/auth/access-tokens/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { buildJWT } from "@thirdweb-dev/auth";
import { LocalWallet } from "@thirdweb-dev/wallets";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../db/configuration/updateConfiguration";
import { createToken } from "../../../../db/tokens/createToken";
import { accessTokenCache } from "../../../../utils/cache/accessToken";
import { getConfig } from "../../../../utils/cache/getConfig";
import { env } from "../../../../utils/env";
import { updateConfiguration } from "../../../../shared/db/configuration/updateConfiguration";
import { createToken } from "../../../../shared/db/tokens/createToken";
import { accessTokenCache } from "../../../../shared/utils/cache/accessToken";
import { getConfig } from "../../../../shared/utils/cache/getConfig";
import { env } from "../../../../shared/utils/env";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
import { AccessTokenSchema } from "./getAll";

Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/auth/access-tokens/getAll.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { getAccessTokens } from "../../../../db/tokens/getAccessTokens";
import { getAccessTokens } from "../../../../shared/db/tokens/getAccessTokens";
import { AddressSchema } from "../../../schemas/address";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";

Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/auth/access-tokens/revoke.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { revokeToken } from "../../../../db/tokens/revokeToken";
import { accessTokenCache } from "../../../../utils/cache/accessToken";
import { revokeToken } from "../../../../shared/db/tokens/revokeToken";
import { accessTokenCache } from "../../../../shared/utils/cache/accessToken";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";

const requestBodySchema = Type.Object({
Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/auth/access-tokens/update.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateToken } from "../../../../db/tokens/updateToken";
import { accessTokenCache } from "../../../../utils/cache/accessToken";
import { updateToken } from "../../../../shared/db/tokens/updateToken";
import { accessTokenCache } from "../../../../shared/utils/cache/accessToken";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";

const requestBodySchema = Type.Object({
Expand Down
12 changes: 6 additions & 6 deletions src/server/routes/auth/keypair/add.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Keypairs, Prisma } from "@prisma/client";
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { type Keypairs, Prisma } from "@prisma/client";
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { insertKeypair } from "../../../../db/keypair/insert";
import { isWellFormedPublicKey } from "../../../../utils/crypto";
import { insertKeypair } from "../../../../shared/db/keypair/insert";
import { isWellFormedPublicKey } from "../../../../shared/utils/crypto";
import { createCustomError } from "../../../middleware/error";
import {
KeypairAlgorithmSchema,
KeypairSchema,
toKeypairSchema,
} from "../../../schemas/keypairs";
} from "../../../../shared/schemas/keypair";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";

const requestBodySchema = Type.Object({
Expand Down
11 changes: 7 additions & 4 deletions src/server/routes/auth/keypair/list.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { listKeypairs } from "../../../../db/keypair/list";
import { KeypairSchema, toKeypairSchema } from "../../../schemas/keypairs";
import { listKeypairs } from "../../../../shared/db/keypair/list";
import {
KeypairSchema,
toKeypairSchema,
} from "../../../../shared/schemas/keypair";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";

const responseBodySchema = Type.Object({
Expand Down
8 changes: 4 additions & 4 deletions src/server/routes/auth/keypair/remove.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { deleteKeypair } from "../../../../db/keypair/delete";
import { keypairCache } from "../../../../utils/cache/keypair";
import { deleteKeypair } from "../../../../shared/db/keypair/delete";
import { keypairCache } from "../../../../shared/utils/cache/keypair";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";

const requestBodySchema = Type.Object({
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/auth/permissions/getAll.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { prisma } from "../../../../db/client";
import { prisma } from "../../../../shared/db/client";
import { AddressSchema } from "../../../schemas/address";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";

Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/auth/permissions/grant.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updatePermissions } from "../../../../db/permissions/updatePermissions";
import { updatePermissions } from "../../../../shared/db/permissions/updatePermissions";
import { AddressSchema } from "../../../schemas/address";
import { permissionsSchema } from "../../../schemas/auth";
import { permissionsSchema } from "../../../../shared/schemas/auth";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";

const requestBodySchema = Type.Object({
Expand Down
Loading

0 comments on commit 5d405f0

Please sign in to comment.