1 /** 2 This module provides an entry point 3 to be used whenever the package is built as 4 a standalone executable. 5 6 Copyright: Francesco mecca 7 License: Public Domain 8 Authors: Francesco Mecca 9 */ 10 11 module main; 12 13 import std.getopt, core.stdc.stdlib, std.stdio, std.file; 14 15 import dummy_web_server; 16 17 enum string NAME = "dummy-web-server"; 18 static string cwd = null; 19 20 static string portHelp = "listen to a different interface. Default: 8080"; 21 static string hostHelp = "listen to a different interface. Default: 127.0.0.1"; 22 static string verboseHelp = ""; 23 24 /** 25 Serve specified file or directory 26 */ 27 void 28 main (string[] args) 29 { 30 ushort port = 8080; 31 auto host = "127.0.0.1"; 32 auto file = "."; 33 bool verbose = false; 34 auto opts = getopt ( 35 args, 36 std.getopt.config.passThrough, std.getopt.config.caseSensitive, 37 "port|p", portHelp, &port, 38 "host|H", hostHelp, &host, 39 "verbose|v", verboseHelp, &verbose, 40 ); 41 42 if (args.length == 2) file = args[1]; 43 if (opts.helpWanted) print_usage_exit (0); 44 if (port < 0 || args.length > 2) print_usage_exit (2); 45 if (!file.exists) { 46 stderr.writeln (NAME ~ ": cannot access '" ~ file ~ "': No such file or directory"); 47 exit (1); 48 } 49 (new Serve(host, port, file, verbose)).serve(); 50 } 51 52 /** 53 Print usage options 54 */ 55 56 void 57 print_usage_exit (ushort status) 58 { 59 auto usage = "Usage: " ~ NAME ~ " [OPTION]... [FILE]...\nServe a file or a directory over HTTP.\n\n" ~ 60 "-p, --port\t" ~ portHelp ~ "\n" ~ 61 "-h, --host\t" ~ hostHelp ~ "\n" ~ 62 "-v, --verbose\t" ~ verboseHelp ~ "\n" ~ 63 "[file|dir]\tspecify a file or a directory to serve"; 64 writeln (usage); 65 exit (status); 66 }