Class: BitClust::App

Inherits:
Object show all
Defined in:
lib/bitclust/app.rb

Overview

Main class of BitClust server application. Actual actions are implemented by RequestHandler.

Supports Rack and WEBrick.

Instance Attribute Summary collapse

Instance Method Summary collapse

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)
  @options = options
  dbpath = options[:dbpath]
  baseurl = options[:baseurl] || ''
  datadir = options[:datadir] || File.expand_path('../../data/bitclust', File.dirname(__FILE__))
  encoding = options[:encoding] || 'utf-8'
  viewpath = options[:viewpath]
  capi = options[:capi]
  if options[:rack]
    request_handler_class = BitClust::RackRequestHandler
  else
    request_handler_class = BitClust::RequestHandler
  end
  @interfaces = {}
  case dbpath
  when String
    # @type var viewpath: String
    dbpath = File.expand_path(dbpath)
    manager = BitClust::ScreenManager.new(
      :base_url => baseurl,
      :cgi_url => File.join(baseurl, viewpath),
      :datadir => datadir,
      :templatedir => options[:templatedir],
      :theme => options[: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.expand_path(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 => options[:templatedir],
        :theme => options[: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

#interfacesObject (readonly)

Returns the value of attribute interfaces.



71
72
73
# File 'lib/bitclust/app.rb', line 71

def interfaces
  @interfaces
end

#versionsObject (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