-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from technologiestiftung/staging
feat: make file limit configurable in env
- Loading branch information
Showing
9 changed files
with
65 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,32 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import { Config } from "../types/config-types"; | ||
import { NextFunction, Request, Response } from "express"; | ||
import { config } from ".."; | ||
|
||
const basicAuthMiddleware = | ||
(config: Config) => (req: Request, res: Response, next: NextFunction) => { | ||
const xApiKeyInHeader = req.headers["x-api-key"]; | ||
const basicAuthMiddleware = ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
const xApiKeyInHeader = req.headers["x-api-key"]; | ||
|
||
if (!xApiKeyInHeader) { | ||
return res.status(401).json({ | ||
message: "Unauthorized", | ||
code: "unauthorized", | ||
status: 401, | ||
}); | ||
} | ||
if (!xApiKeyInHeader) { | ||
return res.status(401).json({ | ||
message: "Unauthorized", | ||
code: "unauthorized", | ||
status: 401, | ||
}); | ||
} | ||
|
||
const validXApiKey = config.xApiKey; | ||
const validXApiKey = config.xApiKey; | ||
|
||
if (xApiKeyInHeader !== validXApiKey) { | ||
return res.status(401).json({ | ||
message: "Unauthorized", | ||
code: "unauthorized", | ||
status: 401, | ||
}); | ||
} | ||
if (xApiKeyInHeader !== validXApiKey) { | ||
return res.status(401).json({ | ||
message: "Unauthorized", | ||
code: "unauthorized", | ||
status: 401, | ||
}); | ||
} | ||
|
||
next(); | ||
}; | ||
next(); | ||
}; | ||
|
||
export default basicAuthMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
import { Config } from "./../types/config-types"; | ||
import cors from "cors"; | ||
import { config } from ".."; | ||
|
||
const corsMiddleware = (config: Config) => { | ||
const corsOptions = { | ||
origin: config.corsAllowedOrigin, | ||
methods: "GET,POST", | ||
allowedHeaders: "Content-Type,Authorization,x-api-key,llm", | ||
optionsSuccessStatus: 200, | ||
}; | ||
|
||
return cors(corsOptions); | ||
const corsOptions = { | ||
origin: config.corsAllowedOrigin, | ||
methods: "GET,POST", | ||
allowedHeaders: "Content-Type,Authorization,x-api-key,llm", | ||
optionsSuccessStatus: 200, | ||
}; | ||
|
||
const corsMiddleware = cors(corsOptions); | ||
|
||
export default corsMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
import rateLimit from "express-rate-limit"; | ||
import { Config } from "../types/config-types"; | ||
import { config } from ".."; | ||
|
||
const limiter = (config: Config) => | ||
rateLimit({ | ||
windowMs: 1 * 60 * 1000, | ||
max: config.rateLimitRequestsPerMinute, | ||
message: "API rate limit exceeded, please try again after one minute.", | ||
handler: function (req, res) { | ||
res.status(429).json({ | ||
message: "API rate limit exceeded, please try again after one minute.", | ||
code: "api_rate_limit_exceeded", | ||
status: 429, | ||
}); | ||
}, | ||
}); | ||
const limiter = rateLimit({ | ||
windowMs: 1 * 60 * 1000, | ||
max: config.rateLimitRequestsPerMinute, | ||
message: "API rate limit exceeded, please try again after one minute.", | ||
handler: function (req, res) { | ||
res.status(429).json({ | ||
message: "API rate limit exceeded, please try again after one minute.", | ||
code: "api_rate_limit_exceeded", | ||
status: 429, | ||
}); | ||
}, | ||
}); | ||
|
||
export default limiter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters