Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 65 lines (55 sloc) 1.914 kb
bb2ea14 @mrtazz add basic app
authored
1 require 'guillotine'
cb7aa22 @mrtazz add redistogo support
authored
2 require 'redis'
bb2ea14 @mrtazz add basic app
authored
3
4 module Katana
5 class App < Guillotine::App
cb7aa22 @mrtazz add redistogo support
authored
6 # use redis adapter with redistogo
7 uri = URI.parse(ENV["REDISTOGO_URL"])
8 REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
9 adapter = Guillotine::Adapters::RedisAdapter.new REDIS
b12c3a0 @mrtazz don't strip query and achor params
authored
10 set :service => Guillotine::Service.new(adapter, :strip_query => false,
11 :strip_anchor => false)
bb2ea14 @mrtazz add basic app
authored
12
13 # authenticate everything except GETs
14 before do
15 unless request.request_method == "GET"
16 protected!
17 end
18 end
19
20 get '/' do
cb7aa22 @mrtazz add redistogo support
authored
21 "Shorten all the URLs"
bb2ea14 @mrtazz add basic app
authored
22 end
23
144976c @mrtazz add experimental API endpoint for tweetbot
authored
24 if ENV['TWEETBOT_API']
25 # experimental (unauthenticated) API endpoint for tweetbot
26 get '/api/create/?' do
27 status, head, body = settings.service.create(params[:url], params[:code])
28
29 if loc = head['Location']
6ac143d @mrtazz just return URL for tweetbot
authored
30 "#{File.join("http://", request.host, loc)}"
144976c @mrtazz add experimental API endpoint for tweetbot
authored
31 else
32 500
33 end
34 end
35 end
36
bb2ea14 @mrtazz add basic app
authored
37 # helper methods
38 helpers do
39
40 # Private: helper method to protect URLs with Rack Basic Auth
41 #
42 # Throws 401 if authorization fails
43 def protected!
44 return unless ENV["HTTP_USER"]
45 unless authorized?
46 response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
47 throw(:halt, [401, "Not authorized\n"])
48 end
49 end
50
51 # Private: helper method to check if authorization parameters match the
52 # set environment variables
53 #
54 # Returns true or false
55 def authorized?
56 @auth ||= Rack::Auth::Basic::Request.new(request.env)
57 user = ENV["HTTP_USER"]
58 pass = ENV["HTTP_PASS"]
59 @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == [user, pass]
60 end
61 end
62
63 end
64 end
Something went wrong with that request. Please try again.