Search in sources :

Example 1 with IScriptingInterface

use of gaiasky.script.IScriptingInterface in project gaiasky by langurmonkey.

the class RESTServer method initialize.

/**
 * Initialize the REST server.
 * <p>
 * Sets the routes and then passes the call to the handler.
 *
 * @param rest_port The port to use for the REST server
 */
public static void initialize(Integer rest_port) {
    /* Check for valid TCP port (otherwise considered as "disabled") */
    port = rest_port;
    printStartupInfo();
    if (port < 0) {
        logger.error("Error: invalid port. REST API inactive.");
        return;
    }
    try {
        logger.info("Starting REST API server on http://localhost:{}/api/", port);
        logger.info("   See available calls at http://localhost:{}/api/help", port);
        port(port);
        logger.info("Setting routes");
        /*
             * Static file location:
             * add static HTML files with API use examples.
             * Note: this logs the value of spark.staticfiles.StaticFilesFolder
             * on warn level for information.
             */
        staticFiles.externalLocation(rest_static_location);
        /* Scripting API mapping */
        get("/api", (request, response) -> {
            response.redirect("/api/help");
            return response;
        });
        get("/api/:cmd", RESTServer::handleApiCall);
        post("/api/:cmd", RESTServer::handleApiCall);
        /* Initialize method index */
        // get set of permitted API commands
        Class<IScriptingInterface> iScriptingInterfaceClass = IScriptingInterface.class;
        Method[] allMethods = iScriptingInterfaceClass.getDeclaredMethods();
        methodMap = new HashMap<>();
        for (Method method : allMethods) {
            Array<Method> matches;
            if (methodMap.containsKey(method.getName())) {
                matches = methodMap.get(method.getName());
            } else {
                matches = new Array<>(false, 1);
            }
            if (!matches.contains(method, true))
                matches.add(method);
            methodMap.put(method.getName(), matches);
        }
        logger.info("Startup finished.");
    } catch (Exception e) {
        logger.error(e, "Caught an exception during initialization:");
    }
}
Also used : IScriptingInterface(gaiasky.script.IScriptingInterface) Method(java.lang.reflect.Method)

Aggregations

IScriptingInterface (gaiasky.script.IScriptingInterface)1 Method (java.lang.reflect.Method)1