Class: BitClust::App
Overview
Main class of BitClust server application. Actual actions are implemented by RequestHandler.
Supports Rack and WEBrick.
Instance Attribute Summary collapse
-
#interfaces ⇒ Object
readonly
Returns the value of attribute interfaces.
-
#versions ⇒ Object
readonly
Returns the value of attribute versions.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #get_instance(_server, *_) ⇒ Object
- #index(req) ⇒ Object
-
#initialize(options) ⇒ App
constructor
A new instance of App.
- #service(req, res) ⇒ Object
Constructor Details
#initialize(options) ⇒ App
Returns a new instance of App.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bitclust/app.rb', line 14 def initialize() @options = dbpath = [:dbpath] baseurl = [:baseurl] || '' datadir = [:datadir] || File.('../../data/bitclust', File.dirname(__FILE__)) encoding = [:encoding] || 'utf-8' viewpath = [:viewpath] capi = [:capi] if [:rack] request_handler_class = BitClust::RackRequestHandler else request_handler_class = BitClust::RequestHandler end @interfaces = {} case dbpath when String # @type var viewpath: String dbpath = File.(dbpath) manager = BitClust::ScreenManager.new( :base_url => baseurl, :cgi_url => File.join(baseurl, viewpath), :datadir => datadir, :templatedir => [:templatedir], :theme => [:theme], :encoding => encoding ) handler = BitClust::ReloadableRequestHandler.new(dbpath, capi, manager, request_handler_class) @interfaces[viewpath] = BitClust::Interface.new { handler } when Array # @type var viewpath: String? dbpaths = dbpath @versions = [] dbpaths.each do |dbpath| next unless /db-([\d_\.]+)/ =~ dbpath dbpath = File.(dbpath) version = ($1 || raise).tr("_", ".") @versions << version if viewpath version_viewpath = File.join(version, viewpath) else version_viewpath = version end manager = BitClust::ScreenManager.new( :base_url => baseurl, :cgi_url => File.join(baseurl, version_viewpath), :datadir => datadir, :templatedir => [:templatedir], :theme => [:theme], :encoding => encoding ) handler = BitClust::ReloadableRequestHandler.new(dbpath, capi, manager, request_handler_class) @interfaces[version_viewpath] = BitClust::Interface.new { handler } $bitclust_context_cache = nil # clear cache end end end |
Instance Attribute Details
#interfaces ⇒ Object (readonly)
Returns the value of attribute interfaces.
71 72 73 |
# File 'lib/bitclust/app.rb', line 71 def interfaces @interfaces end |
#versions ⇒ Object (readonly)
Returns the value of attribute versions.
71 72 73 |
# File 'lib/bitclust/app.rb', line 71 def versions @versions end |
Instance Method Details
#call(env) ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/bitclust/app.rb', line 125 def call(env) [ 200, {'Content-Type' => 'text/html; charset=utf-8'}, [index(Rack::Request.new(env))] ] end |
#get_instance(_server, *_) ⇒ Object
113 114 115 |
# File 'lib/bitclust/app.rb', line 113 def get_instance(_server, *_) self end |
#index(req) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/bitclust/app.rb', line 73 def index(req) case when @interfaces.size == 1 && viewpath = @options[:viewpath] # Redirect from '/' to "#{viewpath}/" @index = "<html><head><meta http-equiv='Refresh' content='0;URL=#{viewpath}'></head></html>" when 1 < @interfaces.size request_path = case when req.respond_to?(:path_info) req.path_info when req.respond_to?(:path) req.path_info end if @versions.any?{|version| %r|\A/?#{version}/?\z| =~ request_path } viewpath = @options[:viewpath] raise unless viewpath.is_a?(String) viewpath = File.join(request_path || raise, viewpath) @index = "<html><head><meta http-equiv='Refresh' content='0;URL=#{viewpath}'></head></html>" else links = "<ul>" @interfaces.keys.sort.each do |v| if (viewpath = @options[:viewpath]).is_a?(String) version = v.sub(viewpath, '') else version = v end url = v links << %Q(<li><a href="#{url}/">#{version}</a></li>) end links << "</ul>" if File.exist?("readme.html") @index = File.read("readme.html").sub(%r!\./bitclust!, '').sub(/<!--links-->/) { links } else @index = "<html><head><title>bitclust</title></head><body>#{links}</body></html>" end end else raise '[BUG]' end end |
#service(req, res) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/bitclust/app.rb', line 117 def service(req, res) unless %r|/#{File.basename(@options[:baseurl] || raise)}/?\z| =~ req.path raise WEBrick::HTTPStatus::NotFound end res.body = index(req) res['Content-Type'] = 'text/html; charset=utf-8' end |