Skip to content

Instantly share code, notes, and snippets.

@kgadek
Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgadek/11380592 to your computer and use it in GitHub Desktop.
Save kgadek/11380592 to your computer and use it in GitHub Desktop.
AGH Informatyka (sem 10) :: Zaawansowane Techniki Integracji Systemów :: Laboratoria 1
{-# LANGUAGE OverloadedStrings, DeriveGeneric #-}
import Web.Scotty
import Data.Aeson
import GHC.Generics
data Stuff = Stuff { dt :: Things
} deriving (Show, Generic)
instance FromJSON Stuff
instance ToJSON Stuff
data Things = Things { id :: Int
, tp :: String
, msg :: String
} deriving (Show, Generic)
instance FromJSON Things
instance ToJSON Things
main :: IO ()
main = scotty 3000 $ do
post "/" $ do
bd <- body
case decode bd of
Nothing -> html "LOL NOPE! <strong>objection!</strong>"
Just (Stuff things) -> raw $ encode (Stuff things { tp = "response" })
@kgadek
Copy link
Author

kgadek commented Apr 28, 2014

Dla porównania — analogiczny program w Pythonie.

import json
from bottle import post, route, run, template, BaseRequest, request

@post('/')
def index():
    xxx = json.loads(request.body.read().decode("utf-8"))
    xxx["data"]["type"] = "response"
    return json.dumps(xxx)

run(host='localhost', port=8080)

Ciężko pokonać bottle.py pod względem rozmiaru kodu w tym przykładzie, niemniej na usprawiedliwienie Haskella: dodatkowo weryfikuje protokół wejściowy i wyjściowy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment