een lange titel

Een Dart webserver voorbeeld in Docker

Dart is de scripting taal van Google.

Dit voorbeeld laat zien hoe je met een kort Dart programma een statische webserver kan maken.

Dit is de broncode static_file_server.dart:

// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// Type localhost:4048 into your browser.
// This server returns the contents of index.html for all requests.

import 'dart:io';
import 'dart:async';
import 'package:http_server/http_server.dart';
import 'package:path/path.dart';

Future main() async 
{
  var pathToBuild = join(dirname(Platform.script.toFilePath()));

  var staticFiles = new VirtualDirectory(pathToBuild);
  staticFiles.allowDirectoryListing = true;
  staticFiles.directoryHandler = (dir, request) 
  {
    print('request');
    var indexUri = new Uri.file(dir.path).resolve('index.html');
    staticFiles.serveFile(new File(indexUri.toFilePath()), request);
  };

  var server = await HttpServer.bind(InternetAddress.ANY_IP_V4, 4048);
  print('Listening on port 4048');
  await server.forEach(staticFiles.serveRequest);
  print('Stop listening on port 4048');
}

Dit is de Dockerfile. De gebruikte Docker image is google/dart. Deze wordt door Google ter beschikking gesteld.

FROM google/dart

WORKDIR /app

ADD pubspec.* /app/
RUN pub get
ADD . /app
RUN pub get --offline

EXPOSE 4048

CMD []
ENTRYPOINT ["/usr/bin/dart", "bin/static_file_server.dart"]