Skip to content

Commit 6443023

Browse files
paluhowickstrom
authored andcommitted
Extract server options module and add Hostaname type
1 parent 7f88b56 commit 6443023

File tree

3 files changed

+57
-45
lines changed

3 files changed

+57
-45
lines changed

src/Hyper/Node/Server.purs

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ module Hyper.Node.Server
44
, NodeResponse
55
, writeString
66
, write
7-
, defaultOptions
8-
, defaultOptionsWithLogging
7+
, module Hyper.Node.Server.Options
98
, runServer
109
, runServer'
1110
) where
@@ -23,8 +22,7 @@ import Control.Monad.Aff.AVar (putVar, takeVar, modifyVar, makeVar', AVAR, makeV
2322
import Control.Monad.Aff.Class (class MonadAff, liftAff)
2423
import Control.Monad.Eff (Eff)
2524
import Control.Monad.Eff.Class (class MonadEff, liftEff)
26-
import Control.Monad.Eff.Console (CONSOLE, log)
27-
import Control.Monad.Eff.Exception (EXCEPTION, Error, catchException, error)
25+
import Control.Monad.Eff.Exception (EXCEPTION, catchException, error)
2826
import Control.Monad.Error.Class (throwError)
2927
import Data.Either (Either(..), either)
3028
import Data.Lazy (defer)
@@ -34,7 +32,8 @@ import Data.Tuple (Tuple(..))
3432
import Hyper.Conn (Conn)
3533
import Hyper.Middleware (Middleware, evalMiddleware, lift')
3634
import Hyper.Middleware.Class (getConn, modifyConn)
37-
import Hyper.Port (Port(..))
35+
import Hyper.Node.Server.Options as Hyper.Node.Server.Options
36+
import Hyper.Node.Server.Options (Options)
3837
import Hyper.Request (class ReadableBody, class Request, class StreamableBody, RequestData, parseUrl, readBody)
3938
import Hyper.Response (class ResponseWritable, class Response, ResponseEnded, StatusLineOpen)
4039
import Hyper.Status (Status(..))
@@ -210,35 +209,6 @@ instance responseWriterHttpResponse :: MonadAff (http ∷ HTTP | e) m
210209
:*> modifyConn (_ { response = HttpResponse r })
211210

212211

213-
type ServerOptions e =
214-
{ hostname String
215-
, port Port
216-
, onListening Port Eff (http HTTP | e) Unit
217-
, onRequestError Error Eff (http HTTP | e) Unit
218-
}
219-
220-
221-
defaultOptions e. ServerOptions e
222-
defaultOptions =
223-
{ hostname: "0.0.0.0"
224-
, port: Port 3000
225-
, onListening: const (pure unit)
226-
, onRequestError: const (pure unit)
227-
}
228-
229-
230-
defaultOptionsWithLogging e. ServerOptions (console CONSOLE | e)
231-
defaultOptionsWithLogging =
232-
defaultOptions { onListening = onListening
233-
, onRequestError = onRequestError
234-
}
235-
where
236-
onListening (Port port) =
237-
log ("Listening on http://localhost:" <> show port)
238-
onRequestError err =
239-
log ("Request failed: " <> show err)
240-
241-
242212
mkHttpRequest :: HTTP.Request -> HttpRequest
243213
mkHttpRequest request =
244214
HttpRequest request requestData
@@ -257,7 +227,7 @@ mkHttpRequest request =
257227
runServer'
258228
:: forall m e c c'
259229
. Functor m
260-
=> ServerOptions e
230+
=> Options e
261231
-> c
262232
-> (forall a. m a -> Aff (http :: HTTP | e) a)
263233
-> Middleware
@@ -269,10 +239,10 @@ runServer'
269239
runServer' options components runM middleware = do
270240
server <- HTTP.createServer onRequest
271241
let listenOptions = { port: unwrap options.port
272-
, hostname: options.hostname
242+
, hostname: unwrap options.hostname
273243
, backlog: Nothing
274244
}
275-
HTTP.listen server listenOptions (options.onListening options.port)
245+
HTTP.listen server listenOptions (options.onListening options.hostname options.port)
276246
where
277247
onRequest HTTP.Request HTTP.Response Eff (http :: HTTP | e) Unit
278248
onRequest request response =
@@ -285,7 +255,7 @@ runServer' options components runM middleware = do
285255

286256
runServer
287257
:: forall e c c'.
288-
ServerOptions e
258+
Options e
289259
-> c
290260
-> Middleware
291261
(Aff (http :: HTTP | e))

src/Hyper/Node/Server/Options.purs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module Hyper.Node.Server.Options
2+
( defaultOptions
3+
, defaultOptionsWithLogging
4+
, Hostname(..)
5+
, Options(..)
6+
, Port(..)
7+
) where
8+
9+
import Control.Monad.Eff (Eff)
10+
import Control.Monad.Eff.Console (CONSOLE, log)
11+
import Control.Monad.Eff.Exception (Error)
12+
import Data.Newtype (class Newtype)
13+
import Node.HTTP (HTTP)
14+
import Prelude
15+
16+
newtype Hostname = Hostname String
17+
derive instance newtypeHostname :: Newtype Hostname _
18+
19+
newtype Port = Port Int
20+
derive instance newtypePort :: Newtype Port _
21+
22+
type Options e =
23+
{ hostname :: Hostname
24+
, port :: Port
25+
, onListening :: Hostname -> Port -> Eff (http :: HTTP | e) Unit
26+
, onRequestError :: Error -> Eff (http :: HTTP | e) Unit
27+
}
28+
29+
30+
defaultOptions :: forall e. Options e
31+
defaultOptions =
32+
{ hostname: Hostname "0.0.0.0"
33+
, port: Port 3000
34+
, onListening: const (const (pure unit))
35+
, onRequestError: const (pure unit)
36+
}
37+
38+
39+
defaultOptionsWithLogging :: forall e. Options (console :: CONSOLE | e)
40+
defaultOptionsWithLogging =
41+
defaultOptions { onListening = onListening
42+
, onRequestError = onRequestError
43+
}
44+
where
45+
onListening (Hostname hostname) (Port port) =
46+
log ("Listening on http://" <> hostname <> ":" <> show port)
47+
onRequestError err =
48+
log ("Request failed: " <> show err)
49+

src/Hyper/Port.purs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)