Created
December 31, 2019 15:49
-
-
Save graphicbeacon/c42af9c013ef656c5f427463f7db1996 to your computer and use it in GitHub Desktop.
"Dart2Native File Server Tutorial" solution code for tutorial on YouTube
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Watch full tutorial