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