-
Notifications
You must be signed in to change notification settings - Fork 846
[Beta]: Budgets and rate limits alongwith UI and everything #1375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
narengogi
wants to merge
7
commits into
main
Choose a base branch
from
feature/rate-limit-resets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8a0eb99
rate limits with resets
narengogi a3d77ca
formatting
narengogi 0b802a3
dont crash on unhandled exceptions
narengogi cf965b9
dont crash on unhandled exceptions
narengogi 10f253d
handle refreshing the conf file at runtime
narengogi e125a6e
simplify ui and handle rat elimit ejection
narengogi a6e41e0
Merge remote-tracking branch 'upstream/main' into feature/rate-limit-…
narengogi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { Context, Hono } from 'hono'; | ||
| import { getDefaultCache } from '../shared/services/cache'; | ||
| import { getSettings } from '../../initializeSettings'; | ||
| import { generateRateLimitKey } from '../middlewares/portkey/handlers/rateLimits'; | ||
| import { RateLimiterKeyTypes } from '../globals'; | ||
|
|
||
| /** | ||
| * Helper function to authenticate admin requests | ||
| */ | ||
| async function authenticateAdmin(c: Context): Promise<boolean> { | ||
| try { | ||
| const fs = await import('fs/promises'); | ||
| const path = await import('path'); | ||
| const settingsPath = path.join(process.cwd(), 'conf.json'); | ||
| const settingsData = await fs.readFile(settingsPath, 'utf-8'); | ||
| const settings = JSON.parse(settingsData); | ||
|
|
||
| const authHeader = | ||
| c.req.header('Authorization') || c.req.header('authorization'); | ||
| const providedKey = | ||
| authHeader?.replace('Bearer ', '') || c.req.header('x-admin-api-key'); | ||
|
|
||
| return providedKey === settings.adminApiKey; | ||
| } catch (error) { | ||
| console.error('Error authenticating admin:', error); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * GET route for /admin/settings | ||
| * Serves the settings configuration file (requires admin authentication) | ||
| */ | ||
| async function getSettingsHandler(c: Context): Promise<Response> { | ||
| const isAuthenticated = await authenticateAdmin(c); | ||
| if (!isAuthenticated) { | ||
| return c.json({ error: 'Unauthorized' }, 401); | ||
| } | ||
|
|
||
| try { | ||
| const fs = await import('fs/promises'); | ||
| const path = await import('path'); | ||
| const settingsPath = path.join(process.cwd(), 'conf.json'); | ||
| const settingsData = await fs.readFile(settingsPath, 'utf-8'); | ||
| return c.json(JSON.parse(settingsData)); | ||
| } catch (error) { | ||
| console.error('Error reading conf.json:', error); | ||
| return c.json({ error: 'Settings file not found' }, 404); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * PUT route for /admin/settings | ||
| * Updates the settings configuration file (requires admin authentication) | ||
| */ | ||
| async function putSettingsHandler(c: Context): Promise<Response> { | ||
| const isAuthenticated = await authenticateAdmin(c); | ||
| if (!isAuthenticated) { | ||
| return c.json({ error: 'Unauthorized' }, 401); | ||
| } | ||
|
|
||
| try { | ||
| const fs = await import('fs/promises'); | ||
| const path = await import('path'); | ||
| const settingsPath = path.join(process.cwd(), 'conf.json'); | ||
| const body = await c.req.json(); | ||
| await fs.writeFile(settingsPath, JSON.stringify(body, null, 2)); | ||
| return c.json({ success: true }); | ||
| } catch (error) { | ||
| console.error('Error writing conf.json:', error); | ||
| return c.json({ error: 'Failed to save settings' }, 500); | ||
| } | ||
| } | ||
|
|
||
| async function resetIntegrationRateLimitHandler(c: Context): Promise<Response> { | ||
| const isAuthenticated = await authenticateAdmin(c); | ||
| if (!isAuthenticated) { | ||
| return c.json({ error: 'Unauthorized' }, 401); | ||
| } | ||
|
|
||
| try { | ||
| const settings = await getSettings(); | ||
| const integrationId = c.req.param('integrationId'); | ||
| const organisationId = settings.organisationDetails.id; | ||
| const workspaceId = settings.organisationDetails?.workspaceDetails?.id; | ||
| const rateLimits = settings.integrations.find( | ||
| (integration) => integration.slug === integrationId | ||
| )?.integration_details?.rate_limits; | ||
| const workspaceKey = `${integrationId}-${workspaceId}`; | ||
| for (const rateLimit of rateLimits) { | ||
| const rateLimitKey = generateRateLimitKey( | ||
| organisationId, | ||
| rateLimit.type, | ||
| RateLimiterKeyTypes.INTEGRATION_WORKSPACE, | ||
| workspaceKey, | ||
| rateLimit.unit | ||
| ); | ||
| const finalKey = `{rate:${rateLimitKey}}:${rateLimit.type}`; | ||
| const cache = getDefaultCache(); | ||
| await cache.delete(finalKey); | ||
narengogi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return c.json({ success: true }); | ||
| } catch (error) { | ||
| console.error('Error deleting cache:', error); | ||
| return c.json({ error: 'Failed to delete cache' }, 500); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Admin routes handler | ||
| * Handles all /admin/* routes | ||
| */ | ||
| export function adminRoutesHandler() { | ||
| const adminApp = new Hono(); | ||
|
|
||
| // Settings routes | ||
| adminApp.get('/settings', getSettingsHandler); | ||
| adminApp.put('/settings', putSettingsHandler); | ||
| adminApp.put( | ||
| '/integrations/ratelimit/:integrationId/reset', | ||
| resetIntegrationRateLimitHandler | ||
| ); | ||
|
|
||
| // Add more admin routes here as needed | ||
| // adminApp.get('/users', getUsersHandler); | ||
| // adminApp.post('/users', createUserHandler); | ||
| // etc. | ||
|
|
||
| return adminApp; | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.