Gitit with 301 redirects

This code uses the Network.Gitit module to do everything to do with the Wiki and also contains my own handler to redirect URLs that contain "%20" to the canonical version. Many thanks to fiddlosopher/John who responded very quickly to my plea for help with a patch that enables this to work.

> import Network.Gitit
> import Happstack.Server.SimpleHTTP
> import Control.Monad
> import Data.List.Utils
> import Data.List
> import Data.Char (toLower)

Import the following modules to enable logging

> import System.Log.Logger (Priority(..), setLevel, setHandlers,
>         getLogger, saveGlobalLogger)
> import System.Log.Handler.Simple (fileHandler)
> 
> main = do

getConfigFromOpts means that a config file must be specified when you run the executable. Use something like "wiki -f config". The config file is in the same format as the one used for gitit.

>  conf <- getConfigFromOpts

The next bit does the logging. Copied straight from gitit.hs

>  let level = if debugMode conf then DEBUG else logLevel conf
>  logFileHandler <- fileHandler (logFile conf) level
>  serverLogger <- getLogger "Happstack.Server.AccessLog.Combined"
>  gititLogger <- getLogger "gitit"
>  saveGlobalLogger $ setLevel level $ setHandlers [logFileHandler] serverLogger
>  saveGlobalLogger $ setLevel level $ setHandlers [logFileHandler] gititLogger

Create directories if needed

>  createStaticIfMissing conf
>  createTemplateIfMissing conf
>  createRepoIfMissing conf
>  initializeGititState conf

redirects does the redirection so it needs to come before the wiki plugin

>  simpleHTTP nullConf{port = 5001} $ msum [redirects
> 					 ,wiki conf
> 					 ]
> redirects::ServerPartT IO Response
> redirects = do

The next two lines get the URI (or URL?) and convert is to a string

> 	rq <- askRq
> 	let ru = rqURL rq

Perform operations on this string and then check if it has changed

> let ru'= toLowerCase $ removeSpaces ru
> 	if (ru/=ru') then

Using "movedPermanently" is SEO best practice

> 	 movedPermanently (ru') (toResponse "")
> 	 else mzero

removeSpaces replaces spaces with hyphens. toLowerCase changes everything to lowercase

> removeSpaces = replace " " "-"
> 
> toLowerCase = map toLower

I also wanted to redirect /front-page to /. This doesn’t really work as I’d like since it makes it impossible to edit the front page

> redirectFrontPage ru = if (ru=="/Front-Page") then
> 			"/"
> 			else ru

Dimitry Golubovsky encourages me to look at the Gitit code and see if it is possible to do the changes there.