8f58e72fa0f4268b443826f28b2d20afb5f290eb
axon
WIP
HTTP server library inspired by axum, allowing best-in-class
expressive routing.
Example
This example implements this REST interface in 36LoC:
GET /cheeses- Lists all cheeses (strings) known to serverPOST /cheeses- Add a cheese to the cheese listDELETE /cheeses/:cheese- Remove a cheese from the cheese list
module Main where
import Prelude
import Axon as Axon
import Axon.Request.Handler as Handler
import Axon.Request.Parts.Class (Delete, Get, Path(..), Post)
import Axon.Request.Parts.Path (type (/))
import Axon.Response (Response)
import Axon.Response.Construct (Json(..), toResponse)
import Axon.Response.Status as Status
import Data.Filterable (filter)
import Data.Foldable (elem)
import Data.Tuple.Nested ((/\))
import Effect (Effect)
import Effect.Aff (Aff, launchAff_, joinFiber)
import Effect.Aff as Aff
import Effect.Class (liftEffect)
import Effect.Ref (Ref)
import Effect.Ref as Ref
main :: Effect Unit
main = launchAff_ do
cheeses :: Ref (Array String) <- liftEffect $ Ref.new
[ "cheddar", "swiss", "gouda" ]
let
getCheeses :: Get -> Path "cheeses" _ -> Aff Response
getCheeses _ _ = liftEffect do
cheeses' <- Ref.read cheeses
toResponse $ Status.ok /\ Json cheeses'
deleteCheese :: Delete -> Path ("cheeses" / String) _ -> Aff Response
deleteCheese _ (Path id) = liftEffect do
cheeses' <- Ref.read cheeses
if not $ elem id cheeses' then
toResponse Status.notFound
else
Ref.modify_ (filter (_ /= id)) cheeses
*> toResponse Status.accepted
postCheese :: Post -> Path "cheeses" _ -> String -> Aff Response
postCheese _ _ cheese =
let
tryInsert as
| elem cheese as = { state: as, value: false }
| otherwise = { state: as <> [ cheese ], value: true }
in
liftEffect
$ Ref.modify' tryInsert cheeses
>>= if _ then toResponse Status.accepted else toResponse Status.conflict
handle <-
Axon.serveBun
{ port: 8080, hostname: "localhost" }
(getCheeses `Handler.or` postCheese `Handler.or` deleteCheese)
joinFiber handle.join
Request Handlers
Request handler functions have any number of parameters that are RequestParts and return an Aff Response (or any MonadAff).
RequestParts
RequestPartsRequest- Always succeeds; provides the entire request
- Combinators
Unit- Always succeeds
a /\ b- Tuple of
aandb, whereaandbareRequestParts.
- Tuple of
Maybe aamust beRequestParts. Ifacan't be extracted, the handler will still succeed and this will beNothing. Ifawas extracted, it's wrapped inJust.
Either a baandbmust beRequestParts. Succeeds if eitheraorbsucceeds (preferringa). Fails if both fail.
- Body
String- succeeds when request has a non-empty body that is valid UTF-8
Json a- succeeds when request has a
Stringbody (see above) that can be parsed intoausingDecodeJson.
- succeeds when request has a
Buffer- succeeds when request has a nonempty body.
Stream- succeeds when request has a nonempty body.
- Headers
Header aamust beTypedHeaderfromAxon.Header.Typed. Allows statically (ex.ContentType Type.MIME.Json) or dynamically (ex.ContentType String) matching request headers.
HeaderMap- All headers provided in the request
- Path
Path a c- Statically match the path of the request, and extract parameters. See
Axon.Request.Parts.Path. (TODO: this feels too magical, maybe follow axum's prior art of baking paths into the router declaration?)
- Statically match the path of the request, and extract parameters. See
- Method -
Get-Post-Put-Patch-Delete-Options-Connect-Trace
Similarly to the structural extraction of request parts; handlers can use Axon.Response.Construct.ToResponse for easily constructing responses.
ToResponse
ToResponse- Combinators
Status /\ a- Special case to make sure any
Statusin a tuple will take priority over any default statuses within. TODO: This case (overlapping witha /\ brequires the class to be "sealed" in an instance chain. Want a clean way around this so consumers can implementToResponse.)
- Special case to make sure any
a /\ b- Merges
toResponse aandtoResponse b, usingbon conflicts
- Merges
- Status
Axon.Response.Status.Status
- Body
Axon.Response.Body.BodyStringNode.Buffer.BufferNode.Stream.Readable a(for alla)Axon.Response.Construct.Json aamust beEncodeJson. This will set the body toastringified, and setContent-Typetoapplication/json.
- Headers
ToResponseis implemented for all implementors ofTypedHeader- TODO:
Map String String
Description
Languages
PureScript
96.4%
JavaScript
3.6%