logMiddleware :: Wai.Middleware
logMiddleware app req respond = app req respond'
  where
    debug f = hPutStrLn stderr $ "[Info#auth-service]" ++ f
    respond' res = do
      unless ( Wai.requestMethod req == "GET"
             && Wai.pathInfo req == ["status"]
             && isLocal (Wai.remoteHost req)
             ) $ debug $ concat
        [ " "
        , fromBS (Wai.requestMethod req) , " "
        , fromBS (Wai.rawPathInfo req) , " > "
        , show . HTTP.statusCode $ Wai.responseStatus res
        , " "
        ]
      respond res
    fromBS = Text.unpack . Text.decodeUtf8With Text.lenientDecode
    isLocal Net.SockAddrUnix{} = True
    isLocal (Net.SockAddrInet _port haddr) =
      Net.hostAddressToTuple haddr == (127,0,0,1)
    isLocal (Net.SockAddrInet6 _port _flow haddr6 _scope) =
      haddr6 == (0, 0, 0, 1)
logMiddleware :: Wai.Middleware
logMiddleware app req respond = app req respond'
  where
    debug f = hPutStrLn stderr $ "[Info#auth-service]" ++ f
    respond' res = do
      unless ( Wai.requestMethod req == "GET"
             && Wai.pathInfo req == ["status"]
             && isLocal (Wai.remoteHost req)
             ) $ debug $ concat
        [ " "
        , fromBS (Wai.requestMethod req) , " "
        , fromBS (Wai.rawPathInfo req) , " > "
        , show . HTTP.statusCode $ Wai.responseStatus res
        , " "
        ]
      respond res
    fromBS = Text.unpack . Text.decodeUtf8With Text.lenientDecode
    isLocal Net.SockAddrUnix{} = True
    isLocal (Net.SockAddrInet _port haddr) =
      Net.hostAddressToTuple haddr == (127,0,0,1)
    isLocal (Net.SockAddrInet6 _port _flow haddr6 _scope) =
      haddr6 == (0, 0, 0, 1)
logMiddleware app req respond = app req respond'
  where
    debug f = hPutStrLn stderr $ "[Info#auth-service]" ++ f
    respond' res = do
      unless ( Wai.requestMethod req == "GET"
             && Wai.pathInfo req == ["status"]
             && isLocal (Wai.remoteHost req)
             ) $ debug $ concat
        [ " "
        , fromBS (Wai.requestMethod req) , " "
        , fromBS (Wai.rawPathInfo req) , " > "
        , show . HTTP.statusCode $ Wai.responseStatus res
        , " "
        ]
      respond res
    fromBS = Text.unpack . Text.decodeUtf8With Text.lenientDecode
    isLocal Net.SockAddrUnix{} = True
    isLocal (Net.SockAddrInet _port haddr) =
      Net.hostAddressToTuple haddr == (127,0,0,1)
    isLocal (Net.SockAddrInet6 _port _flow haddr6 _scope) =
      haddr6 == (0, 0, 0, 1)