summaryrefslogtreecommitdiff
path: root/src/server/MorphoService.hs
diff options
context:
space:
mode:
authorkrasimir <krasimir@chalmers.se>2009-06-04 16:26:52 +0000
committerkrasimir <krasimir@chalmers.se>2009-06-04 16:26:52 +0000
commitf2a968b6d53ac07df67f393bd06c38f368d08c02 (patch)
tree6cf785bcb1807de93f10452b11b0dd306157ec70 /src/server/MorphoService.hs
parent6a263f05aa8d963f7141aca8b7ee0cae0c063515 (diff)
the morpho server is now updated and works with the current GF. the sources are moved in directory server
Diffstat (limited to 'src/server/MorphoService.hs')
-rw-r--r--src/server/MorphoService.hs79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/server/MorphoService.hs b/src/server/MorphoService.hs
new file mode 100644
index 000000000..15590b356
--- /dev/null
+++ b/src/server/MorphoService.hs
@@ -0,0 +1,79 @@
+import GF.Compile
+import GF.Data.Operations
+import GF.Grammar.API
+import GF.Grammar.Parser
+import GF.Grammar.Grammar (Term)
+import GF.Grammar.PrGrammar (prTermTabular)
+import GF.Infra.Option
+import GF.Infra.UseIO
+import GF.Text.UTF8
+
+import Network.FastCGI
+import Text.JSON
+import qualified Codec.Binary.UTF8.String as UTF8 (encodeString)
+import Data.ByteString.Char8 as BS
+
+import Control.Monad
+import System.Environment
+import System.FilePath
+
+import Cache
+import FastCGIUtils
+import URLEncoding
+
+-- FIXME !!!!!!
+grammarFile :: FilePath
+grammarFile = "/usr/local/share/gf-3.0/lib/alltenses/ParadigmsFin.gfo"
+
+grammarPath :: FilePath
+grammarPath = "/usr/local/share/gf-3.0/lib/prelude"
+
+main :: IO ()
+main = do initFastCGI
+ r <- newCache readGrammar
+ loopFastCGI (handleErrors (handleCGIErrors (fcgiMain r)))
+
+fcgiMain :: Cache Grammar -> CGI CGIResult
+fcgiMain cache = liftIO (readCache cache grammarFile) >>= cgiMain
+
+readGrammar :: FilePath -> IO Grammar
+readGrammar file =
+ do let opts = concatOptions [modifyFlags $ \fs -> fs { optVerbosity = Quiet },
+ modifyFlags $ \fs -> fs { optLibraryPath = [grammarPath] }]
+ mgr <- appIOE $ batchCompile opts [file]
+ err (fail "Grammar loading error") return mgr
+
+cgiMain :: Grammar -> CGI CGIResult
+cgiMain sgr =
+ do path <- pathInfo
+ json <- case path of
+ "/eval" -> do mjson <- return (doEval sgr) `ap` getTerm
+ err (throwCGIError 400 "Evaluation error" . (:[])) return mjson
+ _ -> throwCGIError 404 "Not Found" ["Resource not found: " ++ path]
+ outputJSON json
+ where
+ getTerm :: CGI String
+ getTerm = do mt <- getInput "term"
+ maybe (throwCGIError 400 "No term given" ["No term given"]) (return . urlDecodeUnicode) mt
+
+doEval :: Grammar -> String -> Err JSValue
+doEval sgr t = liftM termToJSValue $ eval sgr t
+
+termToJSValue :: Term -> JSValue
+termToJSValue t = showJSON [toJSObject [("name", name), ("value",value)] | (name,value) <- prTermTabular t]
+
+eval :: Grammar -> String -> Err Term
+eval sgr t =
+ case runP pExp (BS.pack t) of
+ Right e -> checkTerm sgr e >>= computeTerm sgr
+ Left (_,msg) -> fail msg
+
+-- * General CGI and JSON stuff
+
+outputJSON :: JSON a => a -> CGI CGIResult
+outputJSON x = do setHeader "Content-Type" "text/json; charset=utf-8"
+ outputStrict $ UTF8.encodeString $ encode x
+
+outputStrict :: String -> CGI CGIResult
+outputStrict x | x == x = output x
+ | otherwise = fail "I am the pope."