Class: Gitjour::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/gitjour/browser.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Browser

Returns a new instance of Browser.



9
10
11
12
13
14
# File 'lib/gitjour/browser.rb', line 9

def initialize(*args)
  @port = args.shift || 9850
  @browser = args.shift
  @services = Set.new
  @mutex = Mutex.new
end

Instance Method Details

#cssObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gitjour/browser.rb', line 89

def css
  @css ||= <<-CSS
    body {
      font-family: sans-serif;
      font-size: 12px;
      background-color: #fff;
    }

    h1 {
      font-size: 20px;
      font-weight: bold;
    }

    ul {
      border: 1px dashed #999;
      padding: 10 10 10 20;
      background-color: #ccc;
    }
  CSS
end

#index(req, res) ⇒ Object



53
54
55
56
# File 'lib/gitjour/browser.rb', line 53

def index(req, res)
  res['Content-Type'] = 'text/html'
  res.body = index_html.result(binding)
end

#index_htmlObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gitjour/browser.rb', line 58

def index_html
  @index_html ||= ERB.new(<<-HTML)
    <html>
      <body>
        <head>
          <link rel="stylesheet" href="/style.css" type="text/css" media="screen"/>
          <title>Browseable Git Repositories</title>
        </head>
        <h1>Browseable Git Repositories</h1>
        <ul>
        <% @mutex.synchronize do %>
          <% @services.map do |s| %>
            <li>
              <a href='http://<%= s.host %>:<%= s.port %>' target="_new">
                <%= s.name %>
              </a>
              <%= s.description unless s.description =~ /^Unnamed repository/ %>
            </li>
          <% end %>
        <% end %>
        </ul>
      </body>
    </html>
  HTML
end

#startObject



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
# File 'lib/gitjour/browser.rb', line 16

def start
  DNSSD.browse("_http._tcp") do |reply|
    begin
      DNSSD.resolve reply.name, reply.type, reply.domain do |resolve_reply|
        next unless resolve_reply.text_record['gitjour']
        service = GitService.new(reply.name,
                                 resolve_reply.target,
                                 resolve_reply.port,
                                 resolve_reply.text_record['description'].to_s)

        @mutex.synchronize do
          if @services.member? service
            @services.delete service
          else
            @services << service
          end
        end
      end
    rescue ArgumentError # usually a jacked DNS text record
    end
  end

  http = WEBrick::HTTPServer.new(:Port => @port.to_i)
  http.mount_proc("/") { |req, res| index(req, res) }
  http.mount_proc("/style.css") { |req, res| stylesheet(req, res) }
  trap("INT") { http.shutdown }
  t = Thread.new { http.start }

  url = "http://localhost:#{@port}"
  if @browser
    `git web--browse -b '#{@browser}' http://localhost:9850`
  else
    `git web--browse -c "instaweb.browser" http://localhost:9850`
  end
  t.join
end

#stylesheet(req, res) ⇒ Object



84
85
86
87
# File 'lib/gitjour/browser.rb', line 84

def stylesheet(req, res)
  res['Content-Type'] = 'text/css'
  res.body = css
end