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
, module Network.Gitit.Page
, 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.Page
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 System.Directory (getTemporaryDirectory)
import Safe
wiki :: Config -> ServerPart Response
wiki conf = do
tempDir <- liftIO getTemporaryDirectory
let maxSize = fromIntegral $ maxUploadSize conf
decodeBody $ defaultBodyPolicy tempDir maxSize maxSize maxSize
let static = staticDir conf
defaultStatic <- liftIO $ getDataFileName $ "data" </> "static"
let staticHandler = withExpiresHeaders $
serveDirectory' static `mplus` serveDirectory' defaultStatic
let debugHandler' = msum [debugHandler | debugMode conf]
let handlers = debugHandler' `mplus` authHandler conf `mplus`
authenticate ForRead (msum 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 handlers)
serveDirectory' :: FilePath -> ServerPart Response
serveDirectory' p = do
rq <- askRq
resp' <- serveDirectory EnableBrowsing [] 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 "_activity" showActivity
, dir "_go" goToPage
, dir "_search" searchResults
, dir "_upload" $ do guard =<< return . uploadsAllowed =<< getConfig
msum [ methodOnly GET >> authenticate ForModify uploadForm
, methodOnly POST >> authenticate ForModify 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" $ authenticate ForModify (unlessNoEdit editPage showPage)
, dir "_diff" $ msum
[ showPageDiff
, guardPath isSourceCode >> showFileDiff ]
, dir "_discuss" discussPage
, dir "_delete" $ msum
[ methodOnly GET >>
authenticate ForModify (unlessNoDelete confirmDelete showPage)
, methodOnly POST >>
authenticate ForModify (unlessNoDelete deletePage showPage) ]
, dir "_preview" preview
, guardIndex >> indexPage
, guardCommand "export" >> exportPage
, methodOnly POST >> guardCommand "cancel" >> showPage
, methodOnly POST >> guardCommand "update" >>
authenticate ForModify (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 :: s -> UnWebT (ReaderT s IO) a -> UnWebT IO a
unpackReaderT st uw = runReaderT uw st