diff --git a/lib/src/database/migration.dart b/lib/src/database/migration.dart index 20135a5..2f35cc4 100644 --- a/lib/src/database/migration.dart +++ b/lib/src/database/migration.dart @@ -79,7 +79,6 @@ class Migration { : ''; query.write( '''DROP TABLE IF EXISTS `$name`; CREATE TABLE `$name` (${_queries.join(',')}$primary$index$foreig)'''); - String sqlQuery = query.toString(); if (MigrationConnection().database?.driver == 'Postgresql') { sqlQuery = _mysqlToPosgresqlMapper(sqlQuery); @@ -762,17 +761,16 @@ class Migration { void uuid( String name, { bool nullable = false, - String? defaultValue, + int length = 36, String? comment, String? collation, String? expression, String? virtuality, }) { - addColumn( + char( name, - 'UUID', nullable: nullable, - defaultValue: defaultValue, + length: length, comment: comment, collation: collation, expression: expression, diff --git a/lib/src/http/response/response.dart b/lib/src/http/response/response.dart index 726ca16..66f239d 100644 --- a/lib/src/http/response/response.dart +++ b/lib/src/http/response/response.dart @@ -8,6 +8,7 @@ enum ResponseType { json, none, html, + sse, streamFile, download, } @@ -29,6 +30,25 @@ class Response { this.headers = const {}, }); + @protected + Future sseHandler(HttpResponse res) async { + res.headers.contentType = ContentType.parse('text/event-stream'); + res.headers.add(HttpHeaders.cacheControlHeader, 'no-cache'); + res.headers.add(HttpHeaders.connectionHeader, 'keep-alive'); + res.headers.add(HttpHeaders.transferEncodingHeader, 'chunked'); + + void writeSSE(String data) { + res.add(utf8.encode('data: $data\n\n')); + } + + await for (var event in data) { + writeSSE(jsonEncode(event)); + await res.flush(); + } + + await res.close(); + } + void makeResponse(HttpResponse res) async { res.statusCode = httpStatusCode; if (headers.isNotEmpty) { @@ -51,6 +71,9 @@ class Response { res.write(data); await res.close(); break; + case ResponseType.sse: + await sseHandler(res); + break; case ResponseType.streamFile: StreamFile? stream = StreamFile( fileName: data['fileName'], @@ -134,6 +157,18 @@ class Response { headers: headers, ); + static sse( + Stream eventStream, { + int statusCode = HttpStatus.ok, + Map headers = const {}, + }) => + Response( + data: eventStream, + responseType: ResponseType.sse, + httpStatusCode: statusCode, + headers: headers, + ); + static download( String fileName, Uint8List bytes, { diff --git a/lib/src/route/route_handler.dart b/lib/src/route/route_handler.dart index 054717f..7a7ea22 100644 --- a/lib/src/route/route_handler.dart +++ b/lib/src/route/route_handler.dart @@ -67,10 +67,12 @@ RouteData? _getMatchRoute(String inputRoute, String method, String? domain) { .trim() .replaceFirst(RegExp(r'^/'), '') .replaceAll('//', '/') + .replaceAll(RegExp(r'/$'), '') .replaceFirst(RegExp(r'/$'), '/'); inputRoute = inputRoute .replaceFirst(RegExp(r'^/'), '') .replaceAll('//', '/') + .replaceAll(RegExp(r'/$'), '') .replaceFirst(RegExp(r'/$'), ''); if (route.prefix != null) {