-
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.
Merge pull request #9 from brasizza/main
Sistema de broadcast + Socket server
- Loading branch information
Showing
27 changed files
with
622 additions
and
87 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,59 @@ | ||
{"users":[{"id":1,"name":"Rodrigo Rahman","email":"rodrigorahman@academiadoflutter.com.br","password":"123"},{"id":2,"name":"Guilherme","email":"guilherme@gmail.com","password":"1234"},{"id":3,"name":"Luana Rahman Alterado Patch","email":"luana@academiadoflutter.com.br","password":"123"}],"products":[{"id":0,"title":"Academia do flutter"},{"id":1,"title":"Jornada Dart"},{"id":2,"title":"Jornada GetX"},{"id":3,"title":"Academia do flutter alterado"},{"id":4,"title":"Academia do flutter 2"},{"id":5,"title":"Academia do flutter","user_id":"#userAuthRef"},{"id":6,"title":"Academia do flutter","user_id":"1"},{"id":7,"title":"Academia do flutter","user_id":"1"}],"students":[{"id":1,"name":"Academia do flutter 2"}]} | ||
{ | ||
"users": [ | ||
{ | ||
"id": 0, | ||
"name": "Rodrigo Rahman", | ||
"email": "rodrigorahman@academiadoflutter.com.br", | ||
"password": "123" | ||
}, | ||
{ | ||
"id": 1, | ||
"name": "Guilherme", | ||
"email": "Guilherme@gmail.com", | ||
"password": "1234" | ||
} | ||
], | ||
"products": [ | ||
{ | ||
"id": 0, | ||
"title": "Academia do flutter" | ||
}, | ||
{ | ||
"id": 1, | ||
"title": "Jornada Dart" | ||
}, | ||
{ | ||
"id": 2, | ||
"title": "Jornada GetX" | ||
}, | ||
{ | ||
"id": 3, | ||
"title": "Academia do flutter alterado" | ||
}, | ||
{ | ||
"id": 4, | ||
"title": "Academia do flutter 2" | ||
}, | ||
{ | ||
"id": 5, | ||
"title": "Academia do flutter", | ||
"user_id": "#userAuthRef" | ||
}, | ||
{ | ||
"id": 6, | ||
"title": "Academia do flutter", | ||
"user_id": "1" | ||
}, | ||
{ | ||
"id": 7, | ||
"title": "Academia do flutter", | ||
"user_id": "1" | ||
} | ||
], | ||
"students": [ | ||
{ | ||
"id": 1, | ||
"name": "Academia do flutter 2" | ||
} | ||
] | ||
} |
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,5 @@ | ||
import '../../models/broadcast_model.dart'; | ||
|
||
abstract class BroadcastBase { | ||
Future<bool> execute({required BroadcastModel broadcast}); | ||
} |
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,50 @@ | ||
import 'dart:developer'; | ||
|
||
import 'package:event_bus/event_bus.dart'; | ||
import 'package:json_rest_server/src/core/broadcast/broadcast_base.dart'; | ||
import 'package:json_rest_server/src/core/broadcast/broadcast_factory.dart'; | ||
import 'package:json_rest_server/src/core/enum/broadcast_type.dart'; | ||
import 'package:json_rest_server/src/models/broadcast_model.dart'; | ||
|
||
class BroadCastController { | ||
final EventBus _eventBus = EventBus(); | ||
List<String> _customProviders = []; | ||
|
||
BroadCastController() { | ||
_eventBus.on<BroadcastModel>().listen((BroadcastModel broadCast) { | ||
BroadcastBase? broadcastBase = | ||
BroadcastFactory.create(broadcast: broadCast); | ||
|
||
broadcastBase?.execute(broadcast: broadCast); | ||
}); | ||
} | ||
|
||
BroadCastController single(String provider) { | ||
_customProviders.clear(); | ||
_customProviders.add(provider); | ||
return this; | ||
} | ||
|
||
BroadCastController multi(List<String> provider) { | ||
_customProviders.clear(); | ||
_customProviders = provider.toSet().toList(); | ||
return this; | ||
} | ||
|
||
void execute({List<String>? providers, required BroadcastModel broadcast}) { | ||
if (_customProviders.isNotEmpty) { | ||
providers = _customProviders; | ||
} | ||
if (providers != null) { | ||
for (var provider in providers) { | ||
if (BroadCastType.fromString(provider) == null) { | ||
log('BroadCast provider: $provider not found'); | ||
} else { | ||
_eventBus.fire(broadcast.copyWith( | ||
broadCastType: BroadCastType.fromString(provider))); | ||
} | ||
} | ||
} | ||
_customProviders.clear(); | ||
} | ||
} |
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,26 @@ | ||
import 'package:get_it/get_it.dart'; | ||
import 'package:json_rest_server/src/core/broadcast/broadcast_base.dart'; | ||
import 'package:json_rest_server/src/core/broadcast/slack/slack_broadcast_impl.dart'; | ||
import 'package:json_rest_server/src/core/broadcast/socket/socket_broadcast_impl.dart'; | ||
import 'package:json_rest_server/src/core/enum/broadcast_type.dart'; | ||
import 'package:json_rest_server/src/models/broadcast_model.dart'; | ||
import 'package:json_rest_server/src/models/config_model.dart'; | ||
import 'package:json_rest_server/src/server/socket/socket_handler.dart'; | ||
|
||
class BroadcastFactory { | ||
static BroadcastBase? create({required BroadcastModel broadcast}) { | ||
switch (broadcast.broadCastType) { | ||
case BroadCastType.socket: | ||
return SocketBroadCastImpl( | ||
socket: GetIt.I.isRegistered<SocketHandler>() | ||
? GetIt.I.get<SocketHandler>() | ||
: null, | ||
); | ||
case BroadCastType.slack: | ||
return SlackBroadCastImpl(config: GetIt.I.get<ConfigModel>()); | ||
|
||
default: | ||
return 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,36 @@ | ||
// ignore_for_file: public_member_api_docs, sort_constructors_first | ||
import 'dart:convert'; | ||
import 'dart:developer'; | ||
|
||
import 'package:http/http.dart' as http; | ||
import 'package:json_rest_server/src/models/broadcast_model.dart'; | ||
import 'package:json_rest_server/src/models/config_model.dart'; | ||
|
||
import '../broadcast_base.dart'; | ||
|
||
class SlackBroadCastImpl implements BroadcastBase { | ||
final ConfigModel _config; | ||
SlackBroadCastImpl({ | ||
required ConfigModel config, | ||
}) : _config = config; | ||
@override | ||
Future<bool> execute({required BroadcastModel broadcast}) async { | ||
if (_config.slack != null) { | ||
if (_config.slack!.url.isEmpty) { | ||
return false; | ||
} | ||
Map payload = {}; | ||
if (_config.slack?.channel != null) { | ||
payload['channel'] = _config.slack?.channel ?? ''; | ||
} | ||
payload['text'] = broadcast.toJson(); | ||
final response = await http.post(Uri.parse(_config.slack!.url), | ||
body: json.encode(payload)); | ||
if (response.statusCode == 200) { | ||
log('Send to Slack', time: DateTime.now()); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
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,26 @@ | ||
// ignore_for_file: public_member_api_docs, sort_constructors_first | ||
import 'dart:developer'; | ||
|
||
import 'package:json_rest_server/src/models/broadcast_model.dart'; | ||
import 'package:json_rest_server/src/server/socket/socket_handler.dart'; | ||
|
||
import '../broadcast_base.dart'; | ||
|
||
class SocketBroadCastImpl implements BroadcastBase { | ||
final SocketHandler? _socket; | ||
SocketBroadCastImpl({ | ||
SocketHandler? socket, | ||
}) : _socket = socket; | ||
@override | ||
Future<bool> execute({required BroadcastModel broadcast}) async { | ||
if (_socket == null) { | ||
return false; | ||
} | ||
final sent = _socket!.sendMessage(broadcast.toJson()); | ||
if (sent) { | ||
log('Send to socket', time: DateTime.now()); | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,17 @@ | ||
enum BroadCastType { | ||
socket, | ||
slack; | ||
|
||
static BroadCastType? fromString(String type) { | ||
switch (type.toUpperCase()) { | ||
case 'SOCKET': | ||
return BroadCastType.socket; | ||
|
||
case 'SLACK': | ||
return BroadCastType.slack; | ||
|
||
default: | ||
return null; | ||
} | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
lib/src/core/helper/json_helper.dart → lib/src/core/helper/cors_helper.dart
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
Oops, something went wrong.