better way of running rails, css and javascript works, nice looking not found page
[zip-doc.git] / zipweb.rb
blobaea195b5c5f6455ee0bc61df441401d0aea465ee
1 #!/usr/bin/ruby
2 require 'rubygems'
3 require 'mongrel'
4 homedir = '/users/stian/source/zip-doc'
5 require File.join(homedir, 'zarchive')
6 require File.join(homedir, 'htmlshrinker')
7                            
8               
9 archive = ZArchive.new(ARGV[0])
10 htmlshrink = HTMLShrinker.new("/id")
11 class SimpleHandler < Mongrel::HttpHandler
12   def process(req, resp)
13      p req
14     t = Time.now    
15     puts req
16     txt = archive.get_article(req.unparsed_uri[3..-1])
17     if txt.nil?
18       out.write = "Sorry, article not found" 
19     else
20       out.write = htmlshrink.uncompress(txt)
21     end
22     puts "Served in #{Time.now - t} seconds."
23   end
24 end 
26 search_proc = lambda do |req, resp|
27   t = Time.now
28   resp['Content-Type'] = "text/html"
29   search_query =req.unparsed_uri[("search".size+2)..-1]
30   puts search_queryearch_query
31   @found_documents=[]
32   FERRET.search_each(search_query) do |docid, score| 
33     @found_documents << {:path => FERRET.reader.get_document(docid)[:filename], 
34     :score => score, :highlight => FERRET.highlight(search_query, 
35     docid, :pre_tag => '<b>', :post_tag => '</b>',
36     :field => :content)}
37   end
38   resp.body = ""
39   unless @found_documents.empty?
40     resp.body <<  "<h1>Search results for query <i>#{search_query}</i></h1>"
41     @found_documents.each do |doc|
42       resp.body << "<li><a href=/d/#{doc[:path]}>#{doc[:path]}</a><br>#{doc[:highlight]}<br><hr>"
43     end
44   else
45     resp.body << "Sorry, no documents found"
46   end
47   puts "Served in #{Time.now - t} seconds."
48 end
50 h = Mongrel::HttpServer.new("0.0.0.0", "3000")
51 h.register("/", SimpleHandler.new)
52 h.register("/files", Mongrel::DirHandler.new("."))
53 h.run.join