Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions Server.hs
Original file line number Diff line number Diff line change
@@ -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


28 changes: 28 additions & 0 deletions react-scotty.cabal
Original file line number Diff line number Diff line change
@@ -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