Class: BitClust::App
Overview
Main class of BitClust server application. Actual actions are implemneted 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.
13 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 70 71 72 73 74 |
# File 'lib/bitclust/app.rb', line 13 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 dbpath = File.(dbpath) db = BitClust::MethodDatabase.new(dbpath) if capi db = [db, BitClust::FunctionDatabase.new(dbpath)] end manager = BitClust::ScreenManager.new( :base_url => baseurl, :cgi_url => File.join(baseurl, viewpath), :datadir => datadir, :templatedir => [:templatedir], :theme => [:theme], :encoding => encoding ) handler = request_handler_class.new(db, manager) @interfaces[viewpath] = BitClust::Interface.new { handler } when Array dbpaths = dbpath @versions = [] dbpaths.each do |dbpath| next unless /db-([\d_\.]+)/ =~ dbpath dbpath = File.(dbpath) version = $1.tr("_", ".") @versions << version if viewpath version_viewpath = File.join(version, viewpath) else version_viewpath = version end db = BitClust::MethodDatabase.new(dbpath) if capi db = [db, BitClust::FunctionDatabase.new(dbpath)] end manager = BitClust::ScreenManager.new( :base_url => baseurl, :cgi_url => File.join(baseurl, version_viewpath), :datadir => datadir, :templatedir => [:templatedir], :theme => [:theme], :encoding => encoding ) handler = request_handler_class.new(db, manager) @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.
76 77 78 |
# File 'lib/bitclust/app.rb', line 76 def interfaces @interfaces end |
#versions ⇒ Object (readonly)
Returns the value of attribute versions.
76 77 78 |
# File 'lib/bitclust/app.rb', line 76 def versions @versions end |
Instance Method Details
#call(env) ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/bitclust/app.rb', line 126 def call(env) [ 200, {'Content-Type' => 'text/html; charset=utf-8'}, [index(Rack::Request.new(env))] ] end |
#get_instance(server) ⇒ Object
114 115 116 |
# File 'lib/bitclust/app.rb', line 114 def get_instance(server) self end |
#index(req) ⇒ Object
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 112 |
# File 'lib/bitclust/app.rb', line 78 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 = File.join(request_path, @options[:viewpath]) @index = "<html><head><meta http-equiv='Refresh' content='0;URL=#{viewpath}'></head></html>" else links = "<ul>" @interfaces.keys.sort.each do |v| if @options[:viewpath] version = v.sub(@options[: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 end end |
#service(req, res) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/bitclust/app.rb', line 118 def service(req, res) unless %r|/#{File.basename(@options[:baseurl])}/?\z| =~ req.path raise WEBrick::HTTPStatus::NotFound end res.body = index(req) res['Content-Type'] = 'text/html; charset=utf-8' end |