From 7bea50af59b319ad3003c9da907e4f91be83f9e1 Mon Sep 17 00:00:00 2001 From: Niklaus Giger Date: Wed, 10 Jan 2018 18:24:01 +0100 Subject: [PATCH] Converted to use Rack based SBSM --- .gitignore | 1 - Gemfile | 1 + Gemfile.lock | 135 +++++++++++++++++++++++++++++++++++ config.ru | 74 +++++++++++++++++++ lib/ydim/html/config.rb | 11 +-- lib/ydim/html/util/rack_app.rb | 40 +++++++++++ lib/ydim/html/util/rack_interface.rb | 28 ++++++++ lib/ydim/html/util/server.rb | 37 ---------- lib/ydim/html/util/session.rb | 52 +++++++++----- lib/ydim/html/view/template.rb | 2 +- ydim-html.gemspec | 4 +- 11 files changed, 322 insertions(+), 63 deletions(-) create mode 100644 Gemfile.lock create mode 100755 config.ru create mode 100755 lib/ydim/html/util/rack_app.rb create mode 100644 lib/ydim/html/util/rack_interface.rb delete mode 100644 lib/ydim/html/util/server.rb diff --git a/.gitignore b/.gitignore index ba3c73b..99043c7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,3 @@ coverage/ pkg/ .bundle/ .ruby-version -Gemfile.lock diff --git a/Gemfile b/Gemfile index ffcf4b3..3126442 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,7 @@ source 'https://rubygems.org' gemspec +gem 'ydim', :path => '/home/niklaus/git/ydim' group :debugger do if RUBY_VERSION.match(/^1/) gem 'pry-debugger' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..c77854b --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,135 @@ +PATH + remote: . + specs: + ydim-html (1.0.4) + clogger + htmlgrid (>= 1.0.8) + rclconf + sbsm (>= 1.5.9) + syck + ydim (>= 1.1.0) + +PATH + remote: /home/niklaus/git/ydim + specs: + ydim (1.1.0) + deprecated (= 2.0.1) + hpricot + mail + needle + odba (>= 1.1.2) + rclconf + rrba + syck + ydbd-pg (>= 0.5.1) + ydbi (>= 0.5.1) + ypdf-writer + +GEM + remote: https://rubygems.org/ + specs: + byebug (9.1.0) + childprocess (0.8.0) + ffi (~> 1.0, >= 1.0.11) + chrono_logger (1.1.1) + clogger (2.2.0) + rack (>= 1.0, < 3.0) + coderay (1.1.2) + color (1.8) + deprecated (2.0.1) + diff-lcs (1.3) + docile (1.1.5) + ffi (1.9.18) + flexmock (2.3.6) + hpricot (0.8.6) + htmlgrid (1.1.4) + sbsm (>= 1.2.7) + json (2.1.0) + mail (2.7.0) + mini_mime (>= 0.1.1) + method_source (0.9.0) + mimemagic (0.3.2) + mini_mime (1.0.0) + minitest (5.11.1) + needle (1.3.0) + odba (1.1.4) + ydbd-pg (>= 0.5.1) + ydbi (>= 0.5.1) + pg (0.21.0) + power_assert (1.1.1) + pry (0.11.3) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + pry-byebug (3.5.1) + byebug (~> 9.1) + pry (~> 0.10) + pry-doc (0.11.1) + pry (~> 0.9) + yard (~> 0.9) + rack (2.0.3) + rake (12.3.0) + rclconf (1.0.0) + rrba (1.0.0) + rspec (3.7.0) + rspec-core (~> 3.7.0) + rspec-expectations (~> 3.7.0) + rspec-mocks (~> 3.7.0) + rspec-core (3.7.1) + rspec-support (~> 3.7.0) + rspec-expectations (3.7.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.7.0) + rspec-mocks (3.7.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.7.0) + rspec-support (3.7.0) + rubyzip (1.2.1) + sbsm (1.5.9) + chrono_logger + hpricot + mail + mimemagic + rack + selenium-webdriver (3.8.0) + childprocess (~> 0.5) + rubyzip (~> 1.0) + simplecov (0.15.1) + docile (~> 1.1.0) + json (>= 1.8, < 3) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.2) + syck (1.3.0) + test-unit (3.2.7) + power_assert + transaction-simple (1.4.0.2) + watir (6.10.2) + selenium-webdriver (~> 3.4, >= 3.4.1) + yard (0.9.12) + ydbd-pg (0.5.4) + pg + ydbi (= 0.5.4) + ydbi (0.5.4) + deprecated (= 2.0.1) + ypdf-writer (1.3.3) + color (>= 1.4.0) + transaction-simple (~> 1.3) + +PLATFORMS + ruby + +DEPENDENCIES + bundler + flexmock + minitest + pry-byebug + pry-doc + rake + rspec + simplecov + test-unit + watir + ydim! + ydim-html! + +BUNDLED WITH + 1.16.0 diff --git a/config.ru b/config.ru new file mode 100755 index 0000000..c2dd483 --- /dev/null +++ b/config.ru @@ -0,0 +1,74 @@ +#\ -w -p 8050 +# 8050 is the port used to serve +# vim: ai ts=2 sts=2 et sw=2 ft=ruby +begin + require 'pry' +rescue LoadError +end +$stdout.sync = true + +trap("USR1") { + puts "caught USR1 signal, clearing Sessions\n" + $oddb.clear +} +trap("USR2") { + puts "caught USR2 signal, flushing stdout...\n" + $stdout.flush +} + +begin + lib_dir = File.expand_path(File.join(File.dirname(__FILE__), 'lib').untaint) + $LOAD_PATH << lib_dir + + require 'sbsm/logger' + require 'ydim/html/version' + + $0 = "Ydim-Html" + + load 'ydim/html/config.rb' + + YDIM::Html.config.log_pattern.gsub!('app', $0.to_s) + SBSM.logger= ChronoLogger.new(YDIM::Html.config.log_pattern) + # We want to redirect the standard error also to the logger + # next line found via https://stackoverflow.com/questions/9637092/redirect-stderr-to-logger-instance + $stderr.reopen SBSM.logger.instance_variable_get(:@logdev).dev + + SBSM.logger.level = Logger::WARN + + require 'ydim/html/util/rack_app' + require 'ydim/html/util/rack_interface' + + require 'rack' + require 'rack/static' + require 'rack/show_exceptions' + require 'rack' + require 'webrick' + require "clogger" + Rack_And_UserAgent = "$ip - $remote_user [$time_local{%d/%b/%Y %H:%M:%S}] " \ + '"$request" $status $response_length $request_time{4} "$http_user_agent"' + + use Clogger, + # ours is Combined + $request_time + # LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined + :format => Rack_And_UserAgent, + :logger => SBSM.logger, + :reentrant => true + use Clogger, :logger=> $stdout, :reentrant => true + use(Rack::Static, urls: ["/doc/"]) + use Rack::ContentLength + SBSM.warn "Starting Rack::Server with log_pattern #{YDIM::Html.config.log_pattern}" + + $stdout.sync = true + VERSION = `git rev-parse HEAD` + process ||= :ydim_html + SBSM.logger.info("process #{process} sbsm #{SBSM::VERSION} and oddb.org #{VERSION}") + my_app = YDIM::Html::Util::RackInterface.new(app: YDIM::Html::Util::App.new) +rescue Exception => e + puts "Error loding required libraries #{e}" + puts e.backtrace[0..9].join("\n") + binding.pry + exit(1) +end + +app = Rack::ShowExceptions.new(Rack::Lint.new(my_app)) +run app diff --git a/lib/ydim/html/config.rb b/lib/ydim/html/config.rb index 58f8fd7..809cc01 100644 --- a/lib/ydim/html/config.rb +++ b/lib/ydim/html/config.rb @@ -16,20 +16,21 @@ default_config_files = [ '/etc/ydim/ydim-htmld.yml', ] defaults = { - 'client_url' => 'druby://localhost:0', + 'client_url' => 'druby://127.0.0.1:0', 'config' => default_config_files, 'currency' => 'CHF', 'email' => nil, - 'html_url' => 'druby://localhost:12376', - 'http_server' => 'http://localhost', + 'html_url' => 'druby://127.0.0.1:12376', + 'http_server' => 'http://127.0.0.1', 'log_file' => $stdout, 'log_level' => 'DEBUG', 'md5_pass' => nil, - 'proxy_url' => 'druby://localhost:0', - 'server_url' => 'druby://localhost:12375', + 'proxy_url' => 'druby://127.0.0.1:0', + 'server_url' => 'druby://127.0.0.1:12375', 'root_key' => nil, 'user' => nil, 'ydim_dir' => ydim_default_dir, + 'log_pattern' => File.join(Dir.pwd, defined?(MiniTest) ? 'test/log' : 'log','/%Y/%m/%d/app_log'), } @config = RCLConf::RCLConf.new(ARGV, defaults) @config.load(@config.config) diff --git a/lib/ydim/html/util/rack_app.rb b/lib/ydim/html/util/rack_app.rb new file mode 100755 index 0000000..0b84f5d --- /dev/null +++ b/lib/ydim/html/util/rack_app.rb @@ -0,0 +1,40 @@ +#!/usr/bin/env ruby +require 'syck' +require 'openssl' +require 'sbsm/app' +require 'ydim/client' +require 'ydim/html' +require 'ydim/html/util/lookandfeel' +require 'ydim/html/util/validator' +require 'ydim/html/util/session' +require 'ydim/odba' +require 'ydim/invoice' +require 'ydim/debitor' + +require 'sbsm/session' +require 'sbsm/app' + +module YDIM + module Html + module Util + class App < SBSM::App + attr_reader :ydim_server, :root_session, :private_key + def initialize + SBSM.logger= ChronoLogger.new(YDIM::Html.config.log_pattern) + SBSM.logger.level = :debug + msg = "YDIM::Html::Util::App used version: sbsm #{SBSM::VERSION}, ydim #{YDIM::VERSION}" + puts msg + SBSM.logger.info(msg) + SBSM.logger.info "Starting Rack-Service #{self.class} and service #{YDIM::Html.config.server_url}" + @system = YDIM::Client.new(Html.config) + @private_key = OpenSSL::PKey::DSA.new(File.read(Html.config.root_key)) + @ydim_server = DRb::DRbObject.new(nil, YDIM::Html.config.server_url) + super() + end + def login(email, pass_hash) + (email == Html.config.email) && (pass_hash == Html.config.md5_pass) + end + end + end + end + end diff --git a/lib/ydim/html/util/rack_interface.rb b/lib/ydim/html/util/rack_interface.rb new file mode 100644 index 0000000..6bfe0a2 --- /dev/null +++ b/lib/ydim/html/util/rack_interface.rb @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby + +require 'ydim/html/config' +require 'ydim/root_session' +require 'sbsm/app' + +module YDIM + module Html + module Util + class RackInterface < SBSM::RackInterface + ENABLE_ADMIN = true + SESSION = Html::Util::Session + VALIDATOR = Html::Util::Validator + def initialize(app:, + auth: nil, + validator: VALIDATOR) + @app = app + super(app: app, + session_class: SESSION, + # unknown_user: YDIM::UnknownUser.new, + validator: validator, + cookie_name: 'oddb.org' + ) + end + end + end + end +end diff --git a/lib/ydim/html/util/server.rb b/lib/ydim/html/util/server.rb deleted file mode 100644 index 810d0b7..0000000 --- a/lib/ydim/html/util/server.rb +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env ruby -# encoding: utf-8 -# Html::Util::Server -- ydim -- 12.01.2006 -- hwyss@ywesee.com - -require 'openssl' -require 'sbsm/drbserver' -require 'ydim/html/util/session' -require 'ydim/html/util/validator' -require 'ydim/client' - -module YDIM - module Html - module Util -class Server < SBSM::DRbServer - SESSION = Html::Util::Session - VALIDATOR = Html::Util::Validator - def initialize(server) - @server = server - @private_key = OpenSSL::PKey::DSA.new(File.read(Html.config.root_key)) - @system = YDIM::Client.new(Html.config) - super(@system) - end - def login(email, pass_hash) - (email == Html.config.email) && (pass_hash == Html.config.md5_pass) - end - def method_missing(meth, *args) - @system.login(@server, @private_key) - begin - @system.send(meth, *args) - ensure - @system.logout - end - end -end - end - end -end diff --git a/lib/ydim/html/util/session.rb b/lib/ydim/html/util/session.rb index edf3804..41793de 100644 --- a/lib/ydim/html/util/session.rb +++ b/lib/ydim/html/util/session.rb @@ -7,23 +7,39 @@ require 'ydim/html/state/global' require 'ydim/html/util/lookandfeel' module YDIM - module Html - module Util -class Session < SBSM::Session - DEFAULT_FLAVOR = "ydim" - DEFAULT_LANGUAGE = 'de' - DEFAULT_STATE = Html::State::Init - LOOKANDFEEL = Html::Custom::Lookandfeel - def login - @app.login(user_input(:email), user_input(:pass)) - end - def invoices - @app.invoice_infos(user_input(:status) || 'is_open') + module Html + module Util + class Session < SBSM::Session + DEFAULT_FLAVOR = "ydim" + DEFAULT_LANGUAGE = 'de' + DEFAULT_STATE = Html::State::Init + LOOKANDFEEL = Html::Custom::Lookandfeel + def initialize(app:, + trans_handler: nil, + validator: nil, + unknown_user: nil, + cookie_name: nil, + multi_threaded: true) + super + @app = app + end + def login + @app.login(user_input(:email), user_input(:pass)) + end + def invoices + @app.root_session.invoice_infos(user_input(:status) || 'is_open') + end + def method_missing(meth, *args) + server = DRbObject.new(nil, YDIM::Html.config.server_url) + client = YDIM::Client.new(YDIM::Html.config) + client.login(server, @app.private_key) + begin + client.send(meth, *args) + ensure + client.logout + end + end + end + end end - def method_missing(meth, *args) - @app.send(meth, *args) - end -end - end - end end diff --git a/lib/ydim/html/view/template.rb b/lib/ydim/html/view/template.rb index bc71bb0..a772eb7 100644 --- a/lib/ydim/html/view/template.rb +++ b/lib/ydim/html/view/template.rb @@ -62,7 +62,7 @@ class Template < HtmlGrid::DivTemplate def ydim_version(model) link = standard_link(:ydim_version, model) link.href = 'https://github.com/zdavatz/ydim-html' - link.set_attribute('title', YDIM_VERSION) + link.set_attribute('title', YDIM::Html::VERSION) link end end diff --git a/ydim-html.gemspec b/ydim-html.gemspec index 7f55a7a..63225b1 100644 --- a/ydim-html.gemspec +++ b/ydim-html.gemspec @@ -17,11 +17,13 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] - spec.add_dependency "sbsm", '= 1.2.7' +# spec.add_dependency "currency" + spec.add_dependency "sbsm", '>= 1.5.9' spec.add_dependency "htmlgrid",'>= 1.0.8' spec.add_dependency "ydim", '>= 1.1.0' spec.add_dependency "rclconf" spec.add_dependency "syck" + spec.add_dependency "clogger" # spec.add_runtime_dependency 'deprecated', '= 2.0.1' spec.add_development_dependency "bundler" -- 2.10.2