aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Server.hs24
1 files changed, 17 insertions, 7 deletions
diff --git a/src/Server.hs b/src/Server.hs
index 14d7579..a61bd8e 100644
--- a/src/Server.hs
+++ b/src/Server.hs
@@ -9,21 +9,28 @@ import Data.Pool (createPool, destroyAllResources)
import Database.MySQL.Base (ConnectInfo)
import qualified Database.MySQL.Simple as MySQL
import Network.Socket
- ( Family(AF_INET, AF_UNIX)
- , SockAddr(SockAddrInet, SockAddrUnix)
+ ( AddrInfoFlag(AI_NUMERICSERV)
+ , Family(AF_UNIX)
+ , SockAddr(SockAddrUnix)
, Socket
, SocketOption(ReuseAddr)
, SocketType(Stream)
+ , addrAddress
+ , addrFamily
+ , addrFlags
+ , addrProtocol
+ , addrSocketType
, bind
, close
+ , defaultHints
+ , getAddrInfo
, getSocketName
- , inet_addr
, listen
, maxListenQueue
, setSocketOption
, socket
)
-import Network.Wai.Handler.Warp (Port, defaultSettings, runSettingsSocket)
+import Network.Wai.Handler.Warp (defaultSettings, runSettingsSocket)
import System.IO (hPutStrLn, stderr)
import System.IO.Error (isDoesNotExistError)
import System.Posix.Files
@@ -73,12 +80,15 @@ createSocket (Socket path) = do
hPutStrLn stderr $ "Listening on UNIX socket `" ++ path ++ "'"
return sock
createSocket (Port port) = do
- sock <- socket AF_INET Stream 0
+ addr:_ <- getAddrInfo (Just hints) (Just "localhost") (Just svc)
+ sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
setSocketOption sock ReuseAddr 1
- addr <- inet_addr "127.0.0.1"
- bind sock $ SockAddrInet (fromIntegral port) addr
+ bind sock $ addrAddress addr
hPutStrLn stderr $ "Listening on localhost:" ++ show port
return sock
+ where
+ svc = show port
+ hints = defaultHints {addrFlags = [AI_NUMERICSERV], addrSocketType = Stream}
closeSocket :: Socket -> IO ()
closeSocket sock = do