got app working reasonably well, and removed db file, that shouldn't be in the db
parent
3e789f832a
commit
1652107549
11
app.rb
11
app.rb
|
|
@ -1,8 +1,12 @@
|
|||
require 'roda'
|
||||
require 'securerandom'
|
||||
require 'json'
|
||||
require 'sequel'
|
||||
require 'logger'
|
||||
|
||||
class App < Roda
|
||||
DB = Sequel.sqlite('db/url_shortener.db')
|
||||
links = DB[:links]
|
||||
plugin :render, escape: true
|
||||
route do |r|
|
||||
r.root do
|
||||
|
|
@ -10,10 +14,15 @@ class App < Roda
|
|||
end
|
||||
|
||||
r.get String do | url_code |
|
||||
"redirect to url"
|
||||
link = links.filter(:code => url_code).first[:url]
|
||||
r.redirect link
|
||||
end
|
||||
|
||||
r.post "create" do
|
||||
url = r.params['url']
|
||||
code = SecureRandom.urlsafe_base64 4
|
||||
links.insert(url: url, code: code)
|
||||
@new_link = 'http://' + request.env['HTTP_HOST'] + '/' + links.filter(:url => url).first[:code]
|
||||
view :create
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -0,0 +1,2 @@
|
|||
<a href="/">Home</a>
|
||||
<h2><a href="<%= @new_link %>" target="_blank"><%= @new_link %></a></h2>
|
||||
Loading…
Reference in New Issue