summaryrefslogtreecommitdiff
path: root/src/server/pgf-http.hs
blob: ff356c6e7d9dce926f16f315d32036852bd511fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

import Network.CGI(requestMethod,getVarWithDefault,logCGI,handleErrors,liftIO)
import System.Environment(getArgs)
import System.Directory(getDirectoryContents)
import System.FilePath(takeExtension,takeFileName,takeDirectory)
       
import RunHTTP(runHTTP)
import ServeStaticFile(serveStaticFile)
import PGFService(cgiMain',getPath,stderrToFile,logFile,newPGFCache)
import FastCGIUtils(outputJSONP,handleCGIErrors)

main :: IO ()
main = do stderrToFile logFile
          cache <- newPGFCache
          args <- getArgs
          port <- case args of
                    []     -> return 41296
                    [port] -> readIO port
          httpMain cache port

httpMain cache port = runHTTP port (do log ; serve =<< getPath)
  where
    log = do method <- requestMethod
             uri    <- getVarWithDefault "REQUEST_URI" "-"
             logCGI $ method++" "++uri

    serve path =
        handleErrors . handleCGIErrors $
        if takeExtension path==".pgf"
        then cgiMain' cache path
        else if takeFileName path=="grammars.cgi"
             then grammarList (takeDirectory path)
             else serveStaticFile path

    grammarList dir =
        do paths <- liftIO $ getDirectoryContents dir
           let pgfs = [path|path<-paths, takeExtension path==".pgf"]
           outputJSONP pgfs