reverting to raw dump, enable cacheing

Jason Ormand 12 年之前
父節點
當前提交
7e41f12568
共有 6 個文件被更改,包括 23 次插入66 次删除
  1. 1 0
      Gemfile
  2. 7 0
      Gemfile.lock
  3. 9 21
      main.rb
  4. 1 1
      public/css/styles.css
  5. 2 1
      views/error.haml
  6. 3 43
      views/lookup.haml

+ 1 - 0
Gemfile

@@ -1,5 +1,6 @@
1 1
 source 'http://rubygems.org'
2 2
 
3
+gem 'unicorn'
3 4
 gem 'sinatra'
4 5
 gem 'whois'
5 6
 gem 'haml'

+ 7 - 0
Gemfile.lock

@@ -3,14 +3,20 @@ GEM
3 3
   specs:
4 4
     haml (3.1.6)
5 5
     json (1.7.3)
6
+    kgio (2.7.4)
6 7
     rack (1.4.1)
7 8
     rack-protection (1.2.0)
8 9
       rack
10
+    raindrops (0.10.0)
9 11
     sinatra (1.3.2)
10 12
       rack (~> 1.3, >= 1.3.6)
11 13
       rack-protection (~> 1.2)
12 14
       tilt (~> 1.3, >= 1.3.3)
13 15
     tilt (1.3.3)
16
+    unicorn (4.3.1)
17
+      kgio (~> 2.6)
18
+      rack
19
+      raindrops (~> 0.7)
14 20
     whois (2.6.3)
15 21
 
16 22
 PLATFORMS
@@ -20,4 +26,5 @@ DEPENDENCIES
20 26
   haml
21 27
   json
22 28
   sinatra
29
+  unicorn
23 30
   whois

+ 9 - 21
main.rb

@@ -11,25 +11,11 @@ end
11 11
 helpers do
12 12
 
13 13
   def cache_for_day
14
-    if settings.environment != :development
15
-      response['Cache-Control'] = 'public, max-age=86400'
16
-    end
14
+    response['Cache-Control'] = 'public, max-age=86400'
17 15
   end
18 16
 
19 17
 	def whois_lookup
20 18
 		lookup_info = Whois.query(params[:url])
21
-		admin_contacts = Hash[lookup_info.admin_contacts[0].each_pair.to_a]
22
-		technical_contacts = Hash[lookup_info.technical_contacts[0].each_pair.to_a]
23
-
24
-		{
25
-			:domain => lookup_info.domain,
26
-			:created_on => lookup_info.created_on,
27
-			:expires_on => lookup_info.expires_on,
28
-			:whois_servers => lookup_info.referral_whois,
29
-			:nameservers => lookup_info.nameservers,
30
-			:admin_contacts => admin_contacts,
31
-			:techical_contacts => technical_contacts
32
-		}
33 19
 	end
34 20
 
35 21
 end
@@ -43,10 +29,11 @@ end
43 29
 
44 30
 get '/lookup' do
45 31
 	begin
46
-		# cache_for_day
32
+		cache_for_day
47 33
 		@whois = whois_lookup
48 34
 		haml :lookup
49
-	rescue
35
+	rescue Exception => e
36
+		@error = e
50 37
 		haml :error
51 38
 	end
52 39
 end
@@ -54,9 +41,10 @@ end
54 41
 
55 42
 get '/lookup.json' do
56 43
 	begin
57
-		# cache_for_day
58
-		whois_lookup.to_json
59
-	rescue
60
-		{:Error => 'Bad Request'}.to_json
44
+		cache_for_day
45
+		whois_lookup.to_s.force_encoding('utf-8').encode.to_json
46
+	rescue Exception => e
47
+		@error = e
48
+		{:Error => @error}.to_json
61 49
 	end
62 50
 end

+ 1 - 1
public/css/styles.css

@@ -16,7 +16,7 @@ p {
16 16
 }
17 17
 
18 18
 .info {
19
-  width: 500px;
19
+  width: 800px;
20 20
   margin: 0 auto 15px auto;
21 21
   padding: 15px;
22 22
   border: solid 1px #ccc;

+ 2 - 1
views/error.haml

@@ -5,4 +5,5 @@
5 5
     %link{:rel => "stylesheet", :type => "text/css", :href => "/css/styles.css"}
6 6
   %body
7 7
     .info
8
-      %h1 Error: invalid request
8
+      %h1 Error:
9
+      %p= @error

+ 3 - 43
views/lookup.haml

@@ -5,46 +5,6 @@
5 5
 		%link{:rel => 'stylesheet', :type => 'text/css', :href => '/css/styles.css'}
6 6
 	%body
7 7
 		.info
8
-			%p.title URL
9
-			%p.content
10
-				= @whois[:domain]
11
-
12
-		.info
13
-			%p.title Admin Contact
14
-			.content
15
-				- if @whois[:admin_contacts]
16
-					- @whois[:admin_contacts].each do |contact|
17
-						= "<p><strong> #{contact[0]} </strong>: #{contact[1]} </p>"
18
-				- else
19
-					= 'none'
20
-
21
-		.info
22
-			%p.title Technical Contact
23
-			.content
24
-				- if @whois[:technical_contacts]
25
-					- @whois[:technical_contacts].each do |contact|
26
-						= "<p> #{contact} </p>"
27
-				- else
28
-					= 'none'
29
-
30
-		.info
31
-			%p.title Create Date
32
-			%p.content
33
-				= @whois[:created_on]
34
-
35
-		.info
36
-			%p.title Expire Date
37
-			%p.content
38
-				= @whois[:expires_on]
39
-
40
-		.info
41
-			%p.title WHOIS Server
42
-			%ul.content
43
-				- @whois[:whois_servers].each do |server|
44
-					= "<li> #{server} </li>"
45
-
46
-		.info
47
-			%p.title Nameservers
48
-			%ul.content
49
-				- @whois[:name_servers].each do |server|
50
-					= "<li> #{server} </li>"
8
+			%p.title Lookup
9
+			%pre.content
10
+				= @whois