diff --git a/postgrest.cabal b/postgrest.cabal index 7eb0931ab..cc5eb1b8a 100644 --- a/postgrest.cabal +++ b/postgrest.cabal @@ -145,7 +145,6 @@ Test-Suite spec , PostgREST.Auth , PostgREST.Config , PostgREST.Error - , PostgREST.Main , PostgREST.Middleware , PostgREST.Parsers , PostgREST.DbStructure diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 445e6bcdd..44672e09f 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -3,7 +3,7 @@ {-# LANGUAGE TupleSections #-} --module PostgREST.App where module PostgREST.App ( - app + postgrest ) where import Control.Applicative @@ -18,6 +18,9 @@ import Data.String.Conversions (cs) import Data.Text (Text, replace, strip) import Data.Tree +import qualified Hasql.Pool as P +import qualified Hasql.Transaction as HT + import Text.Parsec.Error import Text.ParserCombinators.Parsec (parse) @@ -26,25 +29,26 @@ import Network.HTTP.Types.Header import Network.HTTP.Types.Status import Network.HTTP.Types.URI (parseSimpleQuery) import Network.Wai +import Network.Wai.Middleware.RequestLogger (logStdout) import Data.Aeson import Data.Aeson.Types (emptyArray) import Data.Monoid +import Data.Time.Clock.POSIX (getPOSIXTime) import qualified Data.Vector as V import qualified Hasql.Transaction as H -import PostgREST.Config (AppConfig (..)) -import PostgREST.Parsers -import PostgREST.DbStructure -import PostgREST.RangeQuery import PostgREST.ApiRequest (ApiRequest(..), ContentType(..) , Action(..), Target(..) , PreferRepresentation (..) , userApiRequest) -import PostgREST.Types import PostgREST.Auth (tokenJWT) -import PostgREST.Error (errResponse) - +import PostgREST.Config (AppConfig (..)) +import PostgREST.DbStructure +import PostgREST.Error (errResponse, pgErrResponse) +import PostgREST.Parsers +import PostgREST.RangeQuery +import PostgREST.Middleware import PostgREST.QueryBuilder ( callProc , addJoinConditions , sourceCTEName @@ -55,9 +59,24 @@ import PostgREST.QueryBuilder ( callProc , createWriteStatement , ResultsWithCount ) +import PostgREST.Types import Prelude + +postgrest :: AppConfig -> DbStructure -> P.Pool -> Application +postgrest conf dbStructure pool = + let middle = (if configQuiet conf then id else logStdout) . defaultMiddle in + + middle $ \ req respond -> do + time <- getPOSIXTime + body <- strictRequestBody req + + let handleReq = runWithClaims conf time (app dbStructure conf body) req + resp <- either pgErrResponse id <$> P.use pool + (HT.run handleReq HT.ReadCommitted HT.Write) + respond resp + app :: DbStructure -> AppConfig -> RequestBody -> Request -> H.Transaction Response app dbStructure conf reqBody req = let diff --git a/src/PostgREST/Main.hs b/src/PostgREST/Main.hs index c3aa95563..f7a09fefc 100644 --- a/src/PostgREST/Main.hs +++ b/src/PostgREST/Main.hs @@ -1,6 +1,6 @@ {-# LANGUAGE CPP #-} -module PostgREST.Main where +module Main where import PostgREST.App @@ -9,23 +9,16 @@ import PostgREST.Config (AppConfig (..), prettyVersion, readOptions) import PostgREST.DbStructure -import PostgREST.Error (pgErrResponse) -import PostgREST.Middleware -import PostgREST.Types (DbStructure) import Control.Monad import Data.Monoid ((<>)) import Data.String.Conversions (cs) -import Data.Time.Clock.POSIX (getPOSIXTime) import qualified Hasql.Query as H import qualified Hasql.Session as H -import qualified Hasql.Transaction as HT import qualified Hasql.Decoders as HD import qualified Hasql.Encoders as HE import qualified Hasql.Pool as P -import Network.Wai import Network.Wai.Handler.Warp -import Network.Wai.Middleware.RequestLogger (logStdout) import System.IO (BufferMode (..), hSetBuffering, stderr, stdin, stdout) @@ -82,16 +75,3 @@ main = do let dbStructure = either (error.show) id result runSettings appSettings $ postgrest conf dbStructure pool - -postgrest :: AppConfig -> DbStructure -> P.Pool -> Application -postgrest conf dbStructure pool = - let middle = (if configQuiet conf then id else logStdout) . defaultMiddle in - - middle $ \ req respond -> do - time <- getPOSIXTime - body <- strictRequestBody req - - let handleReq = runWithClaims conf time (app dbStructure conf body) req - resp <- either pgErrResponse id <$> P.use pool - (HT.run handleReq HT.ReadCommitted HT.Write) - respond resp diff --git a/test/Main.hs b/test/Main.hs index cc52c0fb4..98cdd3c54 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -6,7 +6,7 @@ import SpecHelper import qualified Hasql.Pool as P import PostgREST.DbStructure (getDbStructure) -import PostgREST.Main (postgrest) +import PostgREST.App (postgrest) import Data.String.Conversions (cs) import qualified Feature.AuthSpec