tvl-depot/src/Main.hs

137 lines
5.1 KiB
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings, ScopedTypeVariables, GeneralizedNewtypeDeriving,
DeriveDataTypeable, FlexibleContexts, MultiParamTypeClasses, TemplateHaskell,
TypeFamilies, RecordWildCards, BangPatterns #-}
2012-02-22 22:03:31 +01:00
module Main where
import Control.Applicative ((<$>), (<*>), optional, pure)
import Control.Exception (bracket)
import Control.Monad (msum, mzero, when, unless)
import Control.Monad.State (get, put)
import Control.Monad.Reader (ask)
import qualified Crypto.Hash.SHA512 as SHA
import Data.Acid
import Data.Acid.Advanced
import Data.Acid.Local
import qualified Data.ByteString.Base64 as B64 (encode)
import Data.ByteString.Char8 (ByteString, pack)
import Data.Data (Data, Typeable)
import Data.Monoid (mempty)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time
import Data.SafeCopy (base, deriveSafeCopy)
import Happstack.Server hiding (Session)
import Network.CGI (liftIO)
import System.Environment(getEnv)
import System.Locale (defaultTimeLocale)
2012-02-22 22:03:31 +01:00
import Blog
import BlogDB hiding (addComment)
2012-02-24 16:06:33 +01:00
import Locales
2012-02-22 22:03:31 +01:00
{- Server -}
tmpPolicy :: BodyPolicy
tmpPolicy = (defaultBodyPolicy "./tmp/" 0 1000 1000)
main :: IO()
main = do
putStrLn ("TazBlog " ++ version ++ " in Haskell starting")
tbDir <- getEnv "TAZBLOG"
bracket (openLocalStateFrom (tbDir ++ "/BlogState") initialBlogState)
(createCheckpointAndClose)
(\acid -> simpleHTTP nullConf $ tazBlog acid)
tazBlog :: AcidState Blog -> ServerPart Response
tazBlog acid = do
msum [ dir (show DE) $ blogHandler acid DE
, dir (show EN) $ blogHandler acid EN
, do nullDir
showIndex acid DE
, do dir " " $ nullDir
seeOther ("https://plus.google.com/115916629925754851590" :: String) (toResponse ())
, path $ \(year :: Int) -> path $ \(month :: Int) -> path $ \(id_ :: String) -> formatOldLink year month id_
, dir "res" $ serveDirectory DisableBrowsing [] "../res"
2012-03-06 23:34:04 +01:00
, dir "notice" $ ok $ toResponse showSiteNotice
, do dir "admin" $ guardSession acid
adminHandler
2012-03-07 14:51:45 +01:00
, dir "admin" $ ok $ toResponse $ adminTemplate adminLogin "Login"
, dir "dologin" $ processLogin acid
, serveDirectory DisableBrowsing [] "../res"
]
2012-02-22 22:03:31 +01:00
blogHandler :: AcidState Blog -> BlogLang -> ServerPart Response
blogHandler acid lang =
msum [ path $ \(eId :: Integer) -> showEntry acid lang $ EntryId eId
2012-03-06 00:50:53 +01:00
, do
decodeBody tmpPolicy
dir "postcomment" $ path $
\(eId :: Integer) -> addComment acid $ EntryId eId
, do nullDir
showIndex acid lang
]
2012-02-22 22:03:31 +01:00
guardSession :: AcidState Blog -> ServerPartT IO ()
guardSession acid = do
(sId :: Text) <- readCookieValue "session"
(Just Session{..}) <- query' acid (GetSession $ SessionID sId)
(uName :: Text) <- readCookieValue "sUser"
now <- liftIO $ getCurrentTime
unless (and [uName == username user, sessionTimeDiff now sdate])
mzero
where
sessionTimeDiff :: UTCTime -> UTCTime -> Bool
sessionTimeDiff now sdate = (diffUTCTime now sdate) > 43200
adminHandler :: ServerPart Response
adminHandler = undefined
formatOldLink :: Int -> Int -> String -> ServerPart Response
formatOldLink y m id_ =
flip seeOther (toResponse ()) $
concat $ intersperse' "/" ["de", show y, show m, replace '.' '/' id_]
showEntry :: AcidState Blog -> BlogLang -> EntryId -> ServerPart Response
showEntry acid lang eId = do
entry <- query' acid (GetEntry eId)
2012-03-06 23:34:04 +01:00
ok $ tryEntry entry lang
2012-03-06 23:34:04 +01:00
tryEntry :: Maybe Entry -> BlogLang -> Response
tryEntry Nothing lang = toResponse $ showError NotFound lang
tryEntry (Just entry) _ = toResponse $ blogTemplate eLang eTitle $ renderEntry entry
where
eTitle = T.append ": " (title entry)
eLang = lang entry
2012-02-22 22:03:31 +01:00
showIndex :: AcidState Blog -> BlogLang -> ServerPart Response
showIndex acid lang = do
entries <- query' acid (LatestEntries lang)
(page :: Maybe Int) <- optional $ lookRead "page"
ok $ toResponse $ blogTemplate lang "" $
renderEntries False (eDrop page entries) (topText lang) (Just $ showLinks page lang)
where
eDrop :: Maybe Int -> [a] -> [a]
eDrop (Just i) = drop ((i-1) * 6)
eDrop Nothing = drop 0
2012-02-24 17:01:36 +01:00
addComment :: AcidState Blog -> EntryId -> ServerPart Response
addComment acid eId = do
now <- liftIO $ getCurrentTime >>= return
nComment <- Comment <$> lookText' "cname"
<*> lookText' "ctext"
<*> pure now
update' acid (AddComment eId nComment)
seeOther ("/" ++ show eId) (toResponse())
2012-03-06 00:50:53 +01:00
processLogin :: AcidState Blog -> ServerPart Response
processLogin acid = do
decodeBody tmpPolicy
account <- lookText' "account"
password <- look "password"
login <- query' acid (CheckUser (Username account) password)
if' login
(addSessionCookie account)
(ok $ toResponse $ ("Fail?" :: Text))
2012-03-03 03:35:20 +01:00
where
addSessionCookie = undefined