Listen on specific host using warp

独自空忆成欢 提交于 2019-12-08 20:26:03

问题


When running a warp application using run, it listens on all IP adresses.

For security reasons, I want to listen on localhost only, handling remote access using a reverse proxy.

How do I need to call run to only listen on a specific host/IP?

Note: This question intentionally shows no research effort as it was answered Q&A-Style.


回答1:


The currently accepted answer got broken by changes to conduit and/or warp. Warp no longer exports a Host constructor. But you don't need it, because the HostPreference type supports the OverloadedStrings extension, so you can just use a string directly.

This example also eliminates the deprecation warnings by switching to setPort and setHost.

{-# LANGUAGE OverloadedStrings #-}

import Network.Wai (responseLBS)
import Network.Wai.Handler.Warp
import Network.HTTP.Types (status200)
import Network.HTTP.Types.Header (hContentType)

main = do
    let port = 3000
    putStrLn $ "Listening on port " ++ show port
    let settings = setPort port $ setHost "127.0.0.1" defaultSettings
    runSettings settings app

app req = return $
    responseLBS status200 [(hContentType, "text/plain")] "Hello world!"



回答2:


run itself can't do that. you need to use runSettings:

The Settings parameter you want to pass it contains the information about the hosts it listens on.

The relevant type here is HostPreference. Although it allows you to specify different wildcards, including IPv6Only, we'll use the Host constructor here.

Based on this answer, I'll show a minimal example to listen on localhost only.

Note that accessors like settingsHost are marked deprecated, but the official documentation for warp 2.1.2.1 still shows an example using settingsTimeout here.

{-# LANGUAGE OverloadedStrings #-}

import Network.Wai (responseLBS)
import Network.Wai.Handler.Warp
import Network.HTTP.Types (status200)
import Network.HTTP.Types.Header (hContentType)

main = do
    let port = 3000
    putStrLn $ "Listening on port " ++ show port
    let settings = defaultSettings { settingsPort = port,
                                     settingsHost = Host "127.0.0.1" }
    runSettings settings app

app req = return $
    responseLBS status200 [(hContentType, "text/plain")] "Hello world!" 


来源:https://stackoverflow.com/questions/22620622/listen-on-specific-host-using-warp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!