refactor is in progress
This commit is contained in:
parent
04300a161e
commit
61ec499c0e
3 changed files with 69 additions and 12 deletions
|
@ -3,17 +3,22 @@
|
|||
module Backend.Http
|
||||
( HttpConfiguration (..),
|
||||
httpDefaultSettings,
|
||||
httpDefaultSetting,
|
||||
forkHttpBackend,
|
||||
httpConfigCodec,
|
||||
httpConfigsCodec,
|
||||
)
|
||||
where
|
||||
|
||||
import Data.String
|
||||
import qualified Data.Text as T
|
||||
import Network.Wai.Handler.Warp as HTTP
|
||||
import Toml (TomlCodec, (.=))
|
||||
import Config
|
||||
import Control.Concurrent
|
||||
import Control.Concurrent.MVar
|
||||
import Data.String
|
||||
import qualified Data.Text as T
|
||||
import qualified Network.Wai as Wai
|
||||
import Network.Wai.Handler.Warp as HTTP
|
||||
import Toml (TomlCodec, (.=))
|
||||
import qualified Toml
|
||||
import Validation
|
||||
|
||||
-- We leave it as a Text here and defer the conversion to the warp type to the last possible moment.
|
||||
-- At least we can touch and print a String. Excepet that we don't use it so we don't need it in another
|
||||
|
@ -31,12 +36,16 @@ bindPreference :: Toml.Key -> TomlCodec BindPreference
|
|||
bindPreference = Toml.match _BindPreference
|
||||
|
||||
data HttpConfiguration = HttpConfiguration
|
||||
{ port :: Int,
|
||||
{ instanceName :: T.Text,
|
||||
port :: Int,
|
||||
bindPref :: BindPreference
|
||||
}
|
||||
|
||||
httpDefaultSettings :: HttpConfiguration
|
||||
httpDefaultSettings = HttpConfiguration 80 "*"
|
||||
httpDefaultSetting :: HttpConfiguration
|
||||
httpDefaultSetting = HttpConfiguration "http" 80 "*"
|
||||
|
||||
httpDefaultSettings :: [HttpConfiguration]
|
||||
httpDefaultSettings = [httpDefaultSetting]
|
||||
|
||||
-- why a String? Why is the only way to generate a HostPreference by its IsString instance?
|
||||
-- why does warp not expose its constructors??
|
||||
|
@ -46,9 +55,18 @@ httpConfigConstructor port bind = HTTP.setPort port $ HTTP.setHost (fromString b
|
|||
httpConfigCodec :: TomlCodec HttpConfiguration
|
||||
httpConfigCodec =
|
||||
HttpConfiguration
|
||||
<$> Toml.int "Port" .= port
|
||||
<$> Toml.text "InstanceName" .= instanceName
|
||||
<*> Toml.int "Port" .= port
|
||||
<*> bindPreference "Bind" .= bindPref
|
||||
|
||||
forkHttpBackend :: Toml.TOML -> IO [MVar ()]
|
||||
forkHttpBackend ast = undefined
|
||||
httpConfigsCodec :: TomlCodec [HttpConfiguration]
|
||||
httpConfigsCodec = Toml.list httpConfigCodec "http"
|
||||
|
||||
forkHttpBackend :: Wai.Application -> FilePath -> IO [MVar ()]
|
||||
forkHttpBackend app configFile = forkWithConfigs (backend app) httpConfigsCodec configFile
|
||||
|
||||
httpToWarpConfig :: HttpConfiguration -> HTTP.Settings
|
||||
httpToWarpConfig config = HTTP.setPort (port config) $ HTTP.setHost (fromString $ bindPref config) $ HTTP.defaultSettings
|
||||
|
||||
backend :: Wai.Application -> HttpConfiguration -> IO ()
|
||||
backend app config = HTTP.runSettings (httpToWarpConfig config) $ app
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue