-
Notifications
You must be signed in to change notification settings - Fork 0
Migrate from generig data route into a restful one #34
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
Merged
fulleni
merged 44 commits into
main
from
migrate-from-generig-data-route-into-a-restful-one
Aug 5, 2025
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
68160ec
feat(api): implement headlines collection endpoint
fulleni 75bdb02
feat(api): add middleware for headlines route
fulleni 415abf3
feat(api): implement headlines item endpoint
fulleni 699be85
feat(middlewares): add middlewares for headline entity
fulleni 91353e0
feat(api): implement countries endpoint
fulleni c322fed
feat(api): implement dashboard summary endpoint
fulleni 9384abe
refactor(api): improve permission handling for headline endpoints
fulleni 7510a30
feat(api): implement languages endpoint
fulleni 6d6e420
feat(api): implement remote-configs endpoints
fulleni d0ad33f
feat(api): implement sources CRUD endpoints
fulleni 83c0be0
feat(api): implement topics CRUD endpoints
fulleni 7aa22a9
feat(api): implement users endpoint
fulleni 1231089
feat(api): implement user preferences and settings endpoints
fulleni b3f7472
refactor(middlewares): simplify authorization middleware
fulleni 37b03b1
refactor(middlewares): simplify user ownership check middleware
fulleni eb31c14
refactor(users): apply ownership check middleware to user endpoints
fulleni 860ce8f
refactor: remove unused middleware file
fulleni 4312c91
refactor(middleware): add comments to countries middleware
fulleni 454bdd6
refactor(headlines): simplify permission logic in middleware
fulleni 8426d2e
refactor(api): add comments to explain languages endpoint restrictions
fulleni 1431b8c
docs(middleware): add documentation for sources middleware
fulleni cb7c212
docs(middleware): add RBAC topics permissions description
fulleni f634ab8
refactor(users): improve middleware for route group
fulleni 57114ea
feat(middleware): enhance user preferences endpoint security
fulleni f94dbde
feat(middleware): enhance user settings endpoint security
fulleni d8bccd1
refactor(remote-config): implement singleton pattern for remote confi…
fulleni 7ab8eca
chore: misc
fulleni c0573a1
lint: misc
fulleni 3fec2c0
refactor(middlewares): remove unused ModelRegistry import and usage
fulleni d19dedd
refactor(env): update rate limit configuration examples
fulleni 1b60d05
chore(env): decrease default rate limit values
fulleni 5749d7f
feat(config): implement separate rate limits for read and write opera…
fulleni a66acba
feat(middlewares): implement configurable rate limiter with RBAC support
fulleni cf3a406
feat(headlines): apply rate limiting to headline endpoints
fulleni c94480e
feat(countries): implement rate limiting for country routes
fulleni 4f33218
style: remove unnecessary break statements in middleware
fulleni e4dbb40
feat(languages): apply rate limiting to middleware
fulleni 8289d10
feat(remote-config): implement rate limiting for API routes
fulleni 9782b62
feat(sources): implement rate limiting for sources endpoints
fulleni 6228ce2
feat(topics): implement rate limiting for API endpoints
fulleni d470576
feat(users): implement rate limiting for users endpoint
fulleni 0c7578a
feat(middlewares): enhance user preferences endpoint with rate limiting
fulleni f44bf42
feat(middleware): enhance user settings endpoint with rate limiting
fulleni 43a4978
docs(README): update data management API description
fulleni 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
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
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
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
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,76 @@ | ||
import 'package:core/core.dart'; | ||
import 'package:dart_frog/dart_frog.dart'; | ||
import 'package:flutter_news_app_api_server_full_source_code/src/config/environment_config.dart'; | ||
import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/rate_limiter_middleware.dart'; | ||
import 'package:flutter_news_app_api_server_full_source_code/src/rbac/permission_service.dart'; | ||
import 'package:flutter_news_app_api_server_full_source_code/src/rbac/permissions.dart'; | ||
|
||
/// A key extractor that uses the authenticated user's ID. | ||
/// | ||
/// This should be used for routes that are protected by authentication, | ||
/// ensuring that the rate limit is applied on a per-user basis. | ||
Future<String?> _userKeyExtractor(RequestContext context) async { | ||
return context.read<User>().id; | ||
} | ||
|
||
/// A role-aware middleware factory that applies a rate limit only if the | ||
/// authenticated user does not have the `rateLimiting.bypass` permission. | ||
Middleware _createRoleAwareRateLimiter({ | ||
required int limit, | ||
required Duration window, | ||
required Future<String?> Function(RequestContext) keyExtractor, | ||
}) { | ||
return (handler) { | ||
return (context) { | ||
// Read dependencies from the context. | ||
final permissionService = context.read<PermissionService>(); | ||
final user = context.read<User>(); // Assumes user is authenticated | ||
|
||
// Check for the bypass permission. | ||
if (permissionService.hasPermission(user, Permissions.rateLimitingBypass)) { | ||
// If the user has the bypass permission, skip the rate limiter. | ||
return handler(context); | ||
} | ||
|
||
// If the user does not have the bypass permission, apply the rate limiter. | ||
return rateLimiter( | ||
limit: limit, | ||
window: window, | ||
keyExtractor: keyExtractor, | ||
)(handler)(context); | ||
}; | ||
}; | ||
} | ||
|
||
/// Creates a pre-configured, role-aware rate limiter for READ operations. | ||
/// | ||
/// This middleware will: | ||
/// 1. Check if the authenticated user has the `rateLimiting.bypass` permission. | ||
/// If so, the check is skipped. | ||
/// 2. If not, it applies the rate limit defined by `RATE_LIMIT_READ_LIMIT` | ||
/// and `RATE_LIMIT_READ_WINDOW_MINUTES` from the environment. | ||
/// 3. It uses the authenticated user's ID as the key for the rate limit. | ||
Middleware createReadRateLimiter() { | ||
return _createRoleAwareRateLimiter( | ||
limit: EnvironmentConfig.rateLimitReadLimit, | ||
window: EnvironmentConfig.rateLimitReadWindow, | ||
keyExtractor: _userKeyExtractor, | ||
); | ||
} | ||
|
||
/// Creates a pre-configured, role-aware rate limiter for WRITE operations. | ||
/// | ||
/// This middleware will: | ||
/// 1. Check if the authenticated user has the `rateLimiting.bypass` permission. | ||
/// If so, the check is skipped. | ||
/// 2. If not, it applies the stricter rate limit defined by | ||
/// `RATE_LIMIT_WRITE_LIMIT` and `RATE_LIMIT_WRITE_WINDOW_MINUTES` from | ||
/// the environment. | ||
/// 3. It uses the authenticated user's ID as the key for the rate limit. | ||
Middleware createWriteRateLimiter() { | ||
return _createRoleAwareRateLimiter( | ||
limit: EnvironmentConfig.rateLimitWriteLimit, | ||
window: EnvironmentConfig.rateLimitWriteWindow, | ||
keyExtractor: _userKeyExtractor, | ||
); | ||
} |
Oops, something went wrong.
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.