module Config where import CGIUtils import DBConnect import HtmlView import MetaDataStore import HDBMetaDataStore import ImageInfo import Control.Monad import Control.Monad.Trans (MonadIO) import Data.Maybe import Network.URI import Network.NewCGI basedir = "../halbum" -- FIXME: make this configurable imgdir = basedir ++ "/images" -- FIXME: make these configurable defThumbSize :: (Int,Int) defThumbSize = (200,150) defViewSize :: (Int,Int) defViewSize = (640,480) data Config = Config { viewConfig :: ViewConfig, metaDataStore :: MetaDataStore } mkCallMe :: CGI ([(String,String)] -> String) mkCallMe = do sn <- liftM (fromMaybe "") $ getVar "SCRIPT_NAME" h <- getVar "HTTP_HOST" p <- liftM (fromMaybe "") $ getVar "SERVER_PORT" let s = if p == "443" then "https:" else "http:" -- FIXME: hacky port = if p == "80" || p == "443" then "" else ":" ++ p a = fmap (\x -> URIAuth { uriUserInfo = "", uriRegName = x, uriPort = port }) h is <- getInputs return (\xs -> let qs = formEncode $ tableUnion is xs in show $ nullURI { uriScheme = s, uriAuthority = a, uriPath = sn, uriQuery = if null qs then "" else "?" ++ qs }) dbConfig :: Bool -> Database -> CGI Config dbConfig admin db = do c <- mkCallMe return Config { viewConfig = ViewConfig { adminView = admin, cssUrl = \css -> basedir ++ "/" ++ css, scriptUrl = \script -> basedir ++ "/" ++ script, adminUrl = "halbumadmin.cgi", startUrl = "halbum.cgi", tagCompleteUrlPrefix = "halbum.cgi?action=completetag&prefix=", allTagsUrl = "?action=alltags", allImagesUrl = "?action=allimages", uploadImagesUrl = "?action=uploadimages", imageViewUrl = \id -> "?action=viewimage&image=" ++ showImageID id, tagViewUrl = \tag -> "?action=tagimages&tag=" ++ tag, callMe = c }, metaDataStore = hdbMetaDataStore db } withConfig :: Bool -> (Config -> CGI a) -> CGI a withConfig admin f = dbConnect admin (\db -> dbConfig admin db >>= f) runHalbum :: Bool -> (Config -> CGI CGIResult) -> IO () runHalbum admin f = runCGI (withConfig admin f `handleExceptionCGI` handleError)