Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/Node/HTTP.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,29 @@ exports.createServer = function (handleRequest) {
};
};

exports.listen = function (server) {
exports.listenImpl = function (server) {
return function (port) {
return function (hostname) {
return function (backlog) {
return function (done) {
return function () {
if (backlog !== null) {
server.listen(port, hostname, backlog, done);
} else {
server.listen(port, hostname, done);
}
};
};
};
};
};
};

exports.listenSocket = function (server) {
return function (path) {
return function (done) {
return function () {
server.listen(port, function () {
done();
});
server.listen(path, done);
};
};
};
Expand Down
19 changes: 17 additions & 2 deletions src/Node/HTTP.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import Prelude

import Control.Monad.Eff (Eff)

import Data.Maybe (Maybe)
import Data.Nullable (Nullable, toNullable)
import Data.StrMap (StrMap)

import Node.Stream (Writable, Readable)
Expand All @@ -27,8 +29,21 @@ foreign import data HTTP :: !
-- | Create a HTTP server, given a function to be executed when a request is received.
foreign import createServer :: forall eff. (Request -> Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Server

-- | Listen on the specified port. The specified callback will be run when setup is complete.
foreign import listen :: forall eff. Server -> Int -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit
foreign import listenImpl :: forall eff. Server -> Int -> String -> Nullable Int -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit

-- | Listen on a port in order to start accepting HTTP requests. The specified callback will be run when setup is complete.
listen :: forall eff. Server -> ListenOptions -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit
listen server opts done = listenImpl server opts.port opts.hostname (toNullable opts.backlog) done

-- | Listen on a unix socket. The specified callback will be run when setup is complete.
foreign import listenSocket :: forall eff. Server -> String -> Eff (http :: HTTP | eff) Unit -> Eff (http :: HTTP | eff) Unit

-- | Options to be supplied to `listen`. See the [Node API](https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_server_listen_handle_callback) for detailed information about these.
type ListenOptions =
{ hostname :: String
, port :: Int
, backlog :: Maybe Int
}

-- | Get the request HTTP version
httpVersion :: Request -> String
Expand Down
3 changes: 2 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log, logShow)

import Data.Foldable (foldMap)
import Data.Maybe (Maybe(..))

import Node.Encoding (Encoding(..))
import Node.HTTP (HTTP, listen, createServer, setHeader, requestMethod, requestURL, responseAsStream, requestAsStream, setStatusCode)
Expand All @@ -25,7 +26,7 @@ main = do
testBasic :: forall eff. Eff (console :: CONSOLE, http :: HTTP | eff) Unit
testBasic = do
server <- createServer respond
listen server 8080 $ void do
listen server { hostname: "localhost", port: 8080, backlog: Nothing } $ void do
log "Listening on port 8080."
simpleReq "http://localhost:8080"
where
Expand Down