@@ -26,7 +26,8 @@ Download the binary ([OS X](http://bin.begriffs.com/dbapi/osx/postgrest-0.2.6.0.
|
|||||||
postgrest --db-host localhost --db-port 5432 \
|
postgrest --db-host localhost --db-port 5432 \
|
||||||
--db-name my_db --db-user postgres \
|
--db-name my_db --db-user postgres \
|
||||||
--db-pass foobar --db-pool 200 \
|
--db-pass foobar --db-pool 200 \
|
||||||
--anonymous postgres --port 3000
|
--anonymous postgres --port 3000 \
|
||||||
|
--v1schema public
|
||||||
```
|
```
|
||||||
|
|
||||||
In production include the `--secure` option which redirects all
|
In production include the `--secure` option which redirects all
|
||||||
|
|||||||
+7
-7
@@ -35,8 +35,8 @@ import PgQuery
|
|||||||
import RangeQuery
|
import RangeQuery
|
||||||
import PgStructure
|
import PgStructure
|
||||||
|
|
||||||
app :: BL.ByteString -> Request -> H.Tx P.Postgres s Response
|
app :: Text -> BL.ByteString -> Request -> H.Tx P.Postgres s Response
|
||||||
app reqBody req =
|
app v1schema reqBody req =
|
||||||
case (path, verb) of
|
case (path, verb) of
|
||||||
([], _) -> do
|
([], _) -> do
|
||||||
body <- encode <$> tables (cs schema)
|
body <- encode <$> tables (cs schema)
|
||||||
@@ -171,7 +171,7 @@ app reqBody req =
|
|||||||
verb = requestMethod req
|
verb = requestMethod req
|
||||||
qq = queryString req
|
qq = queryString req
|
||||||
hdrs = requestHeaders req
|
hdrs = requestHeaders req
|
||||||
schema = requestedSchema hdrs
|
schema = requestedSchema v1schema hdrs
|
||||||
range = rangeRequested hdrs
|
range = rangeRequested hdrs
|
||||||
allOrigins = ("Access-Control-Allow-Origin", "*") :: Header
|
allOrigins = ("Access-Control-Allow-Origin", "*") :: Header
|
||||||
|
|
||||||
@@ -197,11 +197,11 @@ contentRangeH from to total =
|
|||||||
<> cs (show total)
|
<> cs (show total)
|
||||||
)
|
)
|
||||||
|
|
||||||
requestedSchema :: RequestHeaders -> Text
|
requestedSchema :: Text -> RequestHeaders -> Text
|
||||||
requestedSchema hdrs =
|
requestedSchema v1schema hdrs =
|
||||||
case verStr of
|
case verStr of
|
||||||
Just [[_, ver]] -> ver
|
Just [[_, ver]] -> if ver == "1" then v1schema else ver
|
||||||
_ -> "1"
|
_ -> v1schema
|
||||||
|
|
||||||
where verRegex = "version[ ]*=[ ]*([0-9]+)" :: String
|
where verRegex = "version[ ]*=[ ]*([0-9]+)" :: String
|
||||||
accept = cs <$> lookup hAccept hdrs :: Maybe Text
|
accept = cs <$> lookup hAccept hdrs :: Maybe Text
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ data AppConfig = AppConfig {
|
|||||||
, configAnonRole :: String
|
, configAnonRole :: String
|
||||||
, configSecure :: Bool
|
, configSecure :: Bool
|
||||||
, configPool :: Int
|
, configPool :: Int
|
||||||
|
, configV1Schema :: String
|
||||||
}
|
}
|
||||||
|
|
||||||
argParser :: Parser AppConfig
|
argParser :: Parser AppConfig
|
||||||
@@ -34,6 +35,7 @@ argParser = AppConfig
|
|||||||
<*> strOption (long "anonymous" <> short 'a' <> metavar "ROLE" <> help "postgres role to use for non-authenticated requests")
|
<*> strOption (long "anonymous" <> short 'a' <> metavar "ROLE" <> help "postgres role to use for non-authenticated requests")
|
||||||
<*> switch (long "secure" <> short 's' <> help "Redirect all requests to HTTPS")
|
<*> switch (long "secure" <> short 's' <> help "Redirect all requests to HTTPS")
|
||||||
<*> option auto (long "db-pool" <> metavar "COUNT" <> value 10 <> help "Max connections in database pool" <> showDefault)
|
<*> option auto (long "db-pool" <> metavar "COUNT" <> value 10 <> help "Max connections in database pool" <> showDefault)
|
||||||
|
<*> strOption (long "v1schema" <> metavar "NAME" <> value "1" <> help "Schema to use for nonspecified version (or explicit v1)" <> showDefault)
|
||||||
|
|
||||||
defaultCorsPolicy :: CorsResourcePolicy
|
defaultCorsPolicy :: CorsResourcePolicy
|
||||||
defaultCorsPolicy = CorsResourcePolicy Nothing
|
defaultCorsPolicy = CorsResourcePolicy Nothing
|
||||||
|
|||||||
+1
-1
@@ -63,7 +63,7 @@ main = do
|
|||||||
runSettings appSettings $ middle $ \req respond -> do
|
runSettings appSettings $ middle $ \req respond -> do
|
||||||
body <- strictRequestBody req
|
body <- strictRequestBody req
|
||||||
resOrError <- liftIO $ H.session pool $ H.tx Nothing $
|
resOrError <- liftIO $ H.session pool $ H.tx Nothing $
|
||||||
authenticated currRole anonRole (app body) req
|
authenticated currRole anonRole (app (cs $ configV1Schema conf) body) req
|
||||||
either (respond . errResponse) respond resOrError
|
either (respond . errResponse) respond resOrError
|
||||||
|
|
||||||
where
|
where
|
||||||
|
|||||||
+2
-2
@@ -35,7 +35,7 @@ isLeft (Left _ ) = True
|
|||||||
isLeft _ = False
|
isLeft _ = False
|
||||||
|
|
||||||
cfg :: AppConfig
|
cfg :: AppConfig
|
||||||
cfg = AppConfig "postgrest_test" 5432 "postgrest_test" "" "localhost" 3000 "postgrest_anonymous" False 10
|
cfg = AppConfig "postgrest_test" 5432 "postgrest_test" "" "localhost" 3000 "postgrest_anonymous" False 10 "1"
|
||||||
|
|
||||||
testPoolOpts :: PoolSettings
|
testPoolOpts :: PoolSettings
|
||||||
testPoolOpts = fromMaybe (error "bad settings") $ H.poolSettings 1 30
|
testPoolOpts = fromMaybe (error "bad settings") $ H.poolSettings 1 30
|
||||||
@@ -57,7 +57,7 @@ withApp perform = do
|
|||||||
perform $ middle $ \req resp -> do
|
perform $ middle $ \req resp -> do
|
||||||
body <- strictRequestBody req
|
body <- strictRequestBody req
|
||||||
result <- liftIO $ H.session pool $ H.tx Nothing
|
result <- liftIO $ H.session pool $ H.tx Nothing
|
||||||
$ authenticated currRole anonRole (app body) req
|
$ authenticated currRole anonRole (app (cs $ configV1Schema cfg) body) req
|
||||||
either (resp . errResponse) resp result
|
either (resp . errResponse) resp result
|
||||||
|
|
||||||
where middle = cors corsPolicy
|
where middle = cors corsPolicy
|
||||||
|
|||||||
Reference in New Issue
Block a user