-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.dart
More file actions
80 lines (72 loc) · 2.89 KB
/
server.dart
File metadata and controls
80 lines (72 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import 'dart:convert';
import 'dart:io';
void main() async {
final server = await HttpServer.bind(InternetAddress.anyIPv4, 3000);
print('Server listening on port ${server.port}');
await for (HttpRequest request in server) {
// Log all incoming requests for debugging
print('${DateTime.now()}: ${request.method} ${request.uri.path} - Headers: ${request.headers.toString()}');
// Add CORS headers
request.response.headers.add('Access-Control-Allow-Origin', '*');
request.response.headers.add('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
request.response.headers.add('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if (request.method == 'OPTIONS') {
// Handle CORS preflight requests
request.response.statusCode = 200;
await request.response.close();
continue;
}
if ((request.uri.path == '/api/health' || request.uri.path == '/health') &&
(request.method == 'GET' || request.method == 'HEAD')) {
// Health check endpoint for the backend itself (handle both GET and HEAD methods)
print('Health endpoint hit successfully! Method: ${request.method}, Path: ${request.uri.path}');
request.response.headers.contentType = ContentType.json;
// For HEAD requests, we only set headers but don't write body content
if (request.method == 'GET') {
request.response.write(jsonEncode({
'status': 'healthy',
'timestamp': DateTime.now().toIso8601String(),
'service': 'microservice-dashboard-backend',
'path': request.uri.path
}));
}
} else if ((request.uri.path == '/api/services' || request.uri.path == '/services') && request.method == 'GET') {
// Return hardcoded services list (handle both /api/services and /services)
print('Services endpoint hit! Path: ${request.uri.path}');
final services = [
{
'name': 'Dashboard Backend',
'url': '/api/health',
},
{
'name': 'Google DNS',
'url': 'https://8.8.8.8',
},
{
'name': 'Public API',
'url': 'https://api.publicapis.org/entries',
},
{
'name': 'GitHub API',
'url': 'https://api.github.com',
},
{
'name': 'JSONPlaceholder',
'url': 'https://jsonplaceholder.typicode.com/posts/1',
},
{
'name': 'HttpBin',
'url': 'https://httpbin.org/status/200',
},
];
request.response.headers.contentType = ContentType.json;
request.response.write(jsonEncode(services));
} else {
// Return 404 for other paths with debugging info
print('404 - Path not found: ${request.uri.path}');
request.response.statusCode = 404;
request.response.write('Not Found - Requested path: ${request.uri.path}');
}
await request.response.close();
}
}