diff options
author | ryo <ryo@nopwd.lol> | 2025-01-06 17:17:26 +0000 |
---|---|---|
committer | ryo <ryo@nopwd.lol> | 2025-01-06 17:17:26 +0000 |
commit | bb2cb9683b011adca411e5c805fbb87fc0867e14 (patch) | |
tree | 83a8241759baaab808a2647899666a2bfc9aae3b /server.dart |
Initial commit
Diffstat (limited to 'server.dart')
-rw-r--r-- | server.dart | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/server.dart b/server.dart new file mode 100644 index 0000000..eb5307d --- /dev/null +++ b/server.dart @@ -0,0 +1,44 @@ +import 'dart:convert'; +import 'dart:io'; + +void main() async { + const int port = 6969; + final requests = await HttpServer.bind('localhost', port); + print('Started server bind to localhost:$port'); + await for (final request in requests) { + print('uri: ${request.uri}'); + if (request.method == 'GET') { + print('Processing GET method'); + processGet(request); + } else if (request.method == 'POST') { + print('Processing POST method'); + processPost(request); + } + } +} + +void processGet(HttpRequest request) async { + var filename = request.uri.toString().substring(1); + bool exist = await File(filename).exists(); + String data = ''; + if (exist) { + var data = await File(filename).readAsString(); + print('Sent data:'); + print(data); + } else { + File(filename).writeAsString(''); + print('Created file $filename'); + } + request.response + ..statusCode = HttpStatus.ok + ..write(data) + ..close(); +} + +void processPost(HttpRequest request) async { + var filename = request.uri.path.substring(1); + String data = await utf8.decoder.bind(request).join(); + File(filename).writeAsString(data); + print('Received data:'); + print(data); +} |