Andy Melnikov (nponeccop) wrote,
Andy Melnikov
nponeccop

Габриэль жжот

http://www.haskellforall.com/2016/07/auto-generate-service-api-endpoints.html
{-# LANGUAGE DeriveGeneric #-}

import Server.Generic

data Command
    = Add      Double Double
    | Multiply Double Double
    deriving (Generic)

instance ParseRecord Command

handler :: Command -> IO Double
handler (Add      x y) = return (x + y)
handler (Multiply x y) = return (x * y)

main :: IO ()
main = serveJSON 8080 handler
If you run the above code, you get a server that has two endpoints:
/add/:double/:double
/multiply/:double/:double
You can run the server and test that they work:
$ curl 'localhost:8080/add/2/3'
5
$ curl 'localhost:8080/multiply/2/3'
6
Ну то есть, если по русски, - пейнлесс тайпед рест
Tags: fp, haskell, programming, до чего техника дошла
Subscribe

  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 33 comments