Skip to content

Commit c122a90

Browse files
committed
Haskell code.
1 parent 0eb5dbd commit c122a90

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ pip install -r requirements.txt
2222
python server.py
2323
```
2424

25+
### Haskell
26+
27+
```sh
28+
cabal sandbox init
29+
cabal install --only-dependencies
30+
ghc Server.hs
31+
./Server
32+
```
33+
2534
### Ruby
2635
```sh
2736
ruby server.rb

Server.hs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
3+
module Main (main) where
4+
5+
import Web.Scotty
6+
7+
import Control.Monad (mzero)
8+
import Control.Monad.Trans
9+
import Network.Wai.Middleware.Static
10+
import Network.Wai.Middleware.RequestLogger (logStdoutDev)
11+
import Data.ByteString.Lazy (readFile, writeFile, fromStrict)
12+
import qualified Data.ByteString as BS (readFile)
13+
import Prelude hiding (readFile, writeFile)
14+
import Data.Aeson hiding (json)
15+
import Data.Text
16+
import Data.Maybe (fromJust)
17+
18+
data Comment = Comment {
19+
commentText :: Text,
20+
author :: Text
21+
} deriving (Eq, Show, Ord)
22+
23+
instance FromJSON Comment where
24+
parseJSON (Object v) = Comment <$>
25+
v .: "text" <*>
26+
v .: "author"
27+
parseJSON _ = mzero
28+
29+
instance ToJSON Comment where
30+
toJSON (Comment ctext author) = object ["text" .= ctext, "author" .= author]
31+
32+
33+
main :: IO ()
34+
main = scotty 3000 $ do
35+
36+
middleware $ staticPolicy (noDots >-> addBase "public")
37+
middleware logStdoutDev
38+
39+
get "/" $ file "./public/index.html"
40+
41+
get "/comments.json" $ do
42+
comments <- liftIO $ readFile "comments.json"
43+
json $ fromJust $ (decode comments :: Maybe [Comment])
44+
45+
post "/comments.json" $ do
46+
comments <- liftIO $ BS.readFile "comments.json"
47+
let jsonComments = fromJust $ (decode $ fromStrict comments :: Maybe [Comment])
48+
author <- param "author"
49+
comment <- param "text"
50+
let allComments = jsonComments ++ [Comment comment author]
51+
liftIO $ writeFile "comments.json" (encode allComments)
52+
json allComments
53+
54+

react-scotty.cabal

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- Initial react-scotty.cabal generated by cabal init. For further
2+
-- documentation, see http://haskell.org/cabal/users-guide/
3+
4+
name: react-scotty
5+
version: 0.1.0.0
6+
synopsis: React Haskell code with Scotty
7+
-- description:
8+
license: GPL-2
9+
license-file: LICENSE
10+
author: Sibi
11+
maintainer: sibi@psibi.in
12+
-- copyright:
13+
category: Web
14+
build-type: Simple
15+
-- extra-source-files:
16+
cabal-version: >=1.10
17+
18+
executable react-scotty
19+
main-is: Server.hs
20+
-- other-modules:
21+
-- other-extensions:
22+
build-depends: base >=4.8 && <4.9,
23+
scotty, wai-extra,
24+
mtl, text, aeson,
25+
bytestring,
26+
wai-middleware-static
27+
-- hs-source-dirs:
28+
default-language: Haskell2010

0 commit comments

Comments
 (0)