From 426fcf43c4eeb84628de759bfbb6469a73471d95 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Thu, 28 Apr 2022 20:39:39 -0400 Subject: [PATCH] Add option parsing --- src/main.cpp | 25 ++++++++++++++++--------- src/opt/parseopt.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/opt/parseopt.hpp | 22 ++++++++++++++++++++++ src/routes.cpp | 17 +++++++++++++++++ src/routes.hpp | 11 +++++++++++ 5 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 src/opt/parseopt.cpp create mode 100644 src/opt/parseopt.hpp create mode 100644 src/routes.hpp diff --git a/src/main.cpp b/src/main.cpp index 0c144f6..5172db6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,18 +6,25 @@ #include #include +#include "opt/parseopt.hpp" + +#include "routes.hpp" int main(int argc, char** argv){ + + option_flags* flags = parse_options(argc, argv); + crow::SimpleApp app; - CROW_ROUTE(app, "/")([](){ - return "Hello World"; - }); - CROW_ROUTE(app, "/json")([]{ - crow::json::wvalue response({{ "message", "Hello world" }}); - response["kek"] = "nay, cringe"; - return response; - }); + setRoutes(app); - app.port(5000).run(); + std::cerr << "Setting up app" << std::endl; + + app.port(flags->port) + .server_name("proc_api") + .multithreaded(); + + delete flags; + + app.run(); } diff --git a/src/opt/parseopt.cpp b/src/opt/parseopt.cpp new file mode 100644 index 0000000..478d4dd --- /dev/null +++ b/src/opt/parseopt.cpp @@ -0,0 +1,41 @@ +/////////////////////////////////////////////////////////////////////////////// +// Tyler Perkins +// 28-4-22 +// parseopt implementation +// + +#include "parseopt.hpp" + +void help(char* progName){ + std::cout << "Usage: " << progName << " [FLAGS]\n"; + std::cout << "Options:\n"; + std::cout << " [-p] Port to listen on\n"; + std::cout << " [-h] Display this help message\n\n"; + exit(1); +} + +option_flags* parse_options(int argc, char** argv){ + char c; + + option_flags* ret = new option_flags; + + ret->port = 5000; + + while((c = getopt(argc, argv, optarg_string)) != -1){ + switch(c){ + case 'p': + std::cout << optarg << "\n"; + ret->port = atoi(optarg); + break; + case '?': + std::cerr << "Unkown option: " << (char)optopt << "\n"; + case 'h': + help(argv[0]); + break; + default: + abort(); + } + } + + return ret; +} diff --git a/src/opt/parseopt.hpp b/src/opt/parseopt.hpp new file mode 100644 index 0000000..d833e48 --- /dev/null +++ b/src/opt/parseopt.hpp @@ -0,0 +1,22 @@ +/////////////////////////////////////////////////////////////////////////////// +// Tyler Perkins +// 28-4-22 +// parseopt definitions +// + +#pragma once + +#include +#include + +/////////////////////////////////////// +// cli options + +constexpr char optarg_string[] = "p:h"; + +struct option_flags { + uint16_t port; +}; + +void help(char*); +option_flags* parse_options(int, char**); diff --git a/src/routes.cpp b/src/routes.cpp index e69de29..74dbbc0 100644 --- a/src/routes.cpp +++ b/src/routes.cpp @@ -0,0 +1,17 @@ +/////////////////////////////////////////////////////////////////////////////// +// Tyler Perkins +// 28-4-22 +// Routes implementation +// + +#include "routes.hpp" + +void setRoutes(crow::SimpleApp& app){ + CROW_ROUTE(app, "/proc/meminfo")([]{ + return "Meminfo"; + }); + + CROW_ROUTE(app, "/mem")([]{ + return "mem"; + }); +} diff --git a/src/routes.hpp b/src/routes.hpp new file mode 100644 index 0000000..453f796 --- /dev/null +++ b/src/routes.hpp @@ -0,0 +1,11 @@ +/////////////////////////////////////////////////////////////////////////////// +// Tyler Perkins +// 28-4-22 +// Routes definition +// + +#pragma once + +#include + +void setRoutes(crow::SimpleApp&);