diff --git a/examples/HttpServer.cpp b/examples/HttpServer.cpp index abe2fa0..d17e38d 100644 --- a/examples/HttpServer.cpp +++ b/examples/HttpServer.cpp @@ -1,22 +1,75 @@ -/* This is a static HTTP(S) server. It caches the files in given root folder and streams them by request */ +/* This is a simple HTTP(S) web server much like Python's SimpleHTTPServer */ #include /* Helpers for this example */ -#include "helpers/FileCache.h" +#include "helpers/AsyncFileReader.h" #include "helpers/Middleware.h" +/* optparse */ +#define OPTPARSE_IMPLEMENTATION +#include "helpers/optparse.h" + int main(int argc, char **argv) { + int option; + struct optparse options; + optparse_init(&options, argv); + + struct optparse_long longopts[] = { + {"port", 'p', OPTPARSE_REQUIRED}, + {"help", 'h', OPTPARSE_NONE}, + {"color", 'c', OPTPARSE_REQUIRED}, + {"delay", 'd', OPTPARSE_OPTIONAL}, + {0} + }; + + int port = 3000; + + while ((option = optparse_long(&options, longopts, nullptr)) != -1) { + switch (option) { + case 'p': + port = /*options.optarg ? */atoi(options.optarg);// : port; + break; + case 'h': + + //break; + /*case 'b': + brief = true; + break; + case 'c': + color = options.optarg; + break; + case 'd': + delay = options.optarg ? atoi(options.optarg) : 1; + break;*/ + case '?': + std::cout << "Usage: " << argv[0] << " [--help] [--port ] [--ssl ] [--passphrase ] [--dh_params ] " << std::endl; + + return 0; + //fprintf(stderr, "%s: %s\n", argv[0], options.errmsg); + //exit(EXIT_FAILURE); + } + } + + /* Print remaining arguments. */ + char *arg; + while ((arg = optparse_arg(&options))) + printf("%s\n", arg); + + std::cout << "Port is " << port << std::endl; + + return 0; + if (argc < 3 || argc > 7) { std::cout << "Usage: HttpServer root port [ssl_cert ssl_key ssl_dh_params ssl_passphrase]" << std::endl; return 0; } - int port = atoi(argv[2]); + //int port = atoi(argv[2]); const char *root = argv[1]; /* Cache files of root folder */ - FileCache fileCache(root); + //FileCache fileCache(root); /* Either serve over HTTP or HTTPS */ if (argc > 5) { @@ -26,8 +79,8 @@ int main(int argc, char **argv) { .key_file_name = argv[4], /* Required */ .dh_params_file_name = argv[5], /* Required (in this example) */ .passphrase = (argc == 7 ? argv[6] : nullptr) /* Passphrase is optional */ - }).get("/*", [&fileCache](auto *res, auto *req) { - serveFile(res, req)->write(fileCache.getFile(req->getUrl())); + }).get("/*", [](auto *res, auto *req) { + //serveFile(res, req)->write(fileCache.getFile(req->getUrl())); }).listen(port, [port, root](auto *token) { if (token) { std::cout << "Serving " << root << " over HTTPS a " << port << std::endl; @@ -35,8 +88,8 @@ int main(int argc, char **argv) { }).run(); } else { /* HTTP */ - uWS::App().get("/*", [&fileCache](auto *res, auto *req) { - serveFile(res, req)->write(fileCache.getFile(req->getUrl())); + uWS::App().get("/*", [](auto *res, auto *req) { + //serveFile(res, req)->write(fileCache.getFile(req->getUrl())); }).listen(port, [port, root](auto *token) { if (token) { std::cout << "Serving " << root << " over HTTP a " << port << std::endl;