Redis example
This commit is contained in:
parent
ee92a33ede
commit
92e40d88b8
2 changed files with 22 additions and 18 deletions
|
@ -1,24 +1,28 @@
|
|||
import 'package:angel_redis/angel_redis.dart';
|
||||
import 'package:angel_framework/angel_framework.dart';
|
||||
import 'package:angel_framework/http.dart';
|
||||
import 'package:angel_production/angel_production.dart';
|
||||
import 'package:angel_security/angel_security.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:pretty_logging/pretty_logging.dart';
|
||||
import 'package:resp_client/resp_client.dart';
|
||||
import 'package:resp_client/resp_commands.dart';
|
||||
|
||||
main() async {
|
||||
// Logging boilerplate.
|
||||
Logger.root.onRecord.listen(prettyLog);
|
||||
// We run this through angel_production, so that we can have
|
||||
// multiple instances, all using the same Redis queue.
|
||||
main(List<String> args) =>
|
||||
Runner('rate_limit_redis', configureServer).run(args);
|
||||
|
||||
// Create an app, and HTTP driver.
|
||||
var app = Angel(logger: Logger('rate_limit_redis')), http = AngelHttp(app);
|
||||
|
||||
// Create a simple rate limiter that limits users to 5
|
||||
configureServer(Angel app) async {
|
||||
// Create a simple rate limiter that limits users to 10
|
||||
// queries per 30 seconds.
|
||||
//
|
||||
// In this case, we rate limit users by IP address.
|
||||
//
|
||||
// Our Redis store will be used to manage windows.
|
||||
var rateLimiter =
|
||||
InMemoryRateLimiter(5, Duration(seconds: 30), (req, res) => req.ip);
|
||||
var connection = await connectSocket('localhost');
|
||||
var client = RespClient(connection);
|
||||
var service =
|
||||
RedisService(RespCommands(client), prefix: 'rate_limit_redis_example');
|
||||
var rateLimiter = ServiceRateLimiter(
|
||||
10, Duration(seconds: 30), service, (req, res) => req.ip);
|
||||
|
||||
// `RateLimiter.handleRequest` is a middleware, and can be used anywhere
|
||||
// a middleware can be used. In this case, we apply the rate limiter to
|
||||
|
@ -27,10 +31,9 @@ main() async {
|
|||
|
||||
// Basic routes.
|
||||
app
|
||||
..get('/', (req, res) => 'Hello!')
|
||||
..get('/', (req, res) {
|
||||
var instance = req.container.make<InstanceInfo>();
|
||||
res.writeln('This is instance ${instance.id}.');
|
||||
})
|
||||
..fallback((req, res) => throw AngelHttpException.notFound());
|
||||
|
||||
// Start the server.
|
||||
await http.startServer('127.0.0.1', 3000);
|
||||
print('Rate limiting (redis) example listening at ${http.uri}');
|
||||
}
|
||||
|
|
|
@ -21,7 +21,8 @@ class ServiceRateLimiter<Id> extends RateLimiter<Id> {
|
|||
FutureOr<RateLimitingWindow<Id>> getCurrentWindow(
|
||||
RequestContext req, ResponseContext res, DateTime currentTime) async {
|
||||
var id = await getId(req, res);
|
||||
var existing = await service.read(id);
|
||||
var existing = await service.read(id).catchError((_) => null,
|
||||
test: (e) => e is AngelHttpException && e.statusCode == 404);
|
||||
if (existing != null) {
|
||||
return RateLimitingWindow.fromJson(existing);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue