From e571cb9fcb53d361e03c48d916a4f3e4d473b545 Mon Sep 17 00:00:00 2001 From: Jacky Hu Date: Sat, 18 Jun 2016 10:00:44 +0800 Subject: [PATCH] Ability to set addresses to listen on Fixes #461 --- CHANGELOG.md | 1 + main/Main.hs | 7 +++++-- src/PostgREST/Config.hs | 2 +- src/PostgREST/OpenAPI.hs | 10 +++++++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4faca171..0bdb00360 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added - Ability to generate an OpenAPI spec - @mainx07, @hudayou, @ruslantalpa, @begriffs +- Ability to set addresses to listen on - @hudayou ### Fixed - Do not apply limit to parent items - @ruslantalpa diff --git a/main/Main.hs b/main/Main.hs index f0fe92de1..9bac6b36e 100644 --- a/main/Main.hs +++ b/main/Main.hs @@ -13,6 +13,7 @@ import PostgREST.DbStructure import Control.Monad import Data.Monoid ((<>)) import Data.String.Conversions (cs) +import Data.String (IsString (..)) import qualified Hasql.Query as H import qualified Hasql.Session as H import qualified Hasql.Decoders as HD @@ -47,9 +48,11 @@ main = do hSetBuffering stderr NoBuffering conf <- readOptions - let port = configPort conf + let host = configHost conf + port = configPort conf pgSettings = cs (configDatabase conf) - appSettings = setPort port + appSettings = setHost (fromString host) + . setPort port . setServerName (cs $ "postgrest/" <> prettyVersion) $ defaultSettings diff --git a/src/PostgREST/Config.hs b/src/PostgREST/Config.hs index d8d840c41..3835b342a 100644 --- a/src/PostgREST/Config.hs +++ b/src/PostgREST/Config.hs @@ -52,7 +52,7 @@ argParser = AppConfig <$> argument str (help "(REQUIRED) database connection string, e.g. postgres://user:pass@host:port/db" <> metavar "DB_URL") <*> strOption (long "anonymous" <> short 'a' <> help "(REQUIRED) postgres role to use for non-authenticated requests" <> metavar "ROLE") <*> strOption (long "schema" <> short 's' <> help "schema to use for API routes" <> metavar "NAME" <> value "public" <> showDefault) - <*> strOption (long "hostname" <> short 'n' <> help "hostname on which the HTTP server is running" <> metavar "HOSTNAME" <> value "localhost" <> showDefault) + <*> strOption (long "host" <> short 'l' <> help "hostname or ip on which to run HTTP server" <> metavar "HOST" <> value "*4" <> showDefault) <*> option auto (long "port" <> short 'p' <> help "port number on which to run HTTP server" <> metavar "PORT" <> value 3000 <> showDefault) <*> (secret . cs <$> strOption (long "jwt-secret" <> short 'j' <> help "secret used to encrypt and decrypt JWT tokens" <> metavar "SECRET" <> value "secret" <> showDefault)) diff --git a/src/PostgREST/OpenAPI.hs b/src/PostgREST/OpenAPI.hs index 4a03b4c22..dcea58909 100644 --- a/src/PostgREST/OpenAPI.hs +++ b/src/PostgREST/OpenAPI.hs @@ -215,6 +215,14 @@ makeRootPathItem = ("/", p) makePathItems :: [(Table, [Column], [Text])] -> InsOrdHashMap FilePath PathItem makePathItems ti = fromList $ makeRootPathItem : map makePathItem ti +escapeHostName :: String -> String +escapeHostName "*" = "0.0.0.0" +escapeHostName "*4" = "0.0.0.0" +escapeHostName "!4" = "0.0.0.0" +escapeHostName "*6" = "0.0.0.0" +escapeHostName "!6" = "0.0.0.0" +escapeHostName h = h + postgrestSpec:: [(Table, [Column], [Text])] -> String -> Integer -> Swagger postgrestSpec ti h p = (mempty :: Swagger) & basePath ?~ "/" @@ -227,7 +235,7 @@ postgrestSpec ti h p = (mempty :: Swagger) & definitions .~ makeDefinitions ti & paths .~ makePathItems ti where - h' = Just $ Host h (Just (fromInteger p)) + h' = Just $ Host (escapeHostName h) (Just (fromInteger p)) encodeOpenAPI :: [(Table, [Column], [Text])] -> String -> Integer -> ByteString encodeOpenAPI ti h p = encode $ postgrestSpec ti h p