Created
August 24, 2012 05:33
-
-
Save itsuart/3445894 to your computer and use it in GitHub Desktop.
Simple url redirector in 30 lines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings #-} | |
import Yesod | |
import Yesod.Handler | |
import qualified Data.Text as T | |
import qualified Data.Map as M | |
import System.IO | |
type ShurlsMap = M.Map T.Text String | |
myMap :: ShurlsMap | |
myMap = M.insert (T.pack "g") "https://google.com/ncr" M.empty | |
data HsRedir = HsRedir | |
mkYesod "HsRedir" [parseRoutes| | |
/#T.Text RedirR GET | |
|] | |
instance Yesod HsRedir | |
-- Handlers | |
getRedirR :: T.Text -> Handler RepHtml | |
getRedirR text = do | |
case M.lookup text myMap of | |
Just url -> redirect url | |
Nothing -> notFound | |
main :: IO () | |
main = warpDebug 3000 HsRedir |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment