RhythmBox/lib/services/server/server.dart

49 lines
1.2 KiB
Dart
Raw Normal View History

2024-08-26 17:49:05 +00:00
import 'dart:developer';
import 'dart:io';
import 'dart:math' hide log;
import 'package:flutter/foundation.dart';
import 'package:get/get.dart' hide Response;
import 'package:rhythm_box/services/rhythm_media.dart';
import 'package:rhythm_box/services/server/routes/playback.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';
class PlaybackServerProvider extends GetxController {
2024-08-26 18:22:47 +00:00
final int port = Random().nextInt(17500) + 5000;
2024-08-26 17:49:05 +00:00
HttpServer? _server;
Router? _router;
@override
void onInit() {
_initServer();
super.onInit();
}
Future<void> _initServer() async {
const pipeline = Pipeline();
if (kDebugMode) {
pipeline.addMiddleware(logRequests());
}
RhythmMedia.serverPort = port;
_router = Router();
2024-08-27 06:48:31 +00:00
_router!.get('/ping', (Request request) => Response.ok('pong'));
2024-08-26 17:49:05 +00:00
_router!.get(
2024-08-27 06:48:31 +00:00
'/stream/<trackId>',
2024-08-26 17:49:05 +00:00
Get.find<ServerPlaybackRoutesProvider>().getStreamTrackId,
);
_server = await serve(
pipeline.addHandler(_router!.call),
InternetAddress.anyIPv4,
port,
);
log('[Playback] Playback server at http://${_server!.address.host}:${_server!.port}');
}
}