-
Notifications
You must be signed in to change notification settings - Fork 0
/
Luisterpaal.hs
84 lines (66 loc) · 2.72 KB
/
Luisterpaal.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{-# LANGUAGE TypeSynonymInstances #-}
module Luisterpaal where
import Control.Monad
import Control.Monad.Trans
import Control.Monad.Reader
import System.Environment
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as M
import Happstack.Server
import LastFm
import Pages
server :: IO (ServerPart Response)
server = do
conf <- getClientConf
return $ msum
[ withData (\token -> anyRequest $ createSession conf token) -- Callback URL
, withData (\session -> anyRequest $ shakeHands conf session)
, root [ withDataFn userCookie (\user -> anyRequest $ respond ok $ welcomeBack user)
, anyRequest $ respond ok (welcome $ api_key conf)
]
, dir "proxy" $ proxyServe ["*.audioscrobbler.com:80", "195.24.233.58:80"]
, fileServe [] "static"
]
-- Reading the Last.fm client config
getClientConf :: IO ClientConf
getClientConf = do
args <- getArgs
(key : secret : rest) <- readFile (head (args ++ [".last.fm"])) >>= return . words
let (id, version) = case rest of
[] -> ("tst", "1.0")
[i] -> (i , "1.0")
(i:v:_) -> (i , v )
return $ ClientConf key secret id version
-- Parsing submitted data
instance FromData Token where
fromData = look "token"
instance FromData Session where
fromData = do u <- look "user"
k <- look "key"
return $ Session u k
userCookie :: RqData User
userCookie = lookCookieValue "session" >>= return . takeWhile (/=':')
-- Handlers
createSession :: ClientConf -> Token -> Web Response
createSession conf token = do
session <- liftIO $ getSession conf token
case session of
Left err -> respond badRequest $ tryAgain (show err)
Right (Session user skey) -> do
addCookie 100000000 $ mkCookie "session" (user ++ ":" ++ skey)
redir "."
shakeHands :: ClientConf -> Session -> Web Response
shakeHands conf session = do
handshake <- liftIO $ getHandshake conf session
case handshake of
Left err -> respond ok $ "{error:" ++ show (show err) ++ "}"
Right (Handshake key npurl surl) -> respond ok $ "{handshake:" ++ show [key, npurl, surl] ++ "}"
-- Utilities
root :: (Monad m) => [ServerPartT m a] -> ServerPartT m a
root handlers = askRq >>= \rq -> if null $ rqPaths rq then msum handlers else mzero
redir :: String -> Web Response
redir u = found u (toResponse "")
respond :: ToMessage a => (Response -> Web Response) -> a -> Web Response
respond with = with . toResponse
lookHeader :: String -> Headers -> Maybe String
lookHeader name headers = M.lookup (B.pack name) headers >>= return . B.unpack . head . hValue