module Rakka.Resource.Object ( resObject ) where import Network.HTTP.Lucu import Network.HTTP.Lucu.Utils import Rakka.Environment import Rakka.Page import Rakka.Storage import Rakka.SystemConfig import System.FilePath import System.Time resObject :: Environment -> ResourceDef resObject env = ResourceDef { resUsesNativeThread = False , resIsGreedy = True , resGet = Just $ getPathInfo >>= handleGet env . toPageName , resHead = Nothing , resPost = Nothing , resPut = Nothing , resDelete = Nothing } where toPageName :: [String] -> PageName toPageName = decodePageName . dropExtension . joinWith "/" handleGet :: Environment -> PageName -> Resource () handleGet env name = do pageM <- getPage (envStorage env) name case pageM of Nothing -> foundNoEntity Nothing Just redir@(Redirection _ _ _ _) -> handleRedirect env redir Just entity@(Entity _ _ _ _ _ _ _ _ _ _ _ _) -> handleGetEntity env entity {- HTTP/1.1 302 Found Location: http://example.org/object/Destination -} handleRedirect :: Environment -> Page -> Resource () handleRedirect env redir = do BaseURI baseURI <- getSysConf (envSysConf env) (BaseURI undefined) redirect Found (mkObjectURI baseURI $ redirName redir) {- HTTP/1.1 200 OK Content-Type: image/png ... -} handleGetEntity :: Environment -> Page -> Resource () handleGetEntity env page = do let lastMod = toClockTime $ pageLastMod page case pageRevision page of Nothing -> foundTimeStamp lastMod Just rev -> foundEntity (strongETag $ show rev) lastMod setContentType (pageType page) outputLBS (pageContent page)