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 +++++++++++++ build.sh | 7 +++ etc/apachectl | 39 +++++++--------- etc/httpd.conf | 34 +++++++++++--- etc/search.conf | 9 ++++ etc/view.conf | 5 +++ services.cxx | 8 +++- web/apache/log | 12 ++--- web/apache/service | 127 ++++++++++++++++++++++++++++++++++------------------- web/module | 6 ++- 16 files changed, 350 insertions(+), 101 deletions(-) create mode 100644 brep/options.cli create mode 100755 build.sh create mode 100644 etc/search.conf create mode 100644 etc/view.conf 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" + << ""; } } diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..2a39f48 --- /dev/null +++ b/build.sh @@ -0,0 +1,7 @@ +DEBUG="-g -ggdb -fno-inline" + +cd ./brep; cli --generate-file-scanner --suppress-usage --hxx-suffix "" \ + --option-prefix "" ./options.cli; cd .. + +g++ -shared $DEBUG -std=c++11 -I. -I/usr/include/apr-1 \ + -fPIC -o libbrep.so `find . -name '*.cxx'` diff --git a/etc/apachectl b/etc/apachectl index a879354..1039c50 100755 --- a/etc/apachectl +++ b/etc/apachectl @@ -27,23 +27,28 @@ ARGV="$@" # |||||||||||||||||||| START CONFIGURATION SECTION |||||||||||||||||||| # -------------------- -------------------- -PORT=7180 -LOG_LEVEL=trace1 -ADMIN_EMAIL=admin@cppget.org +export PORT=7180 +export SERVER_NAME="cppget.org:$PORT" +export LOG_LEVEL=trace1 +export ADMIN_EMAIL=admin@cppget.org + +export DB_HOST=localhost +export DB_PORT=7136 # |||||||||||||||||||| END CONFIGURATION SECTION ||||||||||||||||||||||| PROJECT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/.. -site_config="$PROJECT_DIR/etc" -workspace="$PROJECT_DIR/var" -www="$PROJECT_DIR/www" +export MODULE_DIR="$PROJECT_DIR" +export WWW_DIR="$PROJECT_DIR/www" +export CONFIG_DIR="$PROJECT_DIR/etc" +export WORKSPACE_DIR="$PROJECT_DIR/var" -mkdir -p "$workspace" +mkdir -p "$WORKSPACE_DIR" # the path to your httpd binary, including options if necessary -HTTPD="/usr/sbin/httpd -d $workspace -f $site_config/httpd.conf" +HTTPD="/usr/sbin/httpd -d $WORKSPACE_DIR -f $CONFIG_DIR/httpd.conf" # a command that outputs a formatted text version of the HTML at the # url given on the command line. Designed for lynx, however other @@ -73,25 +78,11 @@ if [ "x$ARGV" = "x" ] ; then fi case $ARGV in -start) - $HTTPD -C "Listen $PORT" -C "ServerName cppget.org:$PORT" \ - -C "DocumentRoot $www/htdocs" -C "CoreDumpDirectory $workspace" \ - -C "PidFile $workspace/httpd.pid" \ - -C "LogLevel $LOG_LEVEL" \ - -C "ServerAdmin $ADMIN_EMAIL" \ - -k $ARGV \ - \ - -C "LoadModule search_srv $PROJECT_DIR/libbrep.so" \ - -C "search_conf $site_config/search.conf" \ - \ - -C "LoadModule view_srv $PROJECT_DIR/libbrep.so" \ - -C "view_conf $site_config/view.conf" - +start) $HTTPD -k $ARGV ERROR=$? ;; stop|restart|graceful) - $HTTPD -C "ServerName cppget.org:$PORT" \ - -C "PidFile $workspace/httpd.pid" -k $ARGV + $HTTPD -k $ARGV ERROR=$? ;; startssl|sslstart|start-SSL) diff --git a/etc/httpd.conf b/etc/httpd.conf index 95b887d..b52b7d0 100644 --- a/etc/httpd.conf +++ b/etc/httpd.conf @@ -1,13 +1,20 @@ +Listen ${PORT} +ServerName "${SERVER_NAME}" +ServerAdmin "${ADMIN_EMAIL}" + User apache Group apache -ErrorLog error_log -#ErrorLog "|/usr/sbin/rotatelogs /Users/karen/projects/brep/var/error_log.%Y%m%d 86400" +DocumentRoot "${WWW_DIR}/htdocs" +CoreDumpDirectory "${WORKSPACE_DIR}" +PidFile "${WORKSPACE_DIR}/httpd.pid" +#ErrorLog error_log +ErrorLog "|/usr/sbin/rotatelogs error_log.%Y%m%d 86400" ErrorLogFormat "[%t] [%l] [%m] %M" +LogLevel ${LOG_LEVEL} Timeout 60 - KeepAlive On KeepAliveTimeout 3 @@ -33,7 +40,21 @@ LoadModule authz_host_module /usr/lib64/httpd/modules/mod_authz_host.so LoadModule expires_module /usr/lib64/httpd/modules/mod_expires.so LoadModule dir_module /usr/lib64/httpd/modules/mod_dir.so -TypesConfig /etc/mime.types +LoadModule search_srv ${MODULE_DIR}/libbrep.so + + + search-db-host ${DB_HOST} + search-db-port ${DB_PORT} + search-conf "${CONFIG_DIR}/search.conf" + + +LoadModule view_srv ${MODULE_DIR}/libbrep.so + + + view-db-host ${DB_HOST} + view-db-port ${DB_PORT} + view-conf "${CONFIG_DIR}/view.conf" + SetHandler search @@ -43,8 +64,6 @@ TypesConfig /etc/mime.types SetHandler view -DirectoryIndex index.html - ExtendedStatus On @@ -59,3 +78,6 @@ ExtendedStatus On Options FollowSymLinks AllowOverride None + +DirectoryIndex index.html +TypesConfig /etc/mime.types diff --git a/etc/search.conf b/etc/search.conf new file mode 100644 index 0000000..f742cda --- /dev/null +++ b/etc/search.conf @@ -0,0 +1,9 @@ +# brep::module options + +verb 1 + +# brep::search options + +results-on-page 20 + +#ah 7 diff --git a/etc/view.conf b/etc/view.conf new file mode 100644 index 0000000..7c9b163 --- /dev/null +++ b/etc/view.conf @@ -0,0 +1,5 @@ +# brep::module options + +verb 2 + +#oh 8 diff --git a/services.cxx b/services.cxx index 37b32cf..3d6f372 100644 --- a/services.cxx +++ b/services.cxx @@ -11,7 +11,11 @@ using namespace brep; using web::apache::service; static search search_mod; -service AP_MODULE_DECLARE_DATA search_srv ("search", search_mod); +service AP_MODULE_DECLARE_DATA search_srv ("search", + search_mod, + {"db-host", "db-port", "conf"}); static view view_mod; -service AP_MODULE_DECLARE_DATA view_srv ("view", view_mod); +service AP_MODULE_DECLARE_DATA view_srv ("view", + view_mod, + {"db-host", "db-port", "conf"}); diff --git a/web/apache/log b/web/apache/log index f8c65e9..50e2f7c 100644 --- a/web/apache/log +++ b/web/apache/log @@ -21,7 +21,7 @@ namespace web { public: - log (request_rec* req) noexcept: req_ (req) {} + log (server_rec* server) noexcept: server_ (server) {} virtual void write (const char* msg) {write (APLOG_ERR, msg);} @@ -47,30 +47,30 @@ namespace web level = std::min (level, APLOG_TRACE8); if (func) - ap_log_rerror (file, + ap_log_error (file, line, APLOG_NO_MODULE, level, 0, - req_, + server_, "[%s]: %s", func, msg); else // skip function name placeholder from log line // - ap_log_rerror (file, + ap_log_error (file, line, APLOG_NO_MODULE, level, 0, - req_, + server_, ": %s", msg); } private: - request_rec* req_; + server_rec* server_; }; } } diff --git a/web/apache/service b/web/apache/service index d003767..cd405ee 100644 --- a/web/apache/service +++ b/web/apache/service @@ -13,8 +13,11 @@ #include #include +#include +#include // unique_ptr #include #include +#include // move() #include #include @@ -27,10 +30,15 @@ namespace web class service: ::module { public: + + using option_names = std::vector; + // Note that the module exemplar is stored by-reference. // template - service (const std::string& name, M& exemplar) + service (const std::string& name, + M& exemplar, + option_names opts = option_names ()) : ::module { STANDARD20_MODULE_STUFF, @@ -38,36 +46,44 @@ namespace web nullptr, nullptr, nullptr, - directives_, + nullptr, ®ister_hooks }, name_ (name), exemplar_ (exemplar), - conf_ (name + "_conf"), - conf_err_ ("A file containing configuration options for module " + - name_), - - // Defines service configuration directives. At the moment the - // only configuration directive is - // "_conf ". Configuration file path - // specified will be passed as a parameter to - // web::module::init call on exemplar_ object when apache worker - // process is started but prior to accepting client requests. - // - directives_ - { - { - conf_.c_str (), - reinterpret_cast (config_file), - this, - RSRC_CONF, - TAKE1, - conf_err_.c_str () - } - } + option_names_ (std::move (opts)) // Doesn't look like handle_ member is required at all. // handle_ (&handle_impl) { + // Fill apache module directive definitions. Directives share + // common name space in apache configuration file, so to prevent name + // clash have to form directive name as a combination of module and + // option names: -