Permalink
Browse files

Added expireGititCache program.

  • Loading branch information...
1 parent 586399e commit 4b21b6c5af81efdcd4c5cef24a26b8a09a51880d @jgm committed Aug 12, 2009
Showing with 90 additions and 1 deletion.
  1. +15 −1 README.markdown
  2. +66 −0 expireGititCache.hs
  3. +9 −0 gitit.cabal
View
@@ -203,7 +203,21 @@ the name of the page to be expired.
Users who frequently update pages using git or darcs may wish
to set `use-cache` to `no`, or alternatively to add a hook to
the repository that makes the appropriate HTTP request to expire
-pages when they are updated.
+pages when they are updated. To facilitate such hooks, the gitit
+cabal package includes an executable `expireGititCache`. Assuming
+you are running gitit at port 5001 on localhost, and the
+environment variable `CHANGED_FILES` contains a list of the files
+that have changed, you can expire their cached versions using
+
+ expireGititCache http://localhost:5001 $CHANGED_FILES
+
+Or you can specify the files directly:
+
+ expireGititCache http://localhost:5001 "Front Page.page" foo/bar/baz.c
+
+This program will return a success status (0) if the page has been
+successfully expired (or if it was never cached in the first place),
+and a failure status (> 0) otherwise.
Configuring gitit
=================
View
@@ -0,0 +1,66 @@
+{-
+expireGititCache - (C) 2009 John MacFarlane, licensed under the GPL
+
+This program is designed to be used in post-update hooks and other scripts.
+
+Usage: expireGititCache base-url [file..]
+
+Example:
+
+ expireGititCache http://localhost:5001 page1.page foo/bar.hs "Front Page.page"
+
+will produce POST requests to http://localhost:5001/_expire/page1,
+http://localhost:5001/_expire/foo/bar.hs, and
+http://localhost:5001/_expire/Front Page.
+
+Return statuses:
+
+0 -> the cached page was successfully expired (or was not cached in the first place)
+1 -> fewer than two arguments were supplied
+3 -> did not receive a 200 OK response from the request
+5 -> could not parse the uri
+
+-}
+
+module Main
+where
+import Network.HTTP
+import System.Environment
+import Network.URI
+import System.FilePath
+import Control.Monad
+import System.IO
+import System.Exit
+
+main :: IO ()
+main = do
+ args <- getArgs
+ (uriString : files) <- if length args < 2
+ then usageMessage >> return [""]
+ else return args
+ uri <- case parseURI uriString of
+ Just u -> return u
+ Nothing -> do
+ hPutStrLn stderr ("Could not parse URI " ++ uriString)
+ exitWith (ExitFailure 5)
+ forM_ files (expireFile uri)
+
+usageMessage :: IO ()
+usageMessage = do
+ hPutStrLn stderr $ "Usage: expireGititCache base-url [file..]\n" ++
+ "Example: expireGititCache http://localhost:5001 page1.page foo/bar.hs"
+ exitWith (ExitFailure 1)
+
+expireFile :: URI -> FilePath -> IO ()
+expireFile uri file = do
+ let path' = if takeExtension file == ".page"
+ then dropExtension file
+ else file
+ let uri' = uri{uriPath = "/_expire/" ++ urlEncode path'}
+ resResp <- simpleHTTP Request{rqURI = uri', rqMethod = POST, rqHeaders = [], rqBody = ""}
+ case resResp of
+ Left connErr -> error $ show connErr
+ Right (Response (2,0,0) _ _ _) -> return ()
+ _ -> do
+ hPutStrLn stderr ("Request for " ++ show uri' ++ " did not return success status")
+ exitWith (ExitFailure 3)
View
@@ -96,3 +96,12 @@ Executable gitit
ghc-options: -Wall -threaded
ghc-prof-options: -auto-all -caf-all
+Executable expireGititCache
+ hs-source-dirs: .
+ main-is: expireGititCache.hs
+ build-depends: base >=3 && < 5, HTTP, url, filepath
+ if impl(ghc >= 6.10)
+ build-depends: base >= 4, syb
+ ghc-options: -Wall -threaded
+ ghc-prof-options: -auto-all -caf-all
+

0 comments on commit 4b21b6c

Please sign in to comment.