-
Notifications
You must be signed in to change notification settings - Fork 0
/
Centor.hs
60 lines (47 loc) · 1.49 KB
/
Centor.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
module Main where
import Control.Exception (throw, handle, Exception (NoMethodError, ErrorCall))
import Control.Monad (liftM)
import Data.List (isPrefixOf)
import Data.Maybe (fromMaybe)
import Database.HDBC (commit, disconnect, IConnection (..))
import Database.HDBC.PostgreSQL (connectPostgreSQL)
import Haft.Routing
import Network.CGI
import Network.URI (uriPath)
import System.Directory (setCurrentDirectory)
import Centor.Init
main :: IO ()
main = do
-- XXX
setCurrentDirectory "/usr/home/hark/dev/hs-centor/"
db <- liftIO $ connectPostgreSQL "user=hark"
at <- getActionTable db "Centor/Templates/"
ut <- getUrlTree
runCGI $ handleErrors $ handler db ut at
`catchCGI` (\e -> (liftIO $ disconnect db) >> throwCGI e)
resolveFormatMime :: String -> String
resolveFormatMime format = fromMaybe "text/html" $ lookup format [
("html", "text/html"),
("xml", "text/xml"),
("rss", "application/rss+xml"),
("json", "text/plain")
]
handler :: IConnection a => a -> UrlTree -> ActionTable -> CGI CGIResult
handler db ut at = do
setHeader "Content-Type" "text/plain"
-- XXX
let baseurl = "/centor/"
fullurl <- liftM uriPath requestURI
if not $ isPrefixOf baseurl fullurl
then throwCGI $ NoMethodError $ unlines [
"uri isn't in baseurl.",
"uri: " ++ fullurl,
"baseurl: " ++ baseurl
]
else return ()
let req = (drop (length baseurl) fullurl)
let (action, format) = route ut at req
res <- liftIO action
setHeader "Content-Type" $ resolveFormatMime format
liftIO $ commit db
output res