Skip to content

Commit

Permalink
feat: add logs endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mcquenji committed Nov 28, 2024
1 parent a434905 commit cfe0951
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
8 changes: 4 additions & 4 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ void main(List<String> args) async {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen(debugLogHandler);

if (kDebugMode) {
Logger.root.warning('Running in debug mode');
}

try {
await prisma.$connect();

Expand All @@ -32,6 +28,10 @@ void main(List<String> args) async {
],
);

if (kDebugMode) {
Logger.root.warning('Running in debug mode');
}

setPrintResolver((msg) {
final logger = Logger('Modular');
final parts = msg.split(' ');
Expand Down
9 changes: 8 additions & 1 deletion lib/modules/admin/admin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class AdminModule extends Module {
..resource(ProductsResource(), name: '/products')
..resource(FeaturesResource(), name: '/features')
..resource(PaymentsResource(), name: '/payments')
..resource(ClientKeyResource(), name: '/client-keys');
..resource(ClientKeyResource(), name: '/client-keys')
..websocket(
'/logs',
websocket: LogsResource(),
middlewares: [
WebsocketMarkerMiddleware(),
],
);
}
}
40 changes: 40 additions & 0 deletions lib/modules/admin/presentation/resources/logs_resource.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:async';

Check warning on line 1 in lib/modules/admin/presentation/resources/logs_resource.dart

View workflow job for this annotation

GitHub Actions / Lint Check

lib/modules/admin/presentation/resources/logs_resource.dart is not formatted
import 'dart:convert';

import 'package:echidna_dto/echidna_dto.dart';
import 'package:logging/logging.dart';
import 'package:shelf_modular/shelf_modular.dart';

/// Logs endpoint that broadcasts logs to connected clients.
class LogsResource extends WebSocketResource {
final List<ServerLog> _logs = [];

/// Logs endpoint that broadcasts logs to connected clients.
LogsResource() {
Logger.root.onRecord.listen(listener);

Timer.periodic(
const Duration(hours: 1),
(_) {
_logs.removeWhere((log) => log.time.isBefore(DateTime.now().subtract(const Duration(days: 1))));
},
);
}

@override
void connect(WebSocket socket) {
socket.sink.add(jsonEncode(_logs));
}

@override
void disconnect(WebSocket socket) {}

@override
void onMessage(dynamic data, WebSocket socket) {}

void listener(LogRecord record) {

Check warning on line 35 in lib/modules/admin/presentation/resources/logs_resource.dart

View workflow job for this annotation

GitHub Actions / Lint Check

Missing documentation for a public member.. See https://dart.dev/tools/diagnostic-messages#public_member_api_docs
_logs.add(ServerLog.fromLogRecord(record));

broadcast(jsonEncode(_logs));
}
}
3 changes: 2 additions & 1 deletion lib/modules/admin/presentation/resources/resources.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export 'client_key_resource.dart';
export 'customers_resource.dart';
export 'features_resource.dart';
export 'licenses_resource.dart';
export 'logs_resource.dart';
export 'payments_resource.dart';
export 'products_resource.dart';
export 'products_resource.dart';

Check warning on line 7 in lib/modules/admin/presentation/resources/resources.dart

View workflow job for this annotation

GitHub Actions / Lint Check

Missing a newline at the end of the file.. See https://dart.dev/tools/diagnostic-messages#eol_at_end_of_file
16 changes: 15 additions & 1 deletion lib/modules/auth/presentation/guards/auth_guard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ class AuthGuard extends RouteGuard {
FutureOr<bool> canActivate(Request request, Route route) async {
final auth = Modular.get<AuthService>();

final token = request.headers['Authorization']?.split('Bearer ').last;
String? token;

if (route.middlewares.any((element) => element is WebsocketMarkerMiddleware)) {
token = request.url.queryParameters['token'];
} else {
token = request.headers['Authorization']?.split('Bearer ').last;
}

if (token == null) {
return false;
Expand All @@ -23,3 +29,11 @@ class AuthGuard extends RouteGuard {
}
}
}

/// A [ModularMiddleware] that marks a route as a websocket route.
class WebsocketMarkerMiddleware extends ModularMiddleware {
@override
Handler call(Handler handler, [ModularRoute? route]) {
return handler;
}
}

0 comments on commit cfe0951

Please sign in to comment.