-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
74 lines (62 loc) · 2.04 KB
/
Main.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
63
64
65
66
67
68
69
70
71
72
73
74
{-# LANGUAGE FlexibleContexts #-}
module Main where
import Pages
import Constants
import Happstack.Server
import Happstack.Server.Auth
import Control.Monad
import Data.Monoid
import Database.HDBC
import Database.HDBC.Sqlite3
import Control.Monad.IO.Class
import qualified Text.Blaze as B
main = do
con <- connectSqlite3 "blog.db"
g <- prepare con "select ROWID,title,post from posts;"
ins <- prepare con "INSERT INTO posts values (?,?);"
rm <- prepare con "delete from posts where ROWID=?;"
let insertP a b = execute ins [toSql a, toSql b]
let remv i = execute rm [toSql i]
let get = getPosts g
simpleHTTP nullConf $ decodeBody myPolicy >> msum
[dir "style.css" $ serveFile (asContentType "text/css") "style.css",
dir "new" $ myAuth inputHandler,
dir "remove" . myAuth $handleRemove remv,
dir "send" . myAuth $ newPostHandler insertP,
mainHandler get
]
getPosts :: Statement -> IO [(Int,B.Html,B.Html)]
getPosts query = do
execute query []
rows <- fetchAllRows query
return (map (\[a,b,c] -> (fromSql a ,convert b,convert c)) rows)
where convert s = B.toHtml (fromSql s :: String)
myAuth :: ServerPart a -> ServerPart a
myAuth = basicAuth "127.0.0.1" accounts
newPostHandler :: (String -> String -> IO a) -> ServerPart Response
newPostHandler f = do
methodM POST
title <- look "title"
post <- look "post"
liftIO (f title post)
seeOther domain . toResponse $ "The post is added!"
inputHandler :: ServerPart Response
inputHandler = ok . toResponse $ inputPage
mainHandler :: IO [(Int,B.Html,B.Html)] -> ServerPart Response
mainHandler f = do
posts <- liftIO f
ok . toResponse $ mainPage posts
lookId :: RqData Int
lookId = lookRead "id"
handleRemove remv = do
r <- getDataFn lookId
case r of
(Left e) ->
badRequest . toResponse . unlines $ e
(Right i) -> do
liftIO (remv i)
seeOther domain . toResponse $ "The post is deleted!"
initDB = do
con <- connectSqlite3 "blog.db"
runRaw con "CREATE TABLE posts (title TEXT,post TEXT);"
commit con