jimi
12 years ago
commit
c932349675
4 changed files with 111 additions and 0 deletions
-
34app.rb
-
3config.ru
-
17views/index.haml
-
57views/styles.scss
@ -0,0 +1,34 @@ |
|||
require 'sinatra' |
|||
require 'redis' |
|||
require 'haml' |
|||
require 'sass' |
|||
|
|||
redis = Redis.new |
|||
|
|||
helpers do |
|||
include Rack::Utils |
|||
alias_method :h, :escape_html |
|||
|
|||
def random_string(length) |
|||
rand(36**length).to_s(36) |
|||
end |
|||
end |
|||
|
|||
get ('/styles/main.css'){ scss :styles } |
|||
|
|||
get '/' do |
|||
haml :index |
|||
end |
|||
|
|||
post '/' do |
|||
if params[:url] and not params[:url].empty? |
|||
@shortcode = random_string 5 |
|||
redis.setnx "links:#{@shortcode}", params[:url] |
|||
end |
|||
haml :index |
|||
end |
|||
|
|||
get '/:shortcode' do |
|||
@url = redis.get "links:#{params[:shortcode]}" |
|||
redirect @url || '/' |
|||
end |
@ -0,0 +1,3 @@ |
|||
require './app' |
|||
|
|||
run Sinatra::Application |
@ -0,0 +1,17 @@ |
|||
!!!5 |
|||
%html |
|||
%head |
|||
%title Shortlink App |
|||
%link{ rel:"stylesheet", href:"/styles/main.css" } |
|||
%body |
|||
.container |
|||
%h1 shortlink app |
|||
%form{ method:"post" } |
|||
%input{ type:"text", value:"#{params[:url]}", name:"url", id:"url" } |
|||
%input{ type:"submit", value:"shorten", id:"submit" } |
|||
.clear |
|||
-if @shortcode |
|||
.result |
|||
Your shortened URL is: |
|||
%a{ href:"http://jinfield.com/#{@shortcode}" } |
|||
href:"http://jinfield.com/#{@shortcode}" |
@ -0,0 +1,57 @@ |
|||
body { |
|||
font-family: "Gill Sans", "Gill Sans MT", Sans-Serif; } |
|||
|
|||
.container { |
|||
width: 400px; |
|||
margin: 120px auto 0px auto; } |
|||
|
|||
h1 { |
|||
width: 400px; |
|||
margin-bottom: 12px; |
|||
text-align: center; |
|||
color: #ff4b33; |
|||
font-size: 40px; |
|||
padding-bottom: 8px; |
|||
border-bottom: 2px solid #ff4b33; } |
|||
|
|||
form { |
|||
display: block; |
|||
width: 400px; } |
|||
|
|||
input { |
|||
display: block; |
|||
float: left; |
|||
padding: 8px; |
|||
font-size: 16px; } |
|||
|
|||
#url { |
|||
width: 280px; |
|||
margin-right: 12px; } |
|||
|
|||
#submit { |
|||
width: 88px; |
|||
border: none; |
|||
background: #ff4b33; |
|||
padding: 10px; |
|||
&:hover { |
|||
background: #ff7866; } } |
|||
|
|||
.clear { |
|||
height: 1px; |
|||
width: 400px; |
|||
clear: both; } |
|||
|
|||
.result { |
|||
clear: both; |
|||
width: 400px; |
|||
margin-top: 12px; |
|||
border-top: 2px solid #ff4b33; |
|||
padding-top: 12px; |
|||
text-align: center; |
|||
a { |
|||
font-size: 24px; |
|||
display: block; |
|||
margin-top: 8px; |
|||
color: #ff2d11; |
|||
background: #ffd2cc; |
|||
padding: 8px; } } |
Reference in new issue