Skip to content

Commit

Permalink
added support for persistence using redis
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkdoor committed Oct 26, 2011
1 parent 845dbf8 commit addae7d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
4 changes: 3 additions & 1 deletion README
@@ -1,4 +1,6 @@
Simple link shortener written in Fancy using sinatra.fy
Has no persistence yet.
Uses Redis for persisting data.

See: http://fancy-lang.org
http://github.com/bakkdoor/redis.fy
http://github.com/rkh/sinatra.fy
37 changes: 22 additions & 15 deletions app.fy
Expand Up @@ -2,8 +2,9 @@ require("rubygems")
require("sha1")
require: "sinatra"
require: "html"
require: "redis"

LINKS = <[]>
R = Redis Client new

configure: { enable: 'sessions }
configure: 'production with: { disable: 'show_errors }
Expand All @@ -14,19 +15,29 @@ configure: ['production, 'development] with: {
set: 'port to: 3000

# basic layout
def with_layout: b {
def with_layout: body {
HTML new: |h| {
h html: {
h head: {
h title: "Shortefy v0.0.1"
}
h body: {
b call: [h]
body(h)
}
}
} to_s
}

def with_link: id do: block else: else_block ({ "" }) {
if: (R call: ('get, key: id)) then: |link| {
block(link)
} else: else_block
}

def key: id {
"shortefy:#{id}"
}

get: "/" do: {
with_layout: |h| {
h form: <['action => "/new", 'method => "post"]> with: {
Expand All @@ -44,30 +55,26 @@ get: "/" do: {
post: "/new" do: {
link = params['link]
key = SHA1 new(link) to_s [[0, 10]]
LINKS[key]: link
R call: ('set, key: key, link)
redirect: "/show/#{key}"
}

get: /^\/show/([a-zA-Z0-9]+)/ do: |id| {
if: (get_link: id) then: |link| {
get: "/show/:id" do: |id| {
with_link: id do: |link| {
with_layout: |h| {
h h1: "ID: #{id}"
h h2: {
h a: <['href => link]> with: link
}
}
} else: {
redirect: "/"
}
}

get: /^\/([a-zA-Z0-9]+)/ do: |id| {
redirect: $ get_link: id
}

def get_link: id {
if: (LINKS[id]) then: |link| {
link
} else: {
"Link '#{id}' does not exist!" raise!
get: "/:id" do: |id| {
with_link: id do: |link| {
redirect: link
}
}

Expand Down
2 changes: 1 addition & 1 deletion shortefy.fancypack
Expand Up @@ -5,5 +5,5 @@ Fancy Package Specification new: "shortefy" with: {
description: "A Fancy link shortening service."
homepage: "http://www.fancy-lang.org"
version: "0.0.1"
dependencies: [["rkh/sinatra.fy"]]
dependencies: [["rkh/sinatra.fy"], ["bakkdoor/redis.fy"]]
}

0 comments on commit addae7d

Please sign in to comment.