import { serve } from "https://deno.land/std@0.192.0/http/server.ts"; import "../../util/string.ts"; import { ParseResult } from "../../language/types.ts"; import YomikunRAWAPI from "../raw/api.ts"; import { ConnectionProps, ConnectionPropsDefault } from "./props.ts"; import { APIRequest, APIRequestParseSentence, APIResponseParseSentence } from "./types.ts"; export default class YomikunRemoteAPIServer extends YomikunRAWAPI { private props: ConnectionProps; private handlers: Record Promise> = { parseSentence: async _req => { var req = _req as APIRequestParseSentence; var input = req.options?.input if (!input) return new Response("", { status: 404 }); return new Response(JSON.stringify({ command: "parseSentence", response: await this.parseSentence(input), } as APIResponseParseSentence)); }, }; constructor(options?: ConnectionProps) { super(); this.props = { ...ConnectionPropsDefault, ...options }; } async start() { serve(async (req) => { if (req.method != "POST") return new Response("", { status: 400 }); // wrong request (not post) var request = (await req.text()).json({}) as APIRequest; if (!request.command) return new Response("", { status: 400 }); // wrong request (no command) var handler = this.handlers[request.command]; if (!handler) return new Response("", { status: 404 }); // not found (unknown command) return await handler(request); }, { port: this.props.port, onListen: () => { } }); } }