-
Notifications
You must be signed in to change notification settings - Fork 0
/
LulzBB.hs
68 lines (56 loc) · 1.84 KB
/
LulzBB.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
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 LulzBB.Main
import Network.CGI
import Network.URI (uriPath)
import Routing
import System.Directory (setCurrentDirectory)
main :: IO ()
main = do
-- TODO: Un-hardcode this
setCurrentDirectory "/usr/home/hark/dev/hs-lulzbb/"
db <- liftIO $ connectPostgreSQL "user=hark"
at <- getActionTable db "LulzBB/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", "application/json")
]
handler :: IConnection a => a -> UrlTree -> ActionTable -> CGI CGIResult
handler db ut at = do
-- Set a default Content-Type (for errors and stuff)
setHeader "Content-Type" "text/plain"
-- TODO: Un-hardcode this
-- TODO: Make sure this is /-terminated
let baseurl = "/lulzbb/"
-- Resolve the action
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
-- Execute the action
res <- liftIO action
{-
Set the proper headers, but only after the action has
been executed (so errors will be returned as text/plain)
-}
setHeader "Content-Type" $ resolveFormatMime format
-- Commit the database changes
liftIO $ commit db
-- Output the data
output res