Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@ RPC_CONFIG='{
... # Add more networks as needed.
}'

PUBLIC_API_KEYS=string # Optional. Comma-separated list of authorized API keys.
DRIPS_API_KEY=string # Optional. API key withouth rate limit.

NODE_ENV=string # Required. 'development' or 'production'.

POSTGRES_CONNECTION_STRING=string # Required. The connection string for the database.

PRETEND_ALL_REPOS_EXIST=boolean # If true, app will always assume all GitHub repos exist. Used in E2E tests. Defaults to false.

RATE_LIMIT_WINDOW_IN_MINUTES=number # Optional. defaults to 2.
RATE_LIMIT_MAX_REQUESTS_PER_WINDOW=number # Optional. defaults to 1000.
MAX_QUERY_DEPTH=number # Optional. defaults to 4.
TIMEOUT_IN_SECONDS=number # Optional. defaults to 20.

Expand Down
16 changes: 0 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"dotenv": "^16.3.1",
"ethers": "^6.7.1",
"express": "^4.18.2",
"express-rate-limit": "^7.1.5",
"graphql": "^16.8.0",
"graphql-depth-limit": "^1.1.0",
"graphql-tag": "^2.12.6",
Expand Down
10 changes: 0 additions & 10 deletions src/common/appSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,10 @@ export default {
rpcConfig: process.env.RPC_CONFIG
? RpcConfigSchema.parse(JSON.parse(process.env.RPC_CONFIG))
: missingEnvVar('RPC_CONFIG'),
publicApiKeys: process.env.PUBLIC_API_KEYS?.split(',') || [],
dripsApiKey: process.env.DRIPS_API_KEY,
postgresConnectionString: process.env.POSTGRES_CONNECTION_STRING,
pretendAllReposExist:
(process.env.PRETEND_ALL_REPOS_EXIST as unknown as string) === 'true' ||
false,
rateLimitWindowInMinutes: parseInt(
process.env.RATE_LIMIT_WINDOW_IN_MINUTES ?? '2',
10,
),
rateLimitMaxRequestsPerWindow: parseInt(
process.env.RATE_LIMIT_MAX_REQUESTS_PER_WINDOW ?? '1000',
10,
),
maxQueryDepth: parseInt(process.env.MAX_QUERY_DEPTH ?? '10', 10),
timeoutInSeconds: parseInt(process.env.TIMEOUT_IN_SECONDS ?? '20', 10),
ipfsGatewayUrl:
Expand Down
42 changes: 0 additions & 42 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ import { expressMiddleware } from '@apollo/server/express4';
import express from 'express';
import http from 'http';
import bodyParser from 'body-parser';
import rateLimit from 'express-rate-limit';
import type {
NextFunction,
ParamsDictionary,
Request,
Response,
} from 'express-serve-static-core';
import type { ParsedQs } from 'qs';
import depthLimit from 'graphql-depth-limit';
import cors from 'cors';
import resolvers from './resolvers';
Expand Down Expand Up @@ -64,45 +56,11 @@ const server = new ApolloServer<Context>({
],
});

const limiter = rateLimit({
skipFailedRequests: true,
windowMs: appSettings.rateLimitWindowInMinutes * 60 * 1000,
limit: appSettings.rateLimitMaxRequestsPerWindow,
standardHeaders: 'draft-7',
legacyHeaders: false,
handler: (req, res /* , next */) => {
res.status(429).json({
error: {
message: `Too many requests. Please try again at ${new Date(
(req as any).rateLimit.resetTime,
)}`,
statusCode: 429,
},
});
},
});

const customRateLimiter = (
req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>,
res: Response<any, Record<string, any>, number>,
next: NextFunction,
) => {
const apiKey = req.headers.authorization?.split(' ')[1];

if (apiKey && apiKey === appSettings.dripsApiKey) {
next();
} else {
limiter(req, res, next);
}
};

const startServer = async () => {
await connectToDatabase();

await server.start();

app.use(customRateLimiter);

app.use((req, res, next) => {
res.setTimeout(appSettings.timeoutInSeconds * 1000, () => {
res.send(408);
Expand Down