@@ -37,6 +37,7 @@ executable postgrest
|
|||||||
, case-insensitive
|
, case-insensitive
|
||||||
, scientific, time
|
, scientific, time
|
||||||
, aeson >= 0.8, network >= 2.6
|
, aeson >= 0.8, network >= 2.6
|
||||||
|
, aeson-pretty >= 0.7 && < 0.8
|
||||||
, bytestring, text, split, string-conversions
|
, bytestring, text, split, string-conversions
|
||||||
, stringsearch
|
, stringsearch
|
||||||
, containers, unordered-containers
|
, containers, unordered-containers
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ import PostgREST.Types
|
|||||||
|
|
||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
app :: DbStructure -> AppConfig -> Text -> BL.ByteString -> DbRole -> Request -> H.Tx P.Postgres s Response
|
app :: DbStructure -> AppConfig -> DbRole -> BL.ByteString -> DbRole -> Request -> H.Tx P.Postgres s Response
|
||||||
app dbstructure conf authenticator reqBody dbrole req =
|
app dbstructure conf authenticator reqBody dbrole req =
|
||||||
case (path, verb) of
|
case (path, verb) of
|
||||||
|
|
||||||
|
|||||||
+23
-17
@@ -1,39 +1,41 @@
|
|||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
|
|
||||||
|
import PostgREST.App
|
||||||
|
import PostgREST.Config (AppConfig (..),
|
||||||
|
minimumPgVersion,
|
||||||
|
prettyVersion,
|
||||||
|
readOptions)
|
||||||
|
import PostgREST.Error (errResponse, PgError)
|
||||||
|
import PostgREST.Middleware
|
||||||
import PostgREST.PgStructure
|
import PostgREST.PgStructure
|
||||||
import PostgREST.Types
|
import PostgREST.Types
|
||||||
import Network.Wai
|
|
||||||
|
|
||||||
import PostgREST.App
|
|
||||||
import PostgREST.Error (errResponse)
|
|
||||||
import PostgREST.Middleware
|
|
||||||
|
|
||||||
import Control.Monad (unless)
|
import Control.Monad (unless)
|
||||||
import Control.Monad.IO.Class (liftIO)
|
import Control.Monad.IO.Class (liftIO)
|
||||||
|
import Data.Aeson.Encode.Pretty (encodePretty)
|
||||||
import Data.Functor.Identity
|
import Data.Functor.Identity
|
||||||
import Data.Monoid ((<>))
|
import Data.Monoid ((<>))
|
||||||
import Data.String.Conversions (cs)
|
import Data.String.Conversions (cs)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import qualified Hasql as H
|
import qualified Hasql as H
|
||||||
import qualified Hasql.Postgres as P
|
import qualified Hasql.Postgres as P
|
||||||
|
import Network.Wai
|
||||||
import Network.Wai.Handler.Warp hiding (Connection)
|
import Network.Wai.Handler.Warp hiding (Connection)
|
||||||
import Network.Wai.Middleware.RequestLogger (logStdout)
|
import Network.Wai.Middleware.RequestLogger (logStdout)
|
||||||
|
|
||||||
import System.IO (BufferMode (..),
|
import System.IO (BufferMode (..),
|
||||||
hSetBuffering, stderr,
|
hSetBuffering, stderr,
|
||||||
stdin, stdout)
|
stdin, stdout)
|
||||||
|
|
||||||
import PostgREST.Config (AppConfig (..),
|
|
||||||
prettyVersion,
|
|
||||||
readOptions,
|
|
||||||
minimumPgVersion)
|
|
||||||
|
|
||||||
isServerVersionSupported :: H.Session P.Postgres IO Bool
|
isServerVersionSupported :: H.Session P.Postgres IO Bool
|
||||||
isServerVersionSupported = do
|
isServerVersionSupported = do
|
||||||
Identity (row :: Text) <- H.tx Nothing $ H.singleEx [H.stmt|SHOW server_version_num|]
|
Identity (row :: Text) <- H.tx Nothing $ H.singleEx [H.stmt|SHOW server_version_num|]
|
||||||
return $ read (cs row) >= minimumPgVersion
|
return $ read (cs row) >= minimumPgVersion
|
||||||
|
|
||||||
|
hasqlError :: PgError -> IO a
|
||||||
|
hasqlError = error . cs . encodePretty
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
hSetBuffering stdout LineBuffering
|
hSetBuffering stdout LineBuffering
|
||||||
@@ -61,15 +63,19 @@ main = do
|
|||||||
pool :: H.Pool P.Postgres <- H.acquirePool pgSettings poolSettings
|
pool :: H.Pool P.Postgres <- H.acquirePool pgSettings poolSettings
|
||||||
|
|
||||||
supportedOrError <- H.session pool isServerVersionSupported
|
supportedOrError <- H.session pool isServerVersionSupported
|
||||||
either (fail . show)
|
either hasqlError
|
||||||
(\supported ->
|
(\supported ->
|
||||||
unless supported $
|
unless supported $
|
||||||
fail "Cannot run in this PostgreSQL version, PostgREST needs at least 9.2.0"
|
error (
|
||||||
|
"Cannot run in this PostgreSQL version, PostgREST needs at least "
|
||||||
|
<> show minimumPgVersion)
|
||||||
) supportedOrError
|
) supportedOrError
|
||||||
|
|
||||||
Right authenticator <- H.session pool $ do
|
roleOrError <- H.session pool $ do
|
||||||
Identity (role :: Text) <- H.tx Nothing $ H.singleEx [H.stmt|SELECT SESSION_USER|]
|
Identity (role :: Text) <- H.tx Nothing $ H.singleEx
|
||||||
|
[H.stmt|SELECT SESSION_USER|]
|
||||||
return role
|
return role
|
||||||
|
authenticator <- either hasqlError return roleOrError
|
||||||
|
|
||||||
let txSettings = Just (H.ReadCommitted, Just True)
|
let txSettings = Just (H.ReadCommitted, Just True)
|
||||||
metadata <- H.session pool $ H.tx txSettings $ do
|
metadata <- H.session pool $ H.tx txSettings $ do
|
||||||
@@ -79,15 +85,15 @@ main = do
|
|||||||
keys <- allPrimaryKeys
|
keys <- allPrimaryKeys
|
||||||
return (tabs, rels, cols, keys)
|
return (tabs, rels, cols, keys)
|
||||||
|
|
||||||
dbstructure <- case metadata of
|
dbstructure <- either hasqlError
|
||||||
Left e -> fail $ show e
|
(\(tabs, rels, cols, keys) ->
|
||||||
Right (tabs, rels, cols, keys) ->
|
|
||||||
return DbStructure {
|
return DbStructure {
|
||||||
tables=tabs
|
tables=tabs
|
||||||
, columns=cols
|
, columns=cols
|
||||||
, relations=rels
|
, relations=rels
|
||||||
, primaryKeys=keys
|
, primaryKeys=keys
|
||||||
}
|
}
|
||||||
|
) metadata
|
||||||
|
|
||||||
runSettings appSettings $ middle $ \ req respond -> do
|
runSettings appSettings $ middle $ \ req respond -> do
|
||||||
body <- strictRequestBody req
|
body <- strictRequestBody req
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ import PostgREST.Config (AppConfig (..), corsPolicy)
|
|||||||
|
|
||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
authenticated :: forall s. AppConfig -> Text ->
|
authenticated :: forall s. AppConfig -> DbRole ->
|
||||||
(DbRole -> Request -> H.Tx P.Postgres s Response) ->
|
(DbRole -> Request -> H.Tx P.Postgres s Response) ->
|
||||||
Request -> H.Tx P.Postgres s Response
|
Request -> H.Tx P.Postgres s Response
|
||||||
authenticated conf authenticator app req = do
|
authenticated conf authenticator app req = do
|
||||||
|
|||||||
Reference in New Issue
Block a user