Upgrade hasql to 0.4.0
This commit is contained in:
+2
-2
@@ -16,7 +16,7 @@ executable dbapi
|
|||||||
default-extensions: OverloadedStrings
|
default-extensions: OverloadedStrings
|
||||||
other-extensions: QuasiQuotes
|
other-extensions: QuasiQuotes
|
||||||
build-depends: base >=4.6 && <5
|
build-depends: base >=4.6 && <5
|
||||||
, hasql >= 0.2.3 && < 0.3.0, hasql-backend, hasql-postgres
|
, hasql >= 0.4.0, hasql-backend, hasql-postgres
|
||||||
, warp >= 3.0.2, wai >= 3.0.1
|
, warp >= 3.0.2, wai >= 3.0.1
|
||||||
, wai-extra, wai-cors
|
, wai-extra, wai-cors
|
||||||
, wai-middleware-static >= 0.6.0
|
, wai-middleware-static >= 0.6.0
|
||||||
@@ -58,7 +58,7 @@ Test-Suite spec
|
|||||||
Other-Modules: App, Auth, Config, Spec, SpecHelper
|
Other-Modules: App, Auth, Config, Spec, SpecHelper
|
||||||
Build-Depends: base, hspec >= 2.0, QuickCheck
|
Build-Depends: base, hspec >= 2.0, QuickCheck
|
||||||
, hspec-wai >= 0.5.0, hspec-wai-json
|
, hspec-wai >= 0.5.0, hspec-wai-json
|
||||||
, hasql >= 0.2.3 && < 0.3.0, hasql-backend, hasql-postgres
|
, hasql >= 0.4.0, hasql-backend, hasql-postgres
|
||||||
, warp >= 3.0.2, wai >= 3.0.1
|
, warp >= 3.0.2, wai >= 3.0.1
|
||||||
, HTTP, convertible
|
, HTTP, convertible
|
||||||
, case-insensitive
|
, case-insensitive
|
||||||
|
|||||||
+5
-6
@@ -29,7 +29,6 @@ import Data.Aeson
|
|||||||
import Data.Coerce
|
import Data.Coerce
|
||||||
import Data.Monoid
|
import Data.Monoid
|
||||||
import qualified Hasql as H
|
import qualified Hasql as H
|
||||||
import qualified Hasql.Backend as HB
|
|
||||||
import qualified Hasql.Postgres as H
|
import qualified Hasql.Postgres as H
|
||||||
|
|
||||||
import PgQuery
|
import PgQuery
|
||||||
@@ -37,7 +36,7 @@ import RangeQuery
|
|||||||
import PgStructure
|
import PgStructure
|
||||||
import Auth
|
import Auth
|
||||||
|
|
||||||
app :: Request -> H.Session H.Postgres IO Response
|
app :: Request -> H.Session H.Postgres s IO Response
|
||||||
app req =
|
app req =
|
||||||
case (path, verb) of
|
case (path, verb) of
|
||||||
([], _) -> do
|
([], _) -> do
|
||||||
@@ -169,12 +168,12 @@ app req =
|
|||||||
allOrigins = ("Access-Control-Allow-Origin", "*") :: Header
|
allOrigins = ("Access-Control-Allow-Origin", "*") :: Header
|
||||||
|
|
||||||
|
|
||||||
isSqlError :: HB.Error -> Maybe HB.Error
|
isSqlError :: H.Error -> Maybe H.Error
|
||||||
isSqlError (HB.ErroneousResult x) = Just $ HB.ErroneousResult x
|
isSqlError (H.ErroneousResult x) = Just $ H.ErroneousResult x
|
||||||
isSqlError _ = Nothing
|
isSqlError _ = Nothing
|
||||||
|
|
||||||
sqlErrHandler :: HB.Error -> IO Response
|
sqlErrHandler :: H.Error -> IO Response
|
||||||
sqlErrHandler (HB.ErroneousResult err) =
|
sqlErrHandler (H.ErroneousResult err) =
|
||||||
return $ if "42P01" `isInfixOf` err
|
return $ if "42P01" `isInfixOf` err
|
||||||
then responseLBS status404 [] ""
|
then responseLBS status404 [] ""
|
||||||
else responseLBS status400 [] (cs err)
|
else responseLBS status400 [] (cs err)
|
||||||
|
|||||||
+1
-1
@@ -50,7 +50,7 @@ setRole role = H.unit ("set role " <> cs (pgFmtLit role), [], True)
|
|||||||
resetRole :: H.Tx H.Postgres s ()
|
resetRole :: H.Tx H.Postgres s ()
|
||||||
resetRole = H.unit [H.q|reset role|]
|
resetRole = H.unit [H.q|reset role|]
|
||||||
|
|
||||||
addUser :: Text -> Text -> Text -> H.Session H.Postgres IO ()
|
addUser :: Text -> Text -> Text -> H.Session H.Postgres s IO ()
|
||||||
addUser identity pass role = do
|
addUser identity pass role = do
|
||||||
Just hashed <- liftIO $ hashPasswordUsingPolicy fastBcryptHashingPolicy (cs pass)
|
Just hashed <- liftIO $ hashPasswordUsingPolicy fastBcryptHashingPolicy (cs pass)
|
||||||
H.tx Nothing $ H.unit $
|
H.tx Nothing $ H.unit $
|
||||||
|
|||||||
+2
-5
@@ -7,7 +7,6 @@ import Middleware
|
|||||||
|
|
||||||
import Control.Monad (unless)
|
import Control.Monad (unless)
|
||||||
import Control.Monad.IO.Class (liftIO)
|
import Control.Monad.IO.Class (liftIO)
|
||||||
import Control.Monad.Reader (runReaderT, ask)
|
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
import Data.String.Conversions (cs)
|
import Data.String.Conversions (cs)
|
||||||
import Data.List.Split (splitOn)
|
import Data.List.Split (splitOn)
|
||||||
@@ -51,12 +50,10 @@ main = do
|
|||||||
. gzip def . cors corsPolicy . clientErrors
|
. gzip def . cors corsPolicy . clientErrors
|
||||||
. staticPolicy (only [("favicon.ico", "static/favicon.ico")])
|
. staticPolicy (only [("favicon.ico", "static/favicon.ico")])
|
||||||
|
|
||||||
H.session pgSettings sessSettings $ do
|
H.session pgSettings sessSettings $ H.sessionUnlifter >>= \unlift ->
|
||||||
session' <- flip runReaderT <$> ask
|
|
||||||
|
|
||||||
liftIO $ runSettings appSettings $ middle $ \req respond ->
|
liftIO $ runSettings appSettings $ middle $ \req respond ->
|
||||||
respond =<< catchJust isSqlError
|
respond =<< catchJust isSqlError
|
||||||
(session' $ authenticated (cs $ configAnonRole conf) app req)
|
(unlift $ authenticated (cs $ configAnonRole conf) app req)
|
||||||
sqlErrHandler
|
sqlErrHandler
|
||||||
|
|
||||||
where
|
where
|
||||||
|
|||||||
+3
-4
@@ -38,8 +38,8 @@ import Debug.Trace
|
|||||||
-- else Database.PostgreSQL.Simple.withSavepoint conn go
|
-- else Database.PostgreSQL.Simple.withSavepoint conn go
|
||||||
-- where go = app conn req respond
|
-- where go = app conn req respond
|
||||||
|
|
||||||
authenticated :: Text -> (Request -> H.Session H.Postgres IO Response) ->
|
authenticated :: Text -> (Request -> H.Session H.Postgres s IO Response) ->
|
||||||
Request -> H.Session H.Postgres IO Response
|
Request -> H.Session H.Postgres s IO Response
|
||||||
authenticated anon app req = do
|
authenticated anon app req = do
|
||||||
attempt <- httpRequesterRole (requestHeaders req)
|
attempt <- httpRequesterRole (requestHeaders req)
|
||||||
case attempt of
|
case attempt of
|
||||||
@@ -51,7 +51,7 @@ authenticated anon app req = do
|
|||||||
NoCredentials -> runInRole anon
|
NoCredentials -> runInRole anon
|
||||||
|
|
||||||
where
|
where
|
||||||
httpRequesterRole :: RequestHeaders -> H.Session H.Postgres IO LoginAttempt
|
httpRequesterRole :: RequestHeaders -> H.Session H.Postgres s IO LoginAttempt
|
||||||
httpRequesterRole hdrs = do
|
httpRequesterRole hdrs = do
|
||||||
let auth = fromMaybe "" $ lookup hAuthorization hdrs
|
let auth = fromMaybe "" $ lookup hAuthorization hdrs
|
||||||
case split (==' ') (cs auth) of
|
case split (==' ') (cs auth) of
|
||||||
@@ -61,7 +61,6 @@ authenticated anon app req = do
|
|||||||
_ -> return MalformedAuth
|
_ -> return MalformedAuth
|
||||||
_ -> return NoCredentials
|
_ -> return NoCredentials
|
||||||
|
|
||||||
runInRole :: Text -> H.Session H.Postgres IO Response
|
|
||||||
runInRole r = do
|
runInRole r = do
|
||||||
H.tx Nothing $ setRole r
|
H.tx Nothing $ setRole r
|
||||||
resp <- app req
|
resp <- app req
|
||||||
|
|||||||
+2
-5
@@ -11,9 +11,7 @@ import Hasql.Postgres as H
|
|||||||
|
|
||||||
import Data.String.Conversions (cs)
|
import Data.String.Conversions (cs)
|
||||||
-- import Control.Exception.Base (bracket, finally)
|
-- import Control.Exception.Base (bracket, finally)
|
||||||
import Control.Monad.Reader (runReaderT, ask)
|
|
||||||
import Control.Monad (void)
|
import Control.Monad (void)
|
||||||
import Control.Applicative ( (<$>) )
|
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
|
|
||||||
import Network.HTTP.Types.Header (Header, ByteRange, renderByteRange,
|
import Network.HTTP.Types.Header (Header, ByteRange, renderByteRange,
|
||||||
@@ -47,10 +45,9 @@ pgSettings = H.Postgres "localhost" 5432 "dbapi_test" "" "dbapi_test"
|
|||||||
withApp :: ActionWith Application -> IO ()
|
withApp :: ActionWith Application -> IO ()
|
||||||
withApp perform =
|
withApp perform =
|
||||||
perform $ middle $ \req resp ->
|
perform $ middle $ \req resp ->
|
||||||
H.session pgSettings testSettings $ do
|
H.session pgSettings testSettings $ H.sessionUnlifter >>= \unlift ->
|
||||||
session' <- flip runReaderT <$> ask
|
|
||||||
liftIO $ resp =<< catchJust isSqlError
|
liftIO $ resp =<< catchJust isSqlError
|
||||||
(session' $ authenticated (cs $ configAnonRole cfg) app req)
|
(unlift $ authenticated (cs $ configAnonRole cfg) app req)
|
||||||
sqlErrHandler
|
sqlErrHandler
|
||||||
|
|
||||||
where middle = cors corsPolicy
|
where middle = cors corsPolicy
|
||||||
|
|||||||
Reference in New Issue
Block a user