mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-23 18:02:54 -06:00
pgf service: external service hook: always format the output as a JSON string
This commit is contained in:
@@ -135,23 +135,19 @@ pgfMain pgf command = do
|
|||||||
doExternal Nothing input = throwCGIError 400 "Unknown external command" ["Unknown external command"]
|
doExternal Nothing input = throwCGIError 400 "Unknown external command" ["Unknown external command"]
|
||||||
doExternal (Just cmd) input =
|
doExternal (Just cmd) input =
|
||||||
do liftIO $ logError ("External command: "++cmd)
|
do liftIO $ logError ("External command: "++cmd)
|
||||||
cmds <- liftIO $ (readIO =<< readFile "external_services")
|
cmds <- liftIO $ (fmap lines $ readFile "external_services")
|
||||||
`catch` const (return [])
|
`catch` const (return [])
|
||||||
liftIO $ logError ("External services: "++show cmds)
|
liftIO $ logError ("External services: "++show cmds)
|
||||||
maybe err ok (lookup cmd cmds)
|
if cmd `elem` cmds then ok else err
|
||||||
where
|
where
|
||||||
err = throwCGIError 400 "Unknown external command" ["Unknown external command: "++cmd]
|
err = throwCGIError 400 "Unknown external command" ["Unknown external command: "++cmd]
|
||||||
ok output_type =
|
ok =
|
||||||
do let tmpfile1 = "external_input.txt"
|
do let tmpfile1 = "external_input.txt"
|
||||||
tmpfile2 = "external_output.txt"
|
tmpfile2 = "external_output.txt"
|
||||||
liftIO $ writeFile "external_input.txt" input
|
liftIO $ writeFile "external_input.txt" input
|
||||||
liftIO $ system $ cmd ++ " " ++ tmpfile1 ++ " > " ++ tmpfile2
|
liftIO $ system $ cmd ++ " " ++ tmpfile1 ++ " > " ++ tmpfile2
|
||||||
liftIO $ removeFile tmpfile1
|
liftIO $ removeFile tmpfile1
|
||||||
r <- case output_type of
|
r <- outputJSONP =<< liftIO (readFile tmpfile2)
|
||||||
"jsonp" -> outputEncodedJSONP =<< liftIO (readFile tmpfile2)
|
|
||||||
"image/png" -> outputPNG =<< liftIO (BS.readFile tmpfile2)
|
|
||||||
"text/html" -> outputHTML =<< liftIO (readFile tmpfile2)
|
|
||||||
_ -> outputPlain =<< liftIO (readFile tmpfile2)
|
|
||||||
liftIO $ removeFile tmpfile2
|
liftIO $ removeFile tmpfile2
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user