Commit a804e212 authored by Massimo Costantini's avatar Massimo Costantini
Browse files

Refactored

parent 061768cd
Loading
Loading
Loading
Loading
+11 −31
Original line number Diff line number Diff line
@@ -21,40 +21,20 @@ import xmlbuilder from "xmlbuilder";
import { v4 as uuidv4 } from "uuid";

/**
 * Serves the static /tap HTML landing page.
 * Factory for serving static file endpoints.
 *
 * Functional signature: Request -> IO -> IO String
 * Functional signature: String -> (Request -> IO -> IO String)
 *
 * @param {Request} req - Incoming HTTP request
 * @returns {function(Symbol): Promise<string>} - HTML content (or throws on error)
 * @param {string} filePath - Path to the static file to serve.
 * @returns {function(Request): function(Symbol): Promise<string>} - A CPS endpoint function that returns the file content.
 */
const IVOA_tapEndpoint = (req) => async (IO) => {
  return await readFile("Stat/tap.html")(IO);
const staticEndpoint = (filePath) => {
  return (req) => async (IO) => await readFile(filePath)(IO);
};

/**
 * Serves the static /availability XML response.
 *
 * Functional signature: Request -> IO -> IO String
 *
 * @param {Request} req - Incoming HTTP request
 * @returns {function(Symbol): Promise<string>} - XML content in VOTable format or error message
 */
const IVOA_availabilityEndpoint = (req) => async (IO) => {
  return await readFile("Stat/availability.xml")(IO);
};

/**
 * Serves the static /capabilities XML response.
 *
 * Functional signature: Request -> IO -> IO String
 *
 * @param {Request} req - Incoming HTTP request
 * @returns {function(Symbol): Promise<string>} - XML content in VOTable format or error message
 */
const IVOA_capabilitiesEndpoint = (req) => async (IO) => {
  return await readFile("Stat/capabilities.xml")(IO);
};
const IVOA_tapEndpoint = staticEndpoint("Stat/tap.html");
const IVOA_availabilityEndpoint = staticEndpoint("Stat/availability.xml");
const IVOA_capabilitiesEndpoint = staticEndpoint("Stat/capabilities.xml");

/**
 * Executes an SQL query from file and returns a VOTable representation.
@@ -194,8 +174,8 @@ export function makeSyncError(message) {
 * @returns {Promise<void>} - A CPS IO action that starts the server and binds all routes
 */
const IVOA_main = () => async (IO) => {
  const port = await readRESTPort(IO);
  startRESTServer(port, [
  const port = await readRESTPort()(IO);
  await startRESTServer(port, [
    { path: "/tap", endpoint: IVOA_tapEndpoint },
    { path: "/tap/availability", endpoint: IVOA_availabilityEndpoint },
    { path: "/tap/capabilities", endpoint: IVOA_capabilitiesEndpoint },
+15 −45
Original line number Diff line number Diff line
@@ -13,65 +13,35 @@ from typing import Awaitable, Callable
from xml.dom import minidom

import asyncio
import json
import xml.etree.ElementTree as ET
import uuid


def ivoa_tap_endpoint(req) -> Callable[[object], Awaitable[str]]:
def static_endpoint(
    file_path: str,
) -> Callable[[object], Callable[[object], Awaitable[str]]]:
    """
    Serves the static /tap HTML landing page.
    Factory for serving static file endpoints.

    Functional signature: Request -> IO -> IO String
    Functional signature: String -> (Request -> IO -> IO String)

    Args:
        req: Incoming HTTP request.
        file_path: Path to the static file to serve.

    Returns:
        Callable[[object], Awaitable[str]]: A CPS IO action that returns HTML content (or raises on error).
        A CPS endpoint function that returns the file content.
    """

    def inner(io):
        return read_file("Stat/tap.html")(io)

    return inner

    def endpoint(req):
        return lambda io: read_file(file_path)(io)

def ivoa_availability_endpoint(req) -> Callable[[object], Awaitable[str]]:
    """
    Serves the static /availability XML response.
    return endpoint

    Functional signature: Request -> IO -> IO String

    Args:
        req: Incoming HTTP request.

    Returns:
        Callable[[object], Awaitable[str]]: A CPS IO action that returns XML content (or raises on error).
    """

    def inner(io):
        return read_file("Stat/availability.xml")(io)

    return inner


def ivoa_capabilities_endpoint(req) -> Callable[[object], Awaitable[str]]:
    """
    Serves the static /capabilities XML response.

    Functional signature: Request -> IO -> IO String

    Args:
        req: Incoming HTTP request.

    Returns:
        Callable[[object], Awaitable[str]]: A CPS IO action that returns XML content (or raises on error).
    """

    def inner(io):
        return read_file("Stat/capabilities.xml")(io)

    return inner
ivoa_tap_endpoint = static_endpoint("Stat/tap.html")
ivoa_availability_endpoint = static_endpoint("Stat/availability.xml")
ivoa_capabilities_endpoint = static_endpoint("Stat/capabilities.xml")


def ivoa_tables_endpoint(req) -> Callable[[object], Awaitable[str]]:
@@ -208,7 +178,7 @@ def ivoa_sync_endpoint(req) -> Callable[[object], Awaitable[str]]:
            await set_redis(redis)(
                {
                    "key": f"client:{client_id}:query:{row_uuid}",
                    "value": str({**row, "access_url": access_url}),
                    "value": json.dumps({**row, "access_url": access_url}),
                }
            )(io)

+2 −2
Original line number Diff line number Diff line
@@ -71,10 +71,10 @@ const makeWrapper = (handler) => {
export function startRESTServer(port, routes) {
  return async (IO) => {
    for (const route of routes) {
      app.get(route.path, makeWrapper(route.endpoint));
      app.all(route.path, makeWrapper(route.endpoint));
    }

    server = app.listen(config.port);
    server = app.listen(port);
    await putStr("REST server started")(IO);
    return { tag: "RESTActive", server };
  };
+1 −1
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ def start_rest_server(
            return wrapper

        for route in routes:
            app.router.add_route("GET", route["path"], make_wrapper(route["endpoint"]))
            app.router.add_route("*", route["path"], make_wrapper(route["endpoint"]))

        global server_runner
        runner = web.AppRunner(app)
+2 −2
Original line number Diff line number Diff line
@@ -38,8 +38,8 @@ export const buildVOTablesFromRows = ({ fields, rows }) => {
  }

  const tableset = xmlbuilder
    .create("vosi:tableset", { encoding: "UTF-8" })
    .att("xmlns:vosi", "http://www.ivoa.net/xml/VOSITables/v1.0")
    .create("tableset", { encoding: "UTF-8" })
    .att("xmlns", "http://www.ivoa.net/xml/VOSITables/v1.0")
    .att("xmlns:vod", "http://www.ivoa.net/xml/VODataService/v1.1")
    .att("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
    .att(