Simple Tutsplus demo app utilizing Sinatra and Redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

34 lines
556 B

12 years ago
  1. require 'sinatra'
  2. require 'redis'
  3. require 'haml'
  4. require 'sass'
  5. redis = Redis.new
  6. helpers do
  7. include Rack::Utils
  8. alias_method :h, :escape_html
  9. def random_string(length)
  10. rand(36**length).to_s(36)
  11. end
  12. end
  13. get ('/styles/main.css'){ scss :styles }
  14. get '/' do
  15. haml :index
  16. end
  17. post '/' do
  18. if params[:url] and not params[:url].empty?
  19. @shortcode = random_string 5
  20. redis.setnx "links:#{@shortcode}", params[:url]
  21. end
  22. haml :index
  23. end
  24. get '/:shortcode' do
  25. @url = redis.get "links:#{params[:shortcode]}"
  26. redirect @url || '/'
  27. end