module Network.Gitit (
wiki
, reloadTemplates
, runHandler
, module Network.Gitit.Initialize
, module Network.Gitit.Config
, loginUserForm
, module Network.Gitit.Types
, module Network.Gitit.Framework
, module Network.Gitit.Layout
, module Network.Gitit.ContentTransformer
, getFileStore
, getUser
, getConfig
, queryGititState
, updateGititState
)
where
import Network.Gitit.Types
import Network.Gitit.Server
import Network.Gitit.Framework
import Network.Gitit.Handlers
import Network.Gitit.Initialize
import Network.Gitit.Config
import Network.Gitit.Layout
import Network.Gitit.State
(getFileStore, getUser, getConfig, queryGititState, updateGititState)
import Network.Gitit.ContentTransformer
import Network.Gitit.Authentication (loginUserForm)
import Paths_gitit (getDataFileName)
import Control.Monad.Reader
import Prelude hiding (readFile)
import qualified Data.ByteString.Char8 as B
import System.FilePath ((</>))
import Safe
wiki :: Config -> ServerPart Response
wiki conf = do
let static = staticDir conf
defaultStatic <- liftIO $ getDataFileName $ "data" </> "static"
let staticHandler = withExpiresHeaders $
fileServeStrict' [] static `mplus` fileServeStrict' [] defaultStatic
let handlers = [debugHandler | debugMode conf] ++ (authHandler conf : wikiHandlers)
let fs = filestoreFromConfig conf
let ws = WikiState { wikiConfig = conf, wikiFileStore = fs }
if compressResponses conf
then compressedResponseFilter
else return ""
staticHandler `mplus` runHandler ws (withUser conf $ msum handlers)
fileServeStrict' :: [FilePath] -> FilePath -> ServerPart Response
fileServeStrict' ps p = do
rq <- askRq
resp <- fileServeStrict ps p
if rsCode resp == 404 || lastNote "fileServeStrict'" (rqUri rq) == '/'
then mzero
else do
case getHeader "Content-Type" resp of
Just ct | B.pack "text/" `B.isPrefixOf` ct -> return resp
_ -> ignoreFilters >> return resp
wikiHandlers :: [Handler]
wikiHandlers =
[
guardBareBase >> getWikiBase >>= \b -> movedPermanently (b ++ "/") (toResponse ())
, dir "_user" currentUser
, dir "_activity" showActivity
, dir "_go" goToPage
, dir "_search" searchResults
, dir "_upload" $ do guard =<< return . uploadsAllowed =<< getConfig
msum [ methodOnly GET >> requireUser uploadForm
, methodOnly POST >> requireUser uploadFile ]
, dir "_random" $ methodOnly GET >> randomPage
, dir "_index" indexPage
, dir "_feed" feedHandler
, dir "_category" categoryPage
, dir "_categories" categoryListPage
, dir "_expire" expireCache
, dir "_showraw" $ msum
[ showRawPage
, guardPath isSourceCode >> showFileAsText ]
, dir "_history" $ msum
[ showPageHistory
, guardPath isSourceCode >> showFileHistory ]
, dir "_edit" $ requireUser (unlessNoEdit editPage showPage)
, dir "_diff" $ msum
[ showPageDiff
, guardPath isSourceCode >> showFileDiff ]
, dir "_discuss" discussPage
, dir "_delete" $ msum
[ methodOnly GET >>
requireUser (unlessNoDelete confirmDelete showPage)
, methodOnly POST >>
requireUser (unlessNoDelete deletePage showPage) ]
, dir "_preview" preview
, guardIndex >> indexPage
, guardCommand "export" >> exportPage
, methodOnly POST >> guardCommand "cancel" >> showPage
, methodOnly POST >> guardCommand "update" >>
requireUser (unlessNoEdit updatePage showPage)
, showPage
, guardPath isSourceCode >> methodOnly GET >> showHighlightedSource
, handleAny
, notFound =<< (guardPath isPage >> createPage)
]
reloadTemplates :: ServerPart Response
reloadTemplates = do
liftIO recompilePageTemplate
ok $ toResponse "Page templates have been recompiled."
runHandler :: WikiState -> Handler -> ServerPart Response
runHandler = mapServerPartT . unpackReaderT
unpackReaderT:: (Monad m)
=> c
-> (ReaderT c m) (Maybe ((Either b a), FilterFun b))
-> m (Maybe ((Either b a), FilterFun b))
unpackReaderT st handler = runReaderT handler st