Moves all config related code to PostgREST.Config module and
tweak the code to better encapsulate functionality.
This commit is contained in:
@@ -94,6 +94,7 @@ library
|
|||||||
, errors
|
, errors
|
||||||
, bifunctors
|
, bifunctors
|
||||||
|
|
||||||
|
Other-Modules: Paths_postgrest
|
||||||
Exposed-Modules: PostgREST.App
|
Exposed-Modules: PostgREST.App
|
||||||
, PostgREST.Types
|
, PostgREST.Types
|
||||||
, PostgREST.Parsers
|
, PostgREST.Parsers
|
||||||
@@ -130,6 +131,7 @@ Test-Suite spec
|
|||||||
, PostgREST.RangeQuery
|
, PostgREST.RangeQuery
|
||||||
, Spec
|
, Spec
|
||||||
, SpecHelper
|
, SpecHelper
|
||||||
|
, Paths_postgrest
|
||||||
Build-Depends: base, hspec == 2.1.*, QuickCheck
|
Build-Depends: base, hspec == 2.1.*, QuickCheck
|
||||||
, hspec-wai, hspec-wai-json
|
, hspec-wai, hspec-wai-json
|
||||||
, hasql, hasql-backend
|
, hasql, hasql-backend
|
||||||
|
|||||||
+24
-1
@@ -1,14 +1,22 @@
|
|||||||
module PostgREST.Config where
|
module PostgREST.Config ( prettyVersion
|
||||||
|
, readOptions
|
||||||
|
, corsPolicy
|
||||||
|
, AppConfig (..)
|
||||||
|
)
|
||||||
|
where
|
||||||
|
|
||||||
|
|
||||||
import Control.Applicative
|
import Control.Applicative
|
||||||
import qualified Data.ByteString.Char8 as BS
|
import qualified Data.ByteString.Char8 as BS
|
||||||
import qualified Data.CaseInsensitive as CI
|
import qualified Data.CaseInsensitive as CI
|
||||||
|
import Data.List (intercalate)
|
||||||
import Data.String.Conversions (cs)
|
import Data.String.Conversions (cs)
|
||||||
import Data.Text (strip)
|
import Data.Text (strip)
|
||||||
|
import Data.Version (versionBranch)
|
||||||
import Network.Wai
|
import Network.Wai
|
||||||
import Network.Wai.Middleware.Cors (CorsResourcePolicy (..))
|
import Network.Wai.Middleware.Cors (CorsResourcePolicy (..))
|
||||||
import Options.Applicative hiding (columns)
|
import Options.Applicative hiding (columns)
|
||||||
|
import Paths_postgrest (version)
|
||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
data AppConfig = AppConfig {
|
data AppConfig = AppConfig {
|
||||||
@@ -63,3 +71,18 @@ corsPolicy req = case lookup "origin" headers of
|
|||||||
accHeaders = case lookup "access-control-request-headers" headers of
|
accHeaders = case lookup "access-control-request-headers" headers of
|
||||||
Just hdrs -> map (CI.mk . cs . strip . cs) $ BS.split ',' hdrs
|
Just hdrs -> map (CI.mk . cs . strip . cs) $ BS.split ',' hdrs
|
||||||
Nothing -> []
|
Nothing -> []
|
||||||
|
|
||||||
|
prettyVersion :: String
|
||||||
|
prettyVersion = intercalate "." $ map show $ versionBranch version
|
||||||
|
|
||||||
|
readOptions :: IO AppConfig
|
||||||
|
readOptions = customExecParser parserPrefs opts
|
||||||
|
where
|
||||||
|
opts = info (helper <*> argParser) $
|
||||||
|
fullDesc
|
||||||
|
<> progDesc (
|
||||||
|
"PostgREST "
|
||||||
|
<> prettyVersion
|
||||||
|
<> " / create a REST API to an existing Postgres database"
|
||||||
|
)
|
||||||
|
parserPrefs = prefs showHelpOnError
|
||||||
|
|||||||
+5
-18
@@ -1,7 +1,6 @@
|
|||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
|
|
||||||
import Paths_postgrest (version)
|
|
||||||
import PostgREST.PgStructure
|
import PostgREST.PgStructure
|
||||||
import PostgREST.Types
|
import PostgREST.Types
|
||||||
import Network.Wai
|
import Network.Wai
|
||||||
@@ -13,22 +12,21 @@ 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.Functor.Identity
|
import Data.Functor.Identity
|
||||||
import Data.List (intercalate)
|
import Data.String.Conversions (cs,
|
||||||
import Data.String.Conversions (cs)
|
(<>))
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import Data.Version (versionBranch)
|
|
||||||
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.Handler.Warp hiding (Connection)
|
import Network.Wai.Handler.Warp hiding (Connection)
|
||||||
import Network.Wai.Middleware.RequestLogger (logStdout)
|
import Network.Wai.Middleware.RequestLogger (logStdout)
|
||||||
import Options.Applicative hiding (columns)
|
|
||||||
|
|
||||||
import System.IO (BufferMode (..),
|
import System.IO (BufferMode (..),
|
||||||
hSetBuffering, stderr,
|
hSetBuffering, stderr,
|
||||||
stdin, stdout)
|
stdin, stdout)
|
||||||
|
|
||||||
import PostgREST.Config (AppConfig (..),
|
import PostgREST.Config (AppConfig (..),
|
||||||
argParser)
|
prettyVersion,
|
||||||
|
readOptions)
|
||||||
|
|
||||||
isServerVersionSupported :: H.Session P.Postgres IO Bool
|
isServerVersionSupported :: H.Session P.Postgres IO Bool
|
||||||
isServerVersionSupported = do
|
isServerVersionSupported = do
|
||||||
@@ -41,15 +39,7 @@ main = do
|
|||||||
hSetBuffering stdin LineBuffering
|
hSetBuffering stdin LineBuffering
|
||||||
hSetBuffering stderr NoBuffering
|
hSetBuffering stderr NoBuffering
|
||||||
|
|
||||||
let opts = info (helper <*> argParser) $
|
conf <- readOptions
|
||||||
fullDesc
|
|
||||||
<> progDesc (
|
|
||||||
"PostgREST "
|
|
||||||
<> prettyVersion
|
|
||||||
<> " / create a REST API to an existing Postgres database"
|
|
||||||
)
|
|
||||||
parserPrefs = prefs showHelpOnError
|
|
||||||
conf <- customExecParser parserPrefs opts
|
|
||||||
let port = configPort conf
|
let port = configPort conf
|
||||||
|
|
||||||
unless (configSecure conf) $
|
unless (configSecure conf) $
|
||||||
@@ -104,6 +94,3 @@ main = do
|
|||||||
resOrError <- liftIO $ H.session pool $ H.tx txSettings $
|
resOrError <- liftIO $ H.session pool $ H.tx txSettings $
|
||||||
authenticated conf (app dbstructure conf body) req
|
authenticated conf (app dbstructure conf body) req
|
||||||
either (respond . errResponse) respond resOrError
|
either (respond . errResponse) respond resOrError
|
||||||
|
|
||||||
where
|
|
||||||
prettyVersion = intercalate "." $ map show $ versionBranch version
|
|
||||||
|
|||||||
Reference in New Issue
Block a user