From c122a906639fe528a0b5a6108ba0ed6d245d81d6 Mon Sep 17 00:00:00 2001 From: Sibi Date: Fri, 17 Jul 2015 00:50:10 +0530 Subject: [PATCH] Haskell code. --- README.md | 9 ++++++++ Server.hs | 54 ++++++++++++++++++++++++++++++++++++++++++++++ react-scotty.cabal | 28 ++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 Server.hs create mode 100644 react-scotty.cabal diff --git a/README.md b/README.md index f8aa5bc7..80942982 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,15 @@ pip install -r requirements.txt python server.py ``` +### Haskell + +```sh +cabal sandbox init +cabal install --only-dependencies +ghc Server.hs +./Server +``` + ### Ruby ```sh ruby server.rb diff --git a/Server.hs b/Server.hs new file mode 100644 index 00000000..de416d7d --- /dev/null +++ b/Server.hs @@ -0,0 +1,54 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Main (main) where + +import Web.Scotty + +import Control.Monad (mzero) +import Control.Monad.Trans +import Network.Wai.Middleware.Static +import Network.Wai.Middleware.RequestLogger (logStdoutDev) +import Data.ByteString.Lazy (readFile, writeFile, fromStrict) +import qualified Data.ByteString as BS (readFile) +import Prelude hiding (readFile, writeFile) +import Data.Aeson hiding (json) +import Data.Text +import Data.Maybe (fromJust) + +data Comment = Comment { + commentText :: Text, + author :: Text + } deriving (Eq, Show, Ord) + +instance FromJSON Comment where + parseJSON (Object v) = Comment <$> + v .: "text" <*> + v .: "author" + parseJSON _ = mzero + +instance ToJSON Comment where + toJSON (Comment ctext author) = object ["text" .= ctext, "author" .= author] + + +main :: IO () +main = scotty 3000 $ do + + middleware $ staticPolicy (noDots >-> addBase "public") + middleware logStdoutDev + + get "/" $ file "./public/index.html" + + get "/comments.json" $ do + comments <- liftIO $ readFile "comments.json" + json $ fromJust $ (decode comments :: Maybe [Comment]) + + post "/comments.json" $ do + comments <- liftIO $ BS.readFile "comments.json" + let jsonComments = fromJust $ (decode $ fromStrict comments :: Maybe [Comment]) + author <- param "author" + comment <- param "text" + let allComments = jsonComments ++ [Comment comment author] + liftIO $ writeFile "comments.json" (encode allComments) + json allComments + + diff --git a/react-scotty.cabal b/react-scotty.cabal new file mode 100644 index 00000000..a3024211 --- /dev/null +++ b/react-scotty.cabal @@ -0,0 +1,28 @@ +-- Initial react-scotty.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/ + +name: react-scotty +version: 0.1.0.0 +synopsis: React Haskell code with Scotty +-- description: +license: GPL-2 +license-file: LICENSE +author: Sibi +maintainer: sibi@psibi.in +-- copyright: +category: Web +build-type: Simple +-- extra-source-files: +cabal-version: >=1.10 + +executable react-scotty + main-is: Server.hs + -- other-modules: + -- other-extensions: + build-depends: base >=4.8 && <4.9, + scotty, wai-extra, + mtl, text, aeson, + bytestring, + wai-middleware-static + -- hs-source-dirs: + default-language: Haskell2010