From 07780b06aa7b0fe049cc412309cf87e7fb10a0ef Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 30 Apr 2015 14:25:29 +0200 Subject: Implement module configuration with an option list --- brep/module | 24 +++++++++++++++----- brep/module.cxx | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++----- brep/options.cli | 24 ++++++++++++++++++++ brep/search | 10 +++++++++ brep/search.cxx | 37 +++++++++++++++++++++++++----- brep/view | 10 +++++++++ brep/view.cxx | 31 ++++++++++++++++++++++++++ 7 files changed, 187 insertions(+), 17 deletions(-) create mode 100644 brep/options.cli (limited to 'brep') diff --git a/brep/module b/brep/module index 54d41cf..d1399fb 100644 --- a/brep/module +++ b/brep/module @@ -11,6 +11,7 @@ #include +#include #include namespace brep @@ -69,10 +70,6 @@ namespace brep // class module: public web::module { - public: - virtual void - handle (request&, response&) = 0; - // Diagnostics. // protected: @@ -95,18 +92,33 @@ namespace brep module (); module (const module& ); + private: virtual void handle (request&, response&, log&); virtual void - init (const char* path); + handle (request&, response&) = 0; + + virtual void + init (const name_values&, log&); + + // Can be overriden by module implementation which has configuration + // options. + // + virtual void + init (::cli::scanner& s) + { + // Just scan options to ensure there is no misspelled ones. + // + module_options o (s, cli::unknown_mode::fail, cli::unknown_mode::fail); + } // Diagnostics implementation details. // private: log* log_ {nullptr}; // Diagnostics backend provided by the web server. - public: + private: // Extract function name from a __PRETTY_FUNCTION__. // Throw std::invalid_argument if fail to parse. // diff --git a/brep/module.cxx b/brep/module.cxx index a803386..44154d6 100644 --- a/brep/module.cxx +++ b/brep/module.cxx @@ -7,6 +7,7 @@ #include #include +#include // unique_ptr #include #include // strncmp() #include @@ -15,6 +16,8 @@ #include #include +#include + using namespace std; using namespace placeholders; // For std::bind's _1, etc. @@ -52,10 +55,8 @@ namespace brep name = d.name; } - o << name << ": " << sev_str[d.sev] << ": " << d.msg << endl; - - //o << "[" << s[static_cast (d.sev)] << "] [" - // << name << "] " << d.msg << std::endl; + o << name << ": " << sev_str[static_cast (d.sev)] << ": " + << d.msg << endl; } } catch (const sequence_error&) @@ -67,9 +68,64 @@ namespace brep } } + // Parse options with a cli-generated scanner. Options verb and conf are + // recognized by brep::module::init while others to be interpreted by the + // derived class init method. If there is an option which can not be + // interpreted not by brep::module::init nor by derived class init method + // then web server is terminated with a corresponding error message being + // logged. + // void module:: - init (const char* path) + init (const name_values& options, log& log) { + log_ = &log; + + int argc = 0; + std::unique_ptr argv (new const char*[options.size () * 2]); + + for (const auto& nv: options) + { + argv[argc++] = nv.name.c_str (); + argv[argc++] = nv.value.c_str (); + } + + try + { + { + // Read module implementation configuration. + // + cli::argv_file_scanner s (0, + argc, + const_cast (argv.get ()), + "conf"); + + init (s); + } + + // Read brep::module configuration. + // + cli::argv_file_scanner s (0, + argc, + const_cast (argv.get ()), + "conf"); + + module_options o (s, + ::cli::unknown_mode::skip, + ::cli::unknown_mode::skip); + + verb_ = o.verb (); + } + catch (const server_error& e) + { + log_write (e.data); + throw runtime_error ("initialization failed"); + } + catch (const cli::exception& e) + { + std::ostringstream o; + e.print (o); + throw runtime_error (o.str ()); + } } module:: @@ -166,7 +222,7 @@ namespace brep al->write (e.loc.file.c_str (), e.loc.line, name.c_str (), - s[static_cast (e.sev)], + s[static_cast (e.sev)], e.msg.c_str ()); } } diff --git a/brep/options.cli b/brep/options.cli new file mode 100644 index 0000000..cbd5d3a --- /dev/null +++ b/brep/options.cli @@ -0,0 +1,24 @@ +include ; + +namespace brep +{ + class module_options + { + unsigned int verb = 0; + }; + + class db_options + { + std::string db-host = "localhost"; + unsigned short db-port = 3306; + }; + + class search_options: module_options, db_options + { + unsigned int results-on-page = 10; + }; + + class view_options: module_options, db_options + { + }; +} diff --git a/brep/search b/brep/search index 9ea9345..a4edf01 100644 --- a/brep/search +++ b/brep/search @@ -5,7 +5,10 @@ #ifndef BREP_SEARCH #define BREP_SEARCH +#include // shared_ptr + #include +#include namespace brep { @@ -14,6 +17,13 @@ namespace brep public: virtual void handle (request&, response&); + + virtual void + init (::cli::scanner&); + + private: + + std::shared_ptr options_; }; } diff --git a/brep/search.cxx b/brep/search.cxx index 9e6ab4b..15811e3 100644 --- a/brep/search.cxx +++ b/brep/search.cxx @@ -4,6 +4,7 @@ #include +#include // shared_ptr, make_shared() #include #include @@ -14,6 +15,23 @@ using namespace std; namespace brep { void search:: + init (::cli::scanner& s) + { + MODULE_DIAG; + + options_ = std::make_shared (s, + ::cli::unknown_mode::fail, + ::cli::unknown_mode::fail); + + if (options_->results_on_page () > 30) + fail << "too many search results on page: " + << options_->results_on_page (); + else if (options_->results_on_page () > 10) + warn << options_->results_on_page () + << " search results on page is quite a lot but will try to cope"; + } + + void search:: handle (request& rq, response& rs) { MODULE_DIAG; @@ -24,9 +42,17 @@ namespace brep info << "handling search request from "; // << rq.client_ip (); - ostream& o (rs.content (200, "text/html;charset=utf-8", true)); + ostream& o (rs.content ()); + + o << ""; + + o << "Options:" + << "
\ntracing verbosity: " << options_->verb () + << "
\ndb endpoint: " << options_->db_host () << ":" + << options_->db_port () + << "
\nsearch results on page: " << options_->results_on_page (); - o << "Params:"; + o << "

