Skip to content

Commit

Permalink
Merge pull request #90 from javad-zobeidi/dev
Browse files Browse the repository at this point in the history
SSE response anf Fix UUID issue
  • Loading branch information
javad-zobeidi authored Jul 3, 2024
2 parents 9b21822 + 4448175 commit af560eb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/src/database/migration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 35 additions & 0 deletions lib/src/http/response/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum ResponseType {
json,
none,
html,
sse,
streamFile,
download,
}
Expand All @@ -29,6 +30,25 @@ class Response {
this.headers = const {},
});

@protected
Future<void> 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) {
Expand All @@ -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'],
Expand Down Expand Up @@ -134,6 +157,18 @@ class Response {
headers: headers,
);

static sse(
Stream<dynamic> eventStream, {
int statusCode = HttpStatus.ok,
Map<String, String> headers = const {},
}) =>
Response(
data: eventStream,
responseType: ResponseType.sse,
httpStatusCode: statusCode,
headers: headers,
);

static download(
String fileName,
Uint8List bytes, {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/route/route_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit af560eb

Please sign in to comment.