Skip to content

Instantly share code, notes, and snippets.

@lukas-h
Created August 15, 2024 09:44
Show Gist options
  • Save lukas-h/4dd0cbf05239790c4bde64a5bf28f77d to your computer and use it in GitHub Desktop.
Save lukas-h/4dd0cbf05239790c4bde64a5bf28f77d to your computer and use it in GitHub Desktop.
import 'package:cron_consume_microservice/cron_consume_microservice.dart'
as cron_consume_microservice;
import 'dart:convert';
import 'dart:io';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_router/shelf_router.dart';
void main(List<String> arguments) {
// Get the PORT from environment variable
var port = int.parse(Platform.environment['PORT'] ?? '8080');
var router = Router();
router.post('/cron_consume', (shelf.Request request) async {
try {
final requestData = await request.readAsString();
print("Received data: $requestData"); // Logging the incoming payload
// Check if requestData is not empty and valid JSON
if (requestData.isEmpty || !requestData.contains('message')) {
return shelf.Response(400, body: 'Empty or invalid request payload.');
}
final event = json.decode(requestData);
// Validate the message structure
if (!event['message'].containsKey('data')) {
return shelf.Response(400, body: 'Invalid message structure.');
}
final base64Message = event['message']['data'];
// Directly decode the base64 message
final decodedMessage = utf8.decode(base64.decode(base64Message));
final messageJson = json.decode(decodedMessage);
if (!messageJson.containsKey('layerList')) {
return shelf.Response(400,
body: 'Layer list missing in decoded message.');
}
if (!messageJson.containsKey('locationList')) {
return shelf.Response(400,
body: 'Area list field missing in decoded message.');
}
final List<String> layerList = List<String>.from(messageJson['layerList']);
final List<String> locationList = List<String>.from(messageJson['locationList']);
var cron = cron_consume_microservice.CronConsumeMicroservice();
cron.consumeCronJob(layerList, locationList);
return shelf.Response.ok("Success");
} on FormatException catch (e) {
print("Format error: ${e.toString()}"); // Logging specific format error
return shelf.Response(400, body: 'Format error: ${e.toString()}');
} catch (e) {
print("Error: ${e.toString()}"); // Logging any general error
return shelf.Response(400, body: e.toString());
}
});
// Router to test locally using postman
router.post('/cron_consume_local', (shelf.Request request) async {
try {
final requestDataString = await request.readAsString();
final Map<String, dynamic> requestData = json.decode(requestDataString);
final List<String> layerList =
List<String>.from(requestData['layerList']);
final List<String> locationList =
List<String>.from(requestData['locationList']);
var cron = cron_consume_microservice.CronConsumeMicroservice();
cron.consumeCronJob(layerList, locationList);
return shelf.Response.ok("Success");
} on FormatException catch (e) {
print("Format error: ${e.toString()}"); // Logging specific format error
return shelf.Response(400, body: 'Format error: ${e.toString()}');
} catch (e) {
print("Error: ${e.toString()}"); // Logging any general error
return shelf.Response(400, body: e.toString());
}
});
var handler = const shelf.Pipeline()
.addMiddleware(shelf.logRequests())
.addHandler(router);
// Start server on port
io.serve(handler, '0.0.0.0', port).then((server) {
print('Serving at http://${server.address.host}:${server.port}');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment