2024-09-23 01:44:59 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'package:platform_framework/platform_framework.dart';
|
|
|
|
import 'package:platform_framework/http.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
2024-09-23 04:39:29 +00:00
|
|
|
Future<void> apiConfigurer(Protevus app) async {
|
2024-09-23 01:44:59 +00:00
|
|
|
app.get('/', (req, res) => 'Hello, API!');
|
|
|
|
app.fallback((req, res) {
|
|
|
|
return 'fallback on ${req.uri} (within the API)';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-09-23 04:39:29 +00:00
|
|
|
Future<void> frontendConfigurer(Protevus app) async {
|
2024-09-23 01:44:59 +00:00
|
|
|
app.fallback((req, res) => '(usually an index page would be shown here.)');
|
|
|
|
}
|
|
|
|
|
|
|
|
void main() async {
|
|
|
|
// Logging set up/boilerplate
|
|
|
|
hierarchicalLoggingEnabled = true;
|
|
|
|
//Logger.root.onRecord.listen(prettyLog);
|
|
|
|
|
2024-09-23 20:35:32 +00:00
|
|
|
var app = Protevus(logger: Logger('protevus'));
|
2024-09-23 04:39:29 +00:00
|
|
|
var http = ProtevusHttp(app);
|
2024-09-23 01:44:59 +00:00
|
|
|
var multiHost = HostnameRouter.configure({
|
|
|
|
'api.localhost:3000': apiConfigurer,
|
|
|
|
'localhost:3000': frontendConfigurer,
|
|
|
|
});
|
|
|
|
|
|
|
|
app
|
|
|
|
..fallback(multiHost.handleRequest)
|
|
|
|
..fallback((req, res) {
|
|
|
|
res.write('Uncaught hostname: ${req.hostname}');
|
|
|
|
});
|
|
|
|
|
|
|
|
app.errorHandler = (e, req, res) {
|
|
|
|
print(e.message);
|
|
|
|
print(e.stackTrace);
|
|
|
|
return e.toJson();
|
|
|
|
};
|
|
|
|
|
|
|
|
await http.startServer('127.0.0.1', 3000);
|
|
|
|
print('Listening at ${http.uri}');
|
|
|
|
print('See what happens when you visit http://localhost:3000 instead '
|
|
|
|
'of http://127.0.0.1:3000. Then, try '
|
|
|
|
'http://api.localhost:3000.');
|
|
|
|
}
|