Skip to content

Instantly share code, notes, and snippets.

@graphicbeacon
Created December 31, 2019 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphicbeacon/c42af9c013ef656c5f427463f7db1996 to your computer and use it in GitHub Desktop.
Save graphicbeacon/c42af9c013ef656c5f427463f7db1996 to your computer and use it in GitHub Desktop.
"Dart2Native File Server Tutorial" solution code for tutorial on YouTube
import 'dart:io';
import 'package:args/args.dart';
import 'package:http_server/http_server.dart';
main(List<String> arguments) async {
ArgParser parser = ArgParser()
..addOption('dir', defaultsTo: 'web')
..addOption('port', defaultsTo: '8085');
var parsedResults = parser.parse(arguments);
var serveDir = parsedResults['dir'];
var port = int.tryParse(parsedResults['port']);
HttpServer server = await HttpServer.bind('localhost', port);
VirtualDirectory virDir = VirtualDirectory(serveDir)
..allowDirectoryListing = true;
virDir.directoryHandler = (dir, request) {
var indexUri = Uri.file(dir.path).resolve('index.html');
virDir.serveFile(File(indexUri.toFilePath()), request);
};
await for (HttpRequest request in server) {
await virDir.serveRequest(request);
}
}
name: static_file_server
description: A sample command-line application.
version: 1.0.0
homepage: https://www.creativebracket.com
author: Jermaine Oppong
environment:
sdk: '>=2.5.0 <3.0.0'
dependencies:
args: ^1.5.2
http_server: ^0.9.8+3
path: ^1.6.0
dev_dependencies:
pedantic: ^1.8.0
test: ^1.6.0
@graphicbeacon
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment