2018-08-30 22:01:49 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
-- | Run a script and return it's stdout stream
|
|
|
|
runScript :: String -> Aff.Aff (Stream.Readable ())
|
2021-03-22 19:02:36 +00:00
|
|
|
runScript script =
|
|
|
|
EffectClass.liftEffect $ ChildProcess.stdout
|
|
|
|
<$> ChildProcess.spawn "sh" [ "-c", script ] ChildProcess.defaultSpawnOptions
|
2018-08-30 22:01:49 +00:00
|
|
|
|
|
|
|
-- | Say 'hello world!' in chunks when run
|
|
|
|
sayHello :: HTTPure.Request -> HTTPure.ResponseM
|
2021-03-22 19:02:36 +00:00
|
|
|
sayHello = const $ runScript "echo 'hello '; sleep 1; echo 'world!'" >>= HTTPure.ok
|
2018-08-30 22:01:49 +00:00
|
|
|
|
|
|
|
-- | Boot up the server
|
|
|
|
main :: HTTPure.ServerM
|
2021-03-22 19:02:36 +00:00
|
|
|
main =
|
|
|
|
HTTPure.serve 8080 sayHello do
|
|
|
|
Console.log $ " ┌──────────────────────────────────────┐"
|
|
|
|
Console.log $ " │ Server now up on port 8080 │"
|
|
|
|
Console.log $ " │ │"
|
|
|
|
Console.log $ " │ To test, run: │"
|
|
|
|
Console.log $ " │ > curl -Nv localhost:8080 │"
|
|
|
|
Console.log $ " │ # => ... │"
|
|
|
|
Console.log $ " │ # => < Transfer-Encoding: chunked │"
|
|
|
|
Console.log $ " │ # => ... │"
|
|
|
|
Console.log $ " │ # => hello │"
|
|
|
|
Console.log $ " │ (1 second pause) │"
|
|
|
|
Console.log $ " │ # => world! │"
|
|
|
|
Console.log $ " └──────────────────────────────────────┘"
|