Skip to content

Commit 1a6ba20

Browse files
mkleczekwolfgangwalther
authored andcommitted
refactor: Simplify App.initServerSocket
This change gets rid of unnecessary explicit bindRandomPortTCP in initServerSocket. Returned port value was ignored in removed code anyway as assigned port retrieval from an open socket is handled elsewhere.
1 parent ae00c04 commit 1a6ba20

1 file changed

Lines changed: 7 additions & 26 deletions

File tree

src/PostgREST/App.hs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ import PostgREST.Version (docsVersion, prettyVersion)
6363

6464
import qualified Data.ByteString.Char8 as BS
6565
import qualified Data.List as L
66-
import Data.Streaming.Network (bindPortTCP,
67-
bindRandomPortTCP)
66+
import Data.Streaming.Network (bindPortTCP)
6867
import qualified Data.Text as T
6968
import qualified Network.HTTP.Types as HTTP
7069
import qualified Network.HTTP.Types.Header as HTTP (hVary)
@@ -262,33 +261,15 @@ type AppSockets = (NS.Socket, Maybe NS.Socket)
262261

263262
initSockets :: AppConfig -> IO AppSockets
264263
initSockets AppConfig{..} = do
265-
let
266-
cfg'usp = configServerUnixSocket
267-
cfg'uspm = configServerUnixSocketMode
268-
cfg'host = configServerHost
269-
cfg'port = configServerPort
270-
cfg'adminHost = configAdminServerHost
271-
cfg'adminPort = configAdminServerPort
272-
273-
sock <- case cfg'usp of
264+
sock <- case configServerUnixSocket of
274265
-- I'm not using `streaming-commons`' bindPath function here because it's not defined for Windows,
275266
-- but we need to have runtime error if we try to use it in Windows, not compile time error
276-
Just path -> createAndBindDomainSocket path cfg'uspm
277-
Nothing -> do
278-
(_, sock) <-
279-
if cfg'port /= 0
280-
then do
281-
sock <- bindPortTCP cfg'port (fromString $ T.unpack cfg'host)
282-
pure (cfg'port, sock)
283-
else do
284-
-- explicitly bind to a random port, returning bound port number
285-
(num, sock) <- bindRandomPortTCP (fromString $ T.unpack cfg'host)
286-
pure (num, sock)
287-
pure sock
288-
289-
adminSock <- case cfg'adminPort of
267+
Just path -> createAndBindDomainSocket path configServerUnixSocketMode
268+
Nothing -> bindPortTCP configServerPort (fromString $ T.unpack configServerHost)
269+
270+
adminSock <- case configAdminServerPort of
290271
Just adminPort -> do
291-
adminSock <- bindPortTCP adminPort (fromString $ T.unpack cfg'adminHost)
272+
adminSock <- bindPortTCP adminPort (fromString $ T.unpack configAdminServerHost)
292273
pure $ Just adminSock
293274
Nothing -> pure Nothing
294275

0 commit comments

Comments
 (0)