forked from reactjs/react-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.hs
62 lines (49 loc) · 2.12 KB
/
Server.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
-- This file provided by Facebook is for non-commercial testing and evaluation
-- purposes only. Facebook reserves all rights not expressly granted.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
-- FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
{-# 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 "/api/comments" $ do
comments <- liftIO $ readFile "comments.json"
json $ fromJust $ (decode comments :: Maybe [Comment])
post "/api/comments" $ 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