45 lines
1.9 KiB
Haskell
45 lines
1.9 KiB
Haskell
|
module Examples.Chunked.Main where
|
||
|
|
||
|
import Prelude
|
||
|
|
||
|
import Effect.Aff as Aff
|
||
|
import Effect.Class as EffectClass
|
||
|
import Effect.Console as Console
|
||
|
import HTTPure as HTTPure
|
||
|
import Node.ChildProcess as ChildProcess
|
||
|
import Node.Stream as Stream
|
||
|
|
||
|
-- | Serve the example server on this port
|
||
|
port :: Int
|
||
|
port = 8091
|
||
|
|
||
|
-- | Shortcut for `show port`
|
||
|
portS :: String
|
||
|
portS = show port
|
||
|
|
||
|
-- | Run a script and return it's stdout stream
|
||
|
runScript :: String -> Aff.Aff (Stream.Readable ())
|
||
|
runScript script = EffectClass.liftEffect $ ChildProcess.stdout <$>
|
||
|
ChildProcess.spawn "sh" [ "-c", script ] ChildProcess.defaultSpawnOptions
|
||
|
|
||
|
-- | Say 'hello world!' in chunks when run
|
||
|
sayHello :: HTTPure.Request -> HTTPure.ResponseM
|
||
|
sayHello _ =
|
||
|
runScript "echo -n 'hello '; sleep 1; echo -n 'world!'" >>= HTTPure.ok
|
||
|
|
||
|
-- | Boot up the server
|
||
|
main :: HTTPure.ServerM
|
||
|
main = HTTPure.serve port sayHello do
|
||
|
Console.log $ " ┌────────────────────────────────────────────┐"
|
||
|
Console.log $ " │ Server now up on port " <> portS <> " │"
|
||
|
Console.log $ " │ │"
|
||
|
Console.log $ " │ To test, run: │"
|
||
|
Console.log $ " │ > curl -Nv localhost:" <> portS <> " │"
|
||
|
Console.log $ " │ # => ... │"
|
||
|
Console.log $ " │ # => < Transfer-Encoding: chunked │"
|
||
|
Console.log $ " │ # => ... │"
|
||
|
Console.log $ " │ # => hello │"
|
||
|
Console.log $ " │ (1 second pause) │"
|
||
|
Console.log $ " │ # => world! │"
|
||
|
Console.log $ " └────────────────────────────────────────────┘"
|