import 'dart:io';
import 'package:http_server/http_server.dart' show VirtualDirectory;

final String PATH_TO_WEB_CONTENT = "web";

void main() {
  HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 3031).then((HttpServer server) {
    print("Serving at ${server.address}:${server.port}");
    final root = Platform.script.resolve(PATH_TO_WEB_CONTENT).toFilePath();
    final virDir = new VirtualDirectory(root)
        // The following are needed in dev mode to be able to access
        // Dart packages in the cache.
        ..followLinks = true
        ..jailRoot = false;
    server.listen((HttpRequest req) {
      print("${req.method} ${req.uri};\tcached ${req.headers.ifModifiedSince}");
      (req.method == "PUT" ? processPut : virDir.serveRequest)(req);
    });
  });
}

void processPut(HttpRequest request) {
  String filePath = request.uri.toFilePath();
  final file = new File(PATH_TO_WEB_CONTENT + filePath);
  request.pipe(file.openWrite()).then((_) {
    request.response
        ..statusCode = HttpStatus.NO_CONTENT
        ..close();
    print("Wrote to file '${file.path}'");
  });
}

Add simple HTTP server implementation in support of Chap. 6 exercises
Add simple HTTP server implementation in support of Chap. 6 exercises
