Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 165 lines (134 sloc) 3.614 kb
92c4c2c @juster First commit, almost everything works.
authored
1 require 'sinatra'
2 require 'erb'
3 require 'json/ext'
4
562d545 @juster Remove extra whitespace.
authored
5 APPDIR = File.dirname(__FILE__)
92c4c2c @juster First commit, almost everything works.
authored
6
dbe84c3 @juster Minor code cleanup.
authored
7 $: << (APPDIR + '/lib')
92c4c2c @juster First commit, almost everything works.
authored
8 require 'aurlite'
9
10 STATICDIR = APPDIR + '/../static'
11 DB_PATH = APPDIR + '/../var/aurlite.db'
12
13 MIN_SECS = 60
14 HOUR_SECS = MIN_SECS * 60
15 DAY_SECS = HOUR_SECS * 24
76a48a0 @juster Provide a constant for limit count.
authored
16 RESULTS_LIMIT = 500
92c4c2c @juster First commit, almost everything works.
authored
17
18 set :public, STATICDIR
19
562d545 @juster Remove extra whitespace.
authored
20 $AURDB = AURLite.new(DB_PATH)
92c4c2c @juster First commit, almost everything works.
authored
21
562d545 @juster Remove extra whitespace.
authored
22 def timediff_str(oldtime)
92c4c2c @juster First commit, almost everything works.
authored
23 mdiff = Time.now - oldtime
24 diffstr = ""
25
26 if mdiff >= DAY_SECS then
27 mdiff = (mdiff / DAY_SECS).to_i
28 diffstr = "#{mdiff} day"
29 elsif mdiff >= HOUR_SECS then
30 mdiff = (mdiff / HOUR_SECS).to_i
31 diffstr = "#{mdiff} hour"
32 else
33 mdiff = (mdiff / MIN_SECS).to_i
34 diffstr = "#{mdiff} min"
35 end
36
37 diffstr << 's' if mdiff > 1
38 return diffstr
39 end
40
c96f5d4 @juster Forgot. Adds rev. deps and searching by fields (ie license).
authored
41 def next_url (after, extrap)
20d04b3 @juster Determine relative URLs based on the hostname and path in request.
authored
42 url = 'http://' + request.host + request.path
c96f5d4 @juster Forgot. Adds rev. deps and searching by fields (ie license).
authored
43 if extrap
44 qparams = extrap.collect{|k,v| "#{k}=#{v}"}
92c4c2c @juster First commit, almost everything works.
authored
45 else
c96f5d4 @juster Forgot. Adds rev. deps and searching by fields (ie license).
authored
46 qparams = []
47 end
48 qparams << "after=#{after}" if after
49
50 if qparams.empty?
92c4c2c @juster First commit, almost everything works.
authored
51 return url
c96f5d4 @juster Forgot. Adds rev. deps and searching by fields (ie license).
authored
52 else
53 return url + '?' + qparams.join(';')
92c4c2c @juster First commit, almost everything works.
authored
54 end
55 end
56
3ef8e76 @juster Fix incorrect urls in response data.
authored
57 def aurpc_url ()
58 'http://' + request.host + '/aurpc'
59 end
60
562d545 @juster Remove extra whitespace.
authored
61 def package_url(pkgname)
3ef8e76 @juster Fix incorrect urls in response data.
authored
62 aurpc_url + "/packages/#{pkgname}"
92c4c2c @juster First commit, almost everything works.
authored
63 end
64
562d545 @juster Remove extra whitespace.
authored
65 def author_url(author)
3ef8e76 @juster Fix incorrect urls in response data.
authored
66 aurpc_url + "/authors/#{author}"
92c4c2c @juster First commit, almost everything works.
authored
67 end
68
c96f5d4 @juster Forgot. Adds rev. deps and searching by fields (ie license).
authored
69 def pkg_matches (pkgs, extrap=nil)
92c4c2c @juster First commit, almost everything works.
authored
70 pkgs.each do |pkg|
c96f5d4 @juster Forgot. Adds rev. deps and searching by fields (ie license).
authored
71 pkg[:alink] = author_url(pkg[:author])
72 pkg[:pkglink] = package_url(pkg[:name])
92c4c2c @juster First commit, almost everything works.
authored
73 end
74
208745f @juster Only provide "next" urls when needed.
authored
75 matchdata = { :matches => pkgs }
c96f5d4 @juster Forgot. Adds rev. deps and searching by fields (ie license).
authored
76 matchdata[:nextlink] =
208745f @juster Only provide "next" urls when needed.
authored
77 if pkgs.length == RESULTS_LIMIT then
c96f5d4 @juster Forgot. Adds rev. deps and searching by fields (ie license).
authored
78 next_url(pkgs.last[:name], extrap)
208745f @juster Only provide "next" urls when needed.
authored
79 else
80 nil
81 end
82 return matchdata
92c4c2c @juster First commit, almost everything works.
authored
83 end
84
562d545 @juster Remove extra whitespace.
authored
85 def author_matches(anames)
08a96ef @juster Add authors iterator.
authored
86 amatches = anames.collect do |aname|
562d545 @juster Remove extra whitespace.
authored
87 { :name => aname, :url => author_url(aname) }
08a96ef @juster Add authors iterator.
authored
88 end
89
208745f @juster Only provide "next" urls when needed.
authored
90 matchdata = { :matches => amatches }
c96f5d4 @juster Forgot. Adds rev. deps and searching by fields (ie license).
authored
91 matchdata[:nextlink] = if amatches.length != RESULTS_LIMIT then nil
562d545 @juster Remove extra whitespace.
authored
92 else next_url(anames.last) end
208745f @juster Only provide "next" urls when needed.
authored
93 return matchdata
08a96ef @juster Add authors iterator.
authored
94 end
95
562d545 @juster Remove extra whitespace.
authored
96 def find_pkg_glob(glob)
92c4c2c @juster First commit, almost everything works.
authored
97 after = params[:after] || ""
562d545 @juster Remove extra whitespace.
authored
98 pkgs = $AURDB.glob_pkg(glob, after)
d06e6b3 @juster Change all 400 errors to 404.
authored
99 halt 404, "No matches found" unless pkgs.length > 0
92c4c2c @juster First commit, almost everything works.
authored
100
c96f5d4 @juster Forgot. Adds rev. deps and searching by fields (ie license).
authored
101 return JSON.generate pkg_matches(pkgs)
92c4c2c @juster First commit, almost everything works.
authored
102 end
103
104 get '/' do
105 size_of = {}
106 %W{ xz gz bz2 }.each do |ext|
107 path = STATICDIR + "/aurlite.db." + ext
108 size_of[ext] =
109 begin
562d545 @juster Remove extra whitespace.
authored
110 mbs = File.stat(path).size.to_f / (1024 ** 2);
92c4c2c @juster First commit, almost everything works.
authored
111 sprintf '%0.2f', mbs
112 rescue Errno::ENOENT then "-999" end
113 end
114
115 erb :index, :locals => {
116 :download_sizes => size_of,
562d545 @juster Remove extra whitespace.
authored
117 :freshness => timediff_str(File.stat(DB_PATH).mtime)
92c4c2c @juster First commit, almost everything works.
authored
118 }
119 end
120
121 get '/packages/:name' do |name|
562d545 @juster Remove extra whitespace.
authored
122 return find_pkg_glob(name) if name =~ /[*]/
92c4c2c @juster First commit, almost everything works.
authored
123
562d545 @juster Remove extra whitespace.
authored
124 pkginfo = $AURDB.lookup_pkg(name)
d06e6b3 @juster Change all 400 errors to 404.
authored
125 halt 404, "#{name} was not found" unless pkginfo
92c4c2c @juster First commit, almost everything works.
authored
126
562d545 @juster Remove extra whitespace.
authored
127 pkginfo[:alink] = author_url(pkginfo[:author])
92c4c2c @juster First commit, almost everything works.
authored
128 JSON.generate pkginfo
129 end
130
c96f5d4 @juster Forgot. Adds rev. deps and searching by fields (ie license).
authored
131 SRCHKEYS = [ :desc, :url ]
92c4c2c @juster First commit, almost everything works.
authored
132 get '/packages' do
133 after = params[:after] || ""
c96f5d4 @juster Forgot. Adds rev. deps and searching by fields (ie license).
authored
134 q = {}
135 for k in SRCHKEYS do
136 q[k] = params[k] if params[k]
137 end
138 pkgs = if q.empty?
139 $AURDB.iter_pkgs(after)
140 else
141 $AURDB.search_pkgs(after, q)
142 end
143 JSON.generate pkg_matches(pkgs, q)
3f505a9 @juster Add author lookup URL. Rename prep_pkg_matches to pkg_matches.
authored
144 end
145
146 get '/authors/:name' do |name|
562d545 @juster Remove extra whitespace.
authored
147 authorinfo = $AURDB.lookup_author(name)
d06e6b3 @juster Change all 400 errors to 404.
authored
148 halt 404, "#{name} was not found" unless authorinfo
3f505a9 @juster Add author lookup URL. Rename prep_pkg_matches to pkg_matches.
authored
149
562d545 @juster Remove extra whitespace.
authored
150 authorinfo[:packages].each { |pkg| pkg[:url] = package_url(pkg[:name]) }
3f505a9 @juster Add author lookup URL. Rename prep_pkg_matches to pkg_matches.
authored
151 JSON.generate authorinfo
92c4c2c @juster First commit, almost everything works.
authored
152 end
08a96ef @juster Add authors iterator.
authored
153
154 get '/authors' do
562d545 @juster Remove extra whitespace.
authored
155 authors = $AURDB.authors_iter(params[:after] || %Q{})
08a96ef @juster Add authors iterator.
authored
156 halt 404, 'No matches found' if authors.empty?
562d545 @juster Remove extra whitespace.
authored
157 JSON.generate author_matches(authors)
08a96ef @juster Add authors iterator.
authored
158 end
3cb477e @juster Add /packages/:pkg/dependants path for listing reverse deps.
authored
159
160 get '/packages/:match/dependants' do |name|
161 dependants = $AURDB.dependants(name)
162 halt 404, 'No matches found' if dependants.empty?
163 JSON.generate dependants
164 end
Something went wrong with that request. Please try again.