37 lines
911 B
Dart
Executable File
37 lines
911 B
Dart
Executable File
import 'dart:io';
|
|
|
|
import 'package:riverpod/riverpod.dart';
|
|
import 'package:shelf/shelf.dart';
|
|
import 'package:shelf/shelf_io.dart' as shelf_io;
|
|
|
|
import 'package:backend_dart/infrastructure/config/config.dart';
|
|
|
|
import 'router.dart';
|
|
|
|
class Server {
|
|
final Config config;
|
|
|
|
Server(this.config);
|
|
|
|
Future<HttpServer> start() async {
|
|
final container = ProviderContainer();
|
|
// Load the router from the router file
|
|
final router = getRouter(container);
|
|
|
|
// Define the pipeline and attach the router
|
|
final handler = Pipeline()
|
|
.addMiddleware(logRequests()) // Logs all incoming requests
|
|
.addHandler(router.call);
|
|
|
|
// Start the server
|
|
final server = await shelf_io.serve(
|
|
handler,
|
|
InternetAddress.anyIPv4,
|
|
int.parse(config.port),
|
|
);
|
|
|
|
print('Server listening on port ${server.port}');
|
|
return server;
|
|
}
|
|
}
|