Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
This module re-exports the essential functions for quickly writing Nero
applications. Use the original modules for more specialized functions.
- type Application = Request -> IO Response
- class Server a where
- application :: a -> Application
- data Request
- get :: Url -> Request
- post :: Url -> Payload -> Request
- _GET :: Prism' Request GET
- _POST :: Prism' Request POST
- method :: Request -> ByteString
- path :: HasPath a => Lens' a Path
- query :: HasQuery a => Lens' a Query
- form :: Formed a => Traversal' a Form
- params :: Traversal' Request MultiMap
- param :: Param a => Text -> Traversal' a Text
- body :: HasBody a => a -> Body
- data GET
- data POST
- data Response
- ok :: Text -> Response
- movedPermanently :: Url -> Response
- data Url
- match :: Getter Text Match
- class Prefixed a where
- class Suffixed a where
- sep :: Text -> Prism' Match Match
- split :: Text -> Fold Text Match
- exact :: Text -> Prism' Text ()
- class Target a where
- dummyRequest :: Request
- dummyRequestForm :: Request
- dummyUrl :: Url
- module Nero.Prelude
Server
type Application = Request -> IO Response Source
Ultimately any valid Nero server application must be transformed
. This type class facilitates the
creation of web server handling Request
-> IO
Response
Nero
applications.
application :: a -> Application Source
Request
An HTTP Request.
Eq Request | |
Show Request | |
Param Request | It traverses the values with the same key both in the query string
and the form encoded body of a |
Formed Request | |
HasBody Request | |
HasQuery Request | |
HasPath Request | |
HasHost Request | |
HasUrl Request | |
Server (Request -> Maybe Response) | |
Server (Request -> Response) |
method :: Request -> ByteString Source
Show Request
method.
form :: Formed a => Traversal' a Form Source
params :: Traversal' Request MultiMap Source
This Traversal
lets you traverse every HTTP parameter regardless of
whether it's present in the query string or in the form encoded body
of a POST
Request
. In the rare case where there are HTTP parameters in
both, every parameter is still being traversed starting from the /query
string/.
You might want to use param
for traversing a specific parameter.
>>>
let request = dummyRequestForm & query . at "name" ?~ ["hello", "out"] & form . at "name" ?~ ["there"]
>>>
foldOf params request ^? ix "name"
Just ["hello","out","there"]
GET
POST
A POST
Request
.
Response
An HTTP response.
Creates an 200 OK response from the given text. It automatically encodes the text to 'utf-8'. The Mime type is text/plain.
movedPermanently :: Url -> Response Source
URL
Matching
This Prism'
strips/prepends a prefix.
>>>
("/hello/there"::Text) ^? prefixed "/hello/"
Just "there"
>>>
prefixed "/hello/" # ("there"::Text)
"/hello/there"
If matching the entire source it previews to an empty Text
. You might
use exact
if you are expecting this behavior.
>>>
("hello"::Text) ^? prefixed "hello"
Just ""
This also means that to review
an entire source, you need to give it
an empty Text
.
>>>
prefixed "hello" # (mempty::Text)
"hello"
An empty Match
matches to itself regardless of the pattern.
>>>
preview (prefixed "hello") (review (prefixed "hello") (mempty::Text)) <&> is _Empty
Just True
This Prism'
strips/appends a suffix.
>>>
("/hello/there"::Text) ^? suffixed "there"
Just "/hello/"
>>>
suffixed "there" # ("/hello/"::Text)
"/hello/there"
If matching the entire source it previews to an empty Text
. You might
use exact
if you are expecting this behavior.
>>>
("hello"::Text) ^? suffixed "hello"
Just ""
This also means that to review
an entire source, you need to give it
an empty Text
.
>>>
suffixed "hello" # (mempty::Text)
"hello"
An empty Match
matches to itself regardless of the pattern.
>>>
preview (suffixed "hello") (review (suffixed "hello") (mempty::Text)) <&> is _Empty
Just True
sep :: Text -> Prism' Match Match Source
This Prism'
splits/joins at the first occurrence of a boundary
for the first value of a Match
.
>>>
pure "hello/out/there" ^? sep "/" <&> toListOf folded
Just ["out/there","hello"]
>>>
sep "/" # (pure "out/there" <> pure "hello") & view _head
"hello/out/there"
Notice what happens when there is no source before or after a boundary:
>>>
pure "hello/" ^? sep "/"
Just ["","hello"]>>>
(pure "hello/" <> pure "there") ^? sep "/"
Just ["","hello","there"]
>>>
pure "/hello" ^? sep "/"
Just ["hello",""]>>>
(pure "/hello" <> pure "there") ^? sep "/"
Just ["hello","","there"]
When the source is identical to the boundary: >>> pure "hello" ^? sep "hello" Just []
Results handling
Testing
dummyRequest :: Request Source
An empty GET request useful for testing.
dummyRequestForm :: Request Source
An empty POST request with an empty form encoded body useful for testing.
Nero Prelude
module Nero.Prelude