\nParams:"; const name_values& ps (rq.parameters ()); @@ -44,13 +70,14 @@ namespace brep o << "
\n" << p.name << "=" << p.value; } - o << "
\nCookies:"; + o << "

\nCookies:"; for (const auto& c: rq.cookies ()) { - o << "
\n" << c.name << "=" << c.value << " "; + o << "
\n" << c.name << "=" << c.value; } - o << ""; + o << "

View" + << ""; } } diff --git a/brep/view b/brep/view index 819eff3..d97f9cf 100644 --- a/brep/view +++ b/brep/view @@ -5,7 +5,10 @@ #ifndef BREP_VIEW #define BREP_VIEW +#include // shared_ptr + #include +#include namespace brep { @@ -14,6 +17,13 @@ namespace brep public: virtual void handle (request&, response&); + + virtual void + init (::cli::scanner&); + + private: + + std::shared_ptr options_; }; } diff --git a/brep/view.cxx b/brep/view.cxx index 6dafa1b..7ccc159 100644 --- a/brep/view.cxx +++ b/brep/view.cxx @@ -4,12 +4,43 @@ #include +#include // shared_ptr, make_shared() +#include + +#include + using namespace std; namespace brep { void view:: + init (::cli::scanner& s) + { + options_ = std::make_shared (s, + ::cli::unknown_mode::fail, + ::cli::unknown_mode::fail); + } + + void view:: handle (request& rq, response& rs) { + ostream& o (rs.content (200, "text/html;charset=utf-8", false)); + + o << ""; + + o << "Options:" + << "
\ntracing verbosity: " << options_->verb () + << "
\ndb endpoint: " << options_->db_host () << ":" + << options_->db_port (); + + o << "

\nCookies:"; + + for (const auto& c: rq.cookies ()) + { + o << "
\n" << c.name << "=" << c.value; + } + + o << "

Search" + << ""; } } -- cgit v1.1