Skip to content

Instantly share code, notes, and snippets.

@joneshf
Created April 16, 2018 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joneshf/0b4440b5c627d1fc541dacc429099ed5 to your computer and use it in GitHub Desktop.
Save joneshf/0b4440b5c627d1fc541dacc429099ed5 to your computer and use it in GitHub Desktop.
module Main where
import Prelude
import Control.Monad.Eff
import Control.Monad.Eff.Unsafe
import Data.Function.Uncurried
import Unsafe.Coerce
import Control.Monad.Eff.Console as Console
hostname = "127.0.0.1"
port = 3000
createServer :: Fn1 HTTP (Effect Unit)
createServer = mkFn1 \http ->
let server = http.createServer $ mkFn2 \req res -> do
runFn1 res.statusCode 200
runFn2 res.setHeader "Content-Type" "text/plain"
runFn1 res.end "Hello world\n"
pure res
in runFn3 server.listen port hostname \_ -> do
log ("Server running at http://" <> hostname <> ":" <> show port <> "/")
main = createServer http
http :: HTTP
http = unsafeCoerce unit
log :: Fn1 String (Effect Unit)
log = mkFn1 \str -> unsafeCoerceEff (Console.log str)
type Effect = Eff ()
type HTTP
= { createServer ::
Fn1 (Fn2 Request Response (Effect Response))
{ listen :: Fn3 Int String (Unit -> Effect Unit) (Effect Unit)
}
}
type Request
= {}
type Response
= { end :: Fn1 String (Effect Unit)
, setHeader :: Fn2 String String (Effect Unit)
, statusCode :: Fn1 Int (Effect Unit)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment