Skip to content

Commit

Permalink
Add redirect functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Trinkle committed Nov 15, 2013
1 parent 5ed5a20 commit 4692ced
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions .ghci
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
:set -XOverloadedStrings
:set -XTemplateHaskell
:set -XFlexibleInstances
:set -XScopedTypeVariables
3 changes: 2 additions & 1 deletion memoise.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: memoise
Version: 0.8
Version: 0.9
License: BSD3
Author: Ryan Trinkle
Maintainer: [email protected]
Expand All @@ -26,3 +26,4 @@ Executable memoise
Extensions: OverloadedStrings
, TemplateHaskell
, FlexibleInstances
, ScopedTypeVariables
15 changes: 14 additions & 1 deletion src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Snap
import Snap.Snaplet.Heist
import Snap.Snaplet.PostgresqlSimple
import Snap.Util.FileServe
import Snap.Extras.CoreUtils
import Snap.Extras.TextUtils
import Control.Lens
import Data.Text
Expand Down Expand Up @@ -38,10 +39,21 @@ indexHandler = do
case mUrl of
Just url -> do
urlId <- getUrlId $ decodeUtf8 url
mainTextboxContents .= Just ("URL saved with id " <> showT urlId)
mainTextboxContents .= Just ("http://memoi.se/" <> showT urlId)
render "index"
Nothing -> render "index"

redirectHandler :: Handler Memoise Memoise ()
redirectHandler = do
mUrlId :: Maybe Integer <- readMayParam "urlId"
case mUrlId of
Nothing -> redirect "/"
Just urlId -> do
results <- query "SELECT url FROM urls WHERE id = ?" (Only urlId)
case results of
(Only url) : _ -> redirect url
[] -> redirect "/"

mainTextboxAttributeSplice :: AttrSplice (Handler Memoise Memoise)
mainTextboxAttributeSplice _ = do
mContents <- lift $ use mainTextboxContents
Expand All @@ -55,6 +67,7 @@ memoiseInit = makeSnaplet "memoise" "The world's laziest hyperlink shortener" No
modifyHeistState $ bindAttributeSplices [("main-textbox", mainTextboxAttributeSplice)]
d <- nestSnaplet "db" db pgsInit
addRoutes [ ("static", serveDirectory "static")
, (":urlId", redirectHandler)
, ("", indexHandler)
]
return $ Memoise { _heist = h
Expand Down

0 comments on commit 4692ced

Please sign in to comment.