-
Notifications
You must be signed in to change notification settings - Fork 5
/
lilurl.rb
48 lines (43 loc) · 1.14 KB
/
lilurl.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'rubygems'
require 'sinatra'
require 'sinatra/contrib'
require 'json'
require './translate.rb'
set :bind, '0.0.0.0'
get '/' do
erb :index, :locals => {:error => nil}
end
get '/:hash' do
begin
if params[:hash] != "favicon.ico"
newurl=geturl(params[:hash])
redirect to(newurl)
end
rescue ArgumentError => e
erb :index, :locals => {:error => e.to_s}
end
end
post '/' do
if settings.development?
port = ":" + settings.port.to_s
else
port = ""
end
begin
oldurl = params[:oldurl]
postfix = params[:postfix]
newurl=makeurl(oldurl, postfix)
newurl_hash = { 'newurl' => newurl }
respond_with :index do |format|
format.html { erb :index, :locals => {:domain => request.host + port, :newurl => newurl, :error => nil}}
format.json { "{newurl: http://#{request.host}#{port}/#{newurl}}" }
end
rescue ArgumentError => e
respond_with :index do |format|
format.html { erb :index, :locals => {:error => e.to_s }}
format.json { "{error: #{e.to_s}}" }
end
rescue SQLite3::Exception => e
erb :index, :locals => {:error => "Database error: " + e.to_s }
end
end