-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update server * HijackException
- Loading branch information
Showing
19 changed files
with
825 additions
and
519 deletions.
There are no files selected for viewing
This file contains 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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,61 @@ | ||
import 'dart:convert'; | ||
import 'dart:io' as io; | ||
|
||
import 'package:shelf/shelf.dart'; | ||
import 'package:stack_trace/stack_trace.dart' as st; | ||
import 'package:ws_server/src/util/responses.dart'; | ||
|
||
/// Response encoder | ||
final Converter<Map<String, Object?>, String> _responseEncoder = | ||
const JsonEncoder().cast<Map<String, Object?>, String>(); | ||
|
||
/// Middleware that catches all errors and sends a JSON response with the error | ||
/// message. If the error is not an instance of [HttpException], it will be | ||
/// wrapped into one with the status code 500. | ||
Middleware handleErrors({bool showStackTrace = false}) => (Handler handler) => | ||
(Request request) => Future.sync(() => handler(request)) | ||
.then<Response>((Response response) => response) | ||
.catchError( | ||
(Object error, StackTrace? stackTrace) { | ||
final result = error is HttpException | ||
? error | ||
: HttpException( | ||
statusCode: io.HttpStatus.internalServerError, | ||
code: 'internal', | ||
message: 'Internal Server Error', | ||
data: <String, Object?>{ | ||
'path': request.url.path, | ||
'query': request.url.queryParameters, | ||
'method': request.method, | ||
'headers': request.headers, | ||
'error': showStackTrace | ||
? error.toString() | ||
: _errorRepresentation(error), | ||
if (showStackTrace && stackTrace != null) | ||
'stack_trace': st.Trace.format(stackTrace), | ||
}, | ||
); | ||
return Response( | ||
result.statusCode, | ||
body: _responseEncoder.convert(result.toJson()), | ||
headers: <String, String>{ | ||
'Content-Type': io.ContentType.json.value, | ||
}, | ||
); | ||
}, | ||
test: (Object error) => error is HijackException ? false : true, | ||
); | ||
|
||
String _errorRepresentation(Object? error) => switch (error) { | ||
FormatException _ => 'Format exception', | ||
HttpException _ => 'HTTP exception', | ||
UnimplementedError _ => 'Unimplemented error', | ||
UnsupportedError _ => 'Unsupported error', | ||
RangeError _ => 'Range error', | ||
StateError _ => 'State error', | ||
ArgumentError _ => 'Argument error', | ||
TypeError _ => 'Type error', | ||
OutOfMemoryError _ => 'Out of memory error', | ||
StackOverflowError _ => 'Stack overflow error', | ||
_ => 'Unknown error', | ||
}; |
This file contains 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,6 @@ | ||
import 'package:l/l.dart'; | ||
import 'package:shelf/shelf.dart' as shelf; | ||
|
||
shelf.Middleware logPipeline() => shelf.logRequests( | ||
logger: (msg, isError) => isError ? l.w(msg) : l.i(msg), | ||
); |
This file contains 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 |
---|---|---|
@@ -1,10 +1,6 @@ | ||
import 'dart:io' as io; | ||
import 'dart:async'; | ||
|
||
import 'package:shelf/shelf.dart' show Request, Response; | ||
import 'package:ws_server/src/util/responses.dart'; | ||
|
||
Response $healthCheck(Request request) => Response.ok( | ||
'{"data": {"status": "ok"}}', | ||
headers: <String, String>{ | ||
'Content-Type': io.ContentType.json.value, | ||
}, | ||
); | ||
FutureOr<Response> $healthCheck(Request request) => Responses.ok(null); |
This file contains 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,19 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:shelf/shelf.dart' as shelf; | ||
import 'package:ws_server/src/util/responses.dart'; | ||
|
||
FutureOr<shelf.Response> $longPolling(shelf.Request request) async { | ||
final requestTime = DateTime.now().toUtc(); | ||
final ms = switch (request.url.queryParameters['duration']) { | ||
String value => int.tryParse(value) ?? 12000, | ||
_ => 12000, | ||
}; | ||
await Future<void>.delayed(Duration(milliseconds: ms)); | ||
final responseTime = DateTime.now().toUtc(); | ||
return Responses.ok(<String, Object?>{ | ||
'request': requestTime.toIso8601String(), | ||
'response': responseTime.toIso8601String(), | ||
'duration': responseTime.difference(requestTime).inMilliseconds.abs(), | ||
}); | ||
} |
Oops, something went wrong.