public
Fork of chad/gitjour
Description: Serve git and advertise with bonjour
Homepage: http://rubyforge.org/projects/gitjour
Clone URL: git://github.com/alexvollmer/gitjour.git
Search Repo:
Moved browsing webrick stuff to its own Browser class.
alexvollmer (author)
Fri Jun 06 21:01:31 -0700 2008
commit  862ccac7a5d11aea1621e46a4c60255a43f416bc
tree    8b93ca905d004c8099c66cc9f73fcef1d7ed871c
parent  9499e5551f31ce9e98b9bc050ef0917b9def2355
...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
 
289
290
291
...
313
314
315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
...
207
208
209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
211
212
213
...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
0
@@ -207,85 +207,7 @@ module Gitjour
0
       end
0
 
0
       def browse(*args)
0
- port = args.shift || 9850
0
- browser = args.shift
0
-
0
- services = Set.new
0
- mutex = Mutex.new
0
-
0
- DNSSD.browse("_http._tcp") do |reply|
0
- begin
0
- DNSSD.resolve reply.name, reply.type, reply.domain do |resolve_reply|
0
- service = GitService.new(reply.name,
0
- resolve_reply.target,
0
- resolve_reply.port,
0
- resolve_reply.text_record['description'].to_s)
0
-
0
- mutex.synchronize do
0
- if services.member? service
0
- services.delete service
0
- else
0
- services << service
0
- end
0
- end
0
- end
0
- rescue ArgumentError
0
- # usually a jacked DNS text record
0
- end
0
- end
0
-
0
- http = WEBrick::HTTPServer.new(:Port => port.to_i)
0
- http.mount_proc("/") do |req, res|
0
- res['Content-Type'] = 'text/html'
0
- res.body = <<-HTML
0
- <html>
0
- <body>
0
- <head>
0
- <link rel="stylesheet" href="/style.css" type="text/css" media="screen"/>
0
- </head>
0
- <h1>Browseable Git Repositories</h1>
0
- <ul>
0
- #{mutex.synchronize do
0
- services.map do |s|
0
- "<li><a href='http://#{s.host}:#{s.port}'>#{s.name}</a> #{s.description}</li>"
0
- end
0
- end}
0
- </ul>
0
- </body>
0
- </html>
0
- HTML
0
- end
0
- http.mount_proc("/style.css") do |req, res|
0
- res['Content-Type'] = 'text/css'
0
- res.body = <<-CSS
0
- body {
0
- font-family: sans-serif;
0
- font-size: 12px;
0
- background-color: #fff;
0
- }
0
-
0
- h1 {
0
- font-size: 20px;
0
- font-weight: bold;
0
- }
0
-
0
- ul {
0
- border: 1px dashed #999;
0
- padding: 10 10 10 20;
0
- background-color: #ccc;
0
- }
0
- CSS
0
- end
0
- trap("INT") { http.shutdown }
0
- t = Thread.new { http.start }
0
-
0
- url = "http://localhost:#{port}"
0
- if browser
0
- `git web--browse -b '#{browser}' http://localhost:9850`
0
- else
0
- `git web--browse -c "instaweb.browser" http://localhost:9850`
0
- end
0
- t.join
0
+ Browser.new(*args).start
0
       end
0
 
0
       def http_services
0
@@ -313,4 +235,92 @@ module Gitjour
0
 
0
     end
0
   end
0
+
0
+ class Browser
0
+ def initialize(*args)
0
+ @port = args.shift || 9850
0
+ @browser = args.shift
0
+ @services = Set.new
0
+ @mutex = Mutex.new
0
+ end
0
+
0
+ def start
0
+ DNSSD.browse("_http._tcp") do |reply|
0
+ begin
0
+ DNSSD.resolve reply.name, reply.type, reply.domain do |resolve_reply|
0
+ service = GitService.new(reply.name,
0
+ resolve_reply.target,
0
+ resolve_reply.port,
0
+ resolve_reply.text_record['description'].to_s)
0
+
0
+ @mutex.synchronize do
0
+ if @services.member? service
0
+ @services.delete service
0
+ else
0
+ @services << service if service.name =~ /\.git$/
0
+ end
0
+ end
0
+ end
0
+ rescue ArgumentError # usually a jacked DNS text record
0
+ end
0
+ end
0
+
0
+ http = WEBrick::HTTPServer.new(:Port => @port.to_i)
0
+ http.mount_proc("/") { |req, res| index(req, res) }
0
+ http.mount_proc("/style.css") { |req, res| css(req, res) }
0
+ trap("INT") { http.shutdown }
0
+ t = Thread.new { http.start }
0
+
0
+ url = "http://localhost:#{@port}"
0
+ if @browser
0
+ `git web--browse -b '#{@browser}' http://localhost:9850`
0
+ else
0
+ `git web--browse -c "instaweb.browser" http://localhost:9850`
0
+ end
0
+ t.join
0
+ end
0
+
0
+ def index(req, res)
0
+ res['Content-Type'] = 'text/html'
0
+ res.body = <<-HTML
0
+ <html>
0
+ <body>
0
+ <head>
0
+ <link rel="stylesheet" href="/style.css" type="text/css" media="screen"/>
0
+ </head>
0
+ <h1>Browseable Git Repositories</h1>
0
+ <ul>
0
+ #{@mutex.synchronize do
0
+ @services.map do |s|
0
+ "<li><a href='http://#{s.host}:#{s.port}'>#{s.name}</a> #{s.description}</li>"
0
+ end
0
+ end}
0
+ </ul>
0
+ </body>
0
+ </html>
0
+ HTML
0
+ end
0
+
0
+ def css(req, res)
0
+ res['Content-Type'] = 'text/css'
0
+ res.body = <<-CSS
0
+ body {
0
+ font-family: sans-serif;
0
+ font-size: 12px;
0
+ background-color: #fff;
0
+ }
0
+
0
+ h1 {
0
+ font-size: 20px;
0
+ font-weight: bold;
0
+ }
0
+
0
+ ul {
0
+ border: 1px dashed #999;
0
+ padding: 10 10 10 20;
0
+ background-color: #ccc;
0
+ }
0
+ CSS
0
+ end
0
+ end
0
 end

Comments

    No one has commented yet.