handleRequest staticPath sendmail tokenKey req sendResponse = do
  bracket
    (pgConnect defaultPGDatabase {pgDBAddr = Right (SockAddrUnix "/var/run/postgresql/.s.PGSQL.5432"), pgDBName = "vocabulink", pgDBUser = "vocabulink"})
    pgDisconnect
    (\ db -> do
        member <- loggedIn db
        let ?db = db
            ?static = staticPath
            ?sendmail = sendmail
            ?tokenKey = tokenKey
            ?member = member
            ?referrer = (toS `fmap` requestHeaderReferer req) :: Maybe String
        vocabulinkApp req sendResponse `catches` [Handler (vocabulinkHandler sendResponse), Handler defaultHandler])
 where loggedIn db = do
         let auth = (lookup "auth" . parseCookies) =<< (lookup "Cookie" (requestHeaders req))
         token' <- maybe (return Nothing) (validAuth tokenKey . toS) auth
         case token' of
           Nothing -> return Nothing
           Just token -> do
             row <- $(queryTuple
                      "SELECT username, email FROM member \
                      \WHERE member_no = {authMemberNumber token}") db
             return ((\(username, email) -> Member { memberNumber = authMemberNumber token
                                                   , memberName   = username
                                                   , memberEmail  = email
                                                   }) `fmap` row)
       defaultHandler :: SomeException -> _
       defaultHandler _ = sendResponse $ responseLBS status500 [] "An error occurred."
handleRequest staticPath sendmail tokenKey req sendResponse = do
  bracket
    (pgConnect defaultPGDatabase {pgDBAddr = Right (SockAddrUnix "/var/run/postgresql/.s.PGSQL.5432"), pgDBName = "vocabulink", pgDBUser = "vocabulink"})
    pgDisconnect
    (\ db -> do
        member <- loggedIn db
        let ?db = db
            ?static = staticPath
            ?sendmail = sendmail
            ?tokenKey = tokenKey
            ?member = member
            ?referrer = (toS `fmap` requestHeaderReferer req) :: Maybe String
        vocabulinkApp req sendResponse `catches` [Handler (vocabulinkHandler sendResponse), Handler defaultHandler])
 where loggedIn db = do
         let auth = (lookup "auth" . parseCookies) =<< (lookup "Cookie" (requestHeaders req))
         token' <- maybe (return Nothing) (validAuth tokenKey . toS) auth
         case token' of
           Nothing -> return Nothing
           Just token -> do
             row <- $(queryTuple
                      "SELECT username, email FROM member \
                      \WHERE member_no = {authMemberNumber token}") db
             return ((\(username, email) -> Member { memberNumber = authMemberNumber token
                                                   , memberName   = username
                                                   , memberEmail  = email
                                                   }) `fmap` row)
       defaultHandler :: SomeException -> _
       defaultHandler _ = sendResponse $ responseLBS status500 [] "An error occurred."
handleRequest staticPath sendmail tokenKey req sendResponse = do
  bracket
    (pgConnect defaultPGDatabase {pgDBAddr = Right (SockAddrUnix "/var/run/postgresql/.s.PGSQL.5432"), pgDBName = "vocabulink", pgDBUser = "vocabulink"})
    pgDisconnect
    (\ db -> do
        member <- loggedIn db
        let ?db = db
            ?static = staticPath
            ?sendmail = sendmail
            ?tokenKey = tokenKey
            ?member = member
            ?referrer = (toS `fmap` requestHeaderReferer req) :: Maybe String
        vocabulinkApp req sendResponse `catches` [Handler (vocabulinkHandler sendResponse), Handler defaultHandler])
 where loggedIn db = do
         let auth = (lookup "auth" . parseCookies) =<< (lookup "Cookie" (requestHeaders req))
         token' <- maybe (return Nothing) (validAuth tokenKey . toS) auth
         case token' of
           Nothing -> return Nothing
           Just token -> do
             row <- $(queryTuple
                      "SELECT username, email FROM member \
                      \WHERE member_no = {authMemberNumber token}") db
             return ((\(username, email) -> Member { memberNumber = authMemberNumber token
                                                   , memberName   = username
                                                   , memberEmail  = email
                                                   }) `fmap` row)
       defaultHandler :: SomeException -> _
       defaultHandler _ = sendResponse $ responseLBS status500 [] "An error occurred."