-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Payload HTTP server backend recipe #226
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab1a1fd
Payload HTTP server backend.
eviefp 908e5f0
Address review comments.
eviefp 3ee531c
Add a few details about payload.
eviefp 05e4bcb
Use package-set instead.
eviefp e481ff8
Merge remote-tracking branch 'upstream/master' into recipe/payload
eviefp f06296f
Whoops.
eviefp 3d2e011
Restore package.json
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/bower_components/ | ||
/node_modules/ | ||
/.pulp-cache/ | ||
/output/ | ||
/generated-docs/ | ||
/.psc-package/ | ||
/.psc* | ||
/.purs* | ||
/.psa* | ||
/.spago | ||
/web-dist/ | ||
/prod-dist/ | ||
/prod/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# PayloadHttpApiNode | ||
|
||
Implements a simple 'quote' API using the [payload](https://github.com/hoodunit/purescript-payload) | ||
HTTP backend. | ||
|
||
## Expected Behavior: | ||
|
||
### Node.js | ||
|
||
HTTP server is started. You can call the API using your favorite HTTP client. | ||
This example uses [httpie](https://httpie.org/): | ||
```sh | ||
# get all quotes | ||
http get 'http://localhost:3000/quote' | ||
|
||
# get the default initial quote | ||
http get 'http://localhost:3000/quote/1' | ||
|
||
# add a quote | ||
echo "This is a new quote" | http post 'http://localhost:3000/quote' | ||
|
||
# retrieve it | ||
http get 'http://localhost:3000/quote/1' | ||
|
||
# get all quotes again | ||
http get 'http://localhost:3000/quote' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This file just indicates that the node backend is supported. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file should be named |
||
It is used for CI and autogeneration purposes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ name = "PayloadHttpApiNode" | ||
, dependencies = [ "console", "effect", "avar", "ordered-collections", "payload" ] | ||
, packages = ../../packages.dhall | ||
, sources = [ "recipes/PayloadHttpApiNode/src/**/*.purs" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
module PayloadHttpApiNode.Main where | ||
|
||
import Prelude | ||
|
||
import Data.Either (Either(..)) | ||
import Data.Map (Map) | ||
import Data.Map as Map | ||
import Data.Maybe (Maybe(..)) | ||
import Data.Tuple (Tuple(..)) | ||
import Effect (Effect) | ||
import Effect.AVar (AVar) | ||
import Effect.AVar as EffVar | ||
import Effect.Aff (Aff) | ||
import Effect.Aff.AVar as AffVar | ||
import Payload.ResponseTypes (Failure(..)) | ||
import Payload.Server as Payload | ||
import Payload.Spec (GET, Spec(Spec), POST) | ||
|
||
-- | This is used as a return type for whenever we don't have something | ||
-- | meaningful to return. | ||
type StatusCodeResponse = | ||
{ statusCode :: Int | ||
, statusMessage :: String | ||
} | ||
|
||
-- | Our API is all about quotes, and this is how we represent a single quote. | ||
type Quote = | ||
{ text :: String | ||
, id :: Int | ||
} | ||
|
||
-- | This fully describes our API as a big record under the 'Spec' constructor. | ||
-- | | ||
-- | Each label represents an endpoint in our quote API: | ||
-- | 'quote' is a GET-by-id endpoint. We match the URL by the <id> parameter. | ||
-- | 'addQUote' is a POST-to-insert a new quote. The body is just a string. | ||
-- | 'getAll' is a GET everything in our quote 'database'. | ||
-- | | ||
-- | The response type is also encoded in each endpoint. | ||
-- | Note that the implementation is trivial: this only represents type-level | ||
-- | information used downstream. | ||
spec | ||
:: Spec | ||
{ quote :: GET "/quote/<id>" | ||
{ params :: { id :: Int } | ||
, response :: String | ||
} | ||
, addQuote :: POST "/quote" | ||
{ body :: String | ||
, response :: StatusCodeResponse | ||
} | ||
, getAll :: GET "/quote" | ||
{ response :: Array Quote | ||
} | ||
} | ||
spec = Spec | ||
|
||
-- | This is where everything comes together: we create a record of HANDLERS | ||
-- | for each of the routes we defined in 'spec'. | ||
-- | The types must follow the definitions, so for example 'quote' must take | ||
-- | a record containing "params :: { id :: Int }". | ||
-- | | ||
-- | Note that we are taking an 'AVar (Map Int String)'. We do this in order to | ||
-- | keep state across calls. | ||
-- | | ||
-- | AVars are variables that you can read and update within an 'Aff' context. | ||
-- | The 'AVar' created in 'main' is threaded through 'handlers' to each | ||
-- | individual handler. | ||
handlers :: _ | ||
handlers quotes = | ||
{ quote: quote quotes | ||
, addQuote: addQuote quotes | ||
, getAll: getAll quotes | ||
} | ||
|
||
-- | Main entry point: we just create a new 'AVar' (fake "database") and | ||
-- | launch the payload server. | ||
main :: Effect Unit | ||
main = do | ||
quotes <- EffVar.new $ Map.singleton 1 "This is a quote" | ||
Payload.launch spec (handlers quotes) | ||
|
||
-- | This represents our application's state. It's essentially our database | ||
-- | (except it gets reset when application gets restarted). | ||
type State = AVar (Map Int String) | ||
|
||
-- | This is the handler for getting a quote by id. | ||
-- | We start by reading the "database". | ||
-- | If we can't find the requested 'id', then we just return an error. | ||
-- | Otherwise, we return the requested quote. | ||
quote :: State -> { params :: { id :: Int } } -> Aff (Either Failure String) | ||
quote st { params : {id} } = do | ||
quotes <- AffVar.read st | ||
case Map.lookup id quotes of | ||
Nothing -> pure $ Left $ Forward "" | ||
Just v -> pure (pure v) | ||
|
||
-- | Adding a quote requires us to 'block' reads/writes to our 'database', which | ||
-- | is precisely what 'take' does. | ||
-- | We then look for the highest id in the 'database' and increment it by one. | ||
-- | We finish off by 'unblocking' and pushing our updated 'database'. | ||
addQuote :: State -> { body :: String } -> Aff StatusCodeResponse | ||
addQuote st { body } = do | ||
quotes <- AffVar.take st | ||
id <- case Map.findMax quotes of | ||
Nothing -> pure 1 | ||
Just { key } -> pure (key + 1) | ||
AffVar.put (Map.insert id body quotes) st | ||
pure { statusCode: 200, statusMessage: body } | ||
|
||
-- | We start by reading the current 'database'. Since we can't easily encode | ||
-- | a Map structure directly, we transform it to an Array by calling | ||
-- | 'toUnfoldable' (the map works over 'Aff'). | ||
-- | Our type now is 'Aff (Array (Tuple Int String))'. Unfortunately, 'Tuples' | ||
-- | are also not trivial to encode, so we also need to 'map' over the | ||
-- | Array and change all Tuples to records. | ||
-- | | ||
-- | We end up returning all data in the 'database' as an array of records. | ||
getAll :: forall r. State -> { |r } -> Aff (Array Quote) | ||
getAll st _ = map tupleToRecord <<< Map.toUnfoldable <$> AffVar.read st | ||
where | ||
tupleToRecord :: Tuple Int String -> Quote | ||
tupleToRecord (Tuple id text) = {id, text} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is on line 4, it won't be included when you run
make readme
. Remove the newline and let the text wrap.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, could you add something in its description that says this has similar design principles as Haskell's Servant?