mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-21 00:52:51 -06:00
gf-server: New URL format: /grammar.pgf/command
This commit is contained in:
@@ -15,33 +15,32 @@ import Control.Monad
|
|||||||
import Data.Char
|
import Data.Char
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import System.Environment
|
|
||||||
|
|
||||||
|
|
||||||
defaultGrammarFile :: IO FilePath
|
|
||||||
defaultGrammarFile =
|
|
||||||
do env <- getEnvironment
|
|
||||||
return $ fromMaybe "grammar.pgf" $ lookup "PGF_FILE" env
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do initFastCGI
|
main = do initFastCGI
|
||||||
cache <- newCache PGF.readPGF
|
cache <- newCache PGF.readPGF
|
||||||
runFastCGIConcurrent' forkIO 100 (handleErrors (handleCGIErrors (fcgiMain cache)))
|
runFastCGIConcurrent' forkIO 100 (handleErrors (handleCGIErrors (cgiMain cache)))
|
||||||
|
|
||||||
fcgiMain :: Cache PGF -> CGI CGIResult
|
cgiMain :: Cache PGF -> CGI CGIResult
|
||||||
fcgiMain cache = liftIO (defaultGrammarFile >>= readCache cache) >>= cgiMain
|
cgiMain cache =
|
||||||
|
|
||||||
cgiMain :: PGF -> CGI CGIResult
|
|
||||||
cgiMain pgf =
|
|
||||||
do path <- pathInfo
|
do path <- pathInfo
|
||||||
json <- case path of
|
case filter (not . null) $ splitBy (=='/') path of
|
||||||
"/parse" -> return (doParse pgf) `ap` getText `ap` getCat `ap` getFrom
|
[file,command] -> do pgf <- liftIO $ readCache cache file
|
||||||
"/complete" -> return (doComplete pgf) `ap` getText `ap` getCat `ap` getFrom `ap` getLimit
|
json <- pgfMain pgf command
|
||||||
"/linearize" -> return (doLinearize pgf) `ap` getTree `ap` getTo
|
outputJSONP json
|
||||||
"/translate" -> return (doTranslate pgf) `ap` getText `ap` getCat `ap` getFrom `ap` getTo
|
_ -> throwCGIError 400 "Unknown resource" ["Unknown resource: " ++ show path,
|
||||||
"/grammar" -> return (doGrammar pgf) `ap` requestAcceptLanguage
|
"Use /grammar.pgf/command"]
|
||||||
_ -> throwCGIError 404 "Not Found" ["Resource not found: " ++ path]
|
|
||||||
outputJSONP json
|
pgfMain :: PGF -> String -> CGI JSValue
|
||||||
|
pgfMain pgf command =
|
||||||
|
case command of
|
||||||
|
"parse" -> return (doParse pgf) `ap` getText `ap` getCat `ap` getFrom
|
||||||
|
"complete" -> return (doComplete pgf) `ap` getText `ap` getCat `ap` getFrom `ap` getLimit
|
||||||
|
"linearize" -> return (doLinearize pgf) `ap` getTree `ap` getTo
|
||||||
|
"translate" -> return (doTranslate pgf) `ap` getText `ap` getCat `ap` getFrom `ap` getTo
|
||||||
|
"grammar" -> return (doGrammar pgf) `ap` requestAcceptLanguage
|
||||||
|
_ -> throwCGIError 400 "Unknown command" ["Unknown command: " ++ show command]
|
||||||
where
|
where
|
||||||
getText :: CGI String
|
getText :: CGI String
|
||||||
getText = liftM (maybe "" (urlDecodeUnicode . UTF8.decodeString)) $ getInput "input"
|
getText = liftM (maybe "" (urlDecodeUnicode . UTF8.decodeString)) $ getInput "input"
|
||||||
@@ -162,3 +161,11 @@ outputJSONP x =
|
|||||||
outputStrict :: String -> CGI CGIResult
|
outputStrict :: String -> CGI CGIResult
|
||||||
outputStrict x | x == x = output x
|
outputStrict x | x == x = output x
|
||||||
| otherwise = fail "I am the pope."
|
| otherwise = fail "I am the pope."
|
||||||
|
|
||||||
|
-- * General utilities
|
||||||
|
|
||||||
|
splitBy :: (a -> Bool) -> [a] -> [[a]]
|
||||||
|
splitBy _ [] = [[]]
|
||||||
|
splitBy f list = case break f list of
|
||||||
|
(first,[]) -> [first]
|
||||||
|
(first,_:rest) -> first : splitBy f rest
|
||||||
@@ -18,9 +18,11 @@ import java.util.ArrayList;
|
|||||||
public class GF {
|
public class GF {
|
||||||
|
|
||||||
private String baseURL;
|
private String baseURL;
|
||||||
|
private String pgfName;
|
||||||
|
|
||||||
public GF (String baseURL) {
|
public GF (String baseURL, String pgfName) {
|
||||||
this.baseURL = baseURL;
|
this.baseURL = baseURL;
|
||||||
|
this.pgfName = pgfName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static interface GFCallback<T extends JavaScriptObject> {
|
public static interface GFCallback<T extends JavaScriptObject> {
|
||||||
@@ -155,7 +157,7 @@ public class GF {
|
|||||||
/* Utilities */
|
/* Utilities */
|
||||||
|
|
||||||
private <T extends JavaScriptObject> GFRequest sendRequest (String resource, List<Arg> vars, final GFCallback<T> callback) {
|
private <T extends JavaScriptObject> GFRequest sendRequest (String resource, List<Arg> vars, final GFCallback<T> callback) {
|
||||||
String url = baseURL + "/" + resource + "?" + buildQueryString(vars);
|
String url = baseURL + "/" + pgfName + "/" + resource + "?" + buildQueryString(vars);
|
||||||
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
|
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
|
||||||
builder.setTimeoutMillis(30000);
|
builder.setTimeoutMillis(30000);
|
||||||
builder.setHeader("Accept","text/plain, text/html;q=0.5, */*;q=0.1");
|
builder.setHeader("Accept","text/plain, text/html;q=0.5, */*;q=0.1");
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import java.util.Set;
|
|||||||
public class Translate implements EntryPoint {
|
public class Translate implements EntryPoint {
|
||||||
|
|
||||||
private static final String gfBaseURL = "/pgf";
|
private static final String gfBaseURL = "/pgf";
|
||||||
|
private static final String pgfName = "grammar.pgf";
|
||||||
|
|
||||||
private GF gf;
|
private GF gf;
|
||||||
|
|
||||||
@@ -195,7 +196,7 @@ public class Translate implements EntryPoint {
|
|||||||
statusPopup.add(statusLabel);
|
statusPopup.add(statusLabel);
|
||||||
statusPopup.center();
|
statusPopup.center();
|
||||||
|
|
||||||
gf = new GF(gfBaseURL);
|
gf = new GF(gfBaseURL, pgfName);
|
||||||
|
|
||||||
createTranslationUI();
|
createTranslationUI();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user