aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/http/client.ts33
-rw-r--r--core/http/server.ts39
-rw-r--r--core/http/types.ts25
3 files changed, 74 insertions, 23 deletions
diff --git a/core/http/client.ts b/core/http/client.ts
index 42d75f0..a77b616 100644
--- a/core/http/client.ts
+++ b/core/http/client.ts
@@ -1,7 +1,9 @@
-import { ParseDepth, ParseResult } from "../../language/types.ts";
-import YomikunError from "../../util/error.ts";
+import "../../util/array.ts";
+
+import { ParseResult } from "../../language/types.ts";
import API from "../api.ts";
import { ConnectionProps, ConnectionPropsDefault } from "./props.ts";
+import { APIRequest, APIRequestParseSentence, APIResponseParseSentence } from "./types.ts";
/**
* @summary Yomikun HTTP API
@@ -18,14 +20,27 @@ export default class YomikunRemoteAPIClient implements API {
async prepare() { }
- async parseSentence(input: string) {
- var response = await fetch(`http://${this.props.host}:${this.props.port}/parseSentence`);
- console.log(response.body);
+ private async request(details: APIRequest) {
+ var response = await fetch(`http://${this.props.host}:${this.props.port}`, {
+ method: "POST",
+ headers: {
+ "Accept": "application/json",
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(details),
+ });
+ return response.json();
+ }
- return {
- depth: ParseDepth.Term,
- tokens: [],
- } as ParseResult;
+ async parseSentence(input: string) {
+ var request: APIRequestParseSentence = {
+ command: "parseSentence",
+ options: {
+ input: input,
+ },
+ };
+ var { response } = await this.request(request) as APIResponseParseSentence;
+ return response;
}
}
diff --git a/core/http/server.ts b/core/http/server.ts
index 8a6786e..b5d6c13 100644
--- a/core/http/server.ts
+++ b/core/http/server.ts
@@ -1,33 +1,44 @@
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";
-interface Endpoint {
- endpoint: string;
-};
export default class YomikunRemoteAPIServer extends YomikunRAWAPI {
private props: ConnectionProps;
+ private handlers: Record<string, (req: APIRequest) => Promise<Response>> = {
+ 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 parseSentence(input: string) {
- return await super.parseSentence(input);
- }
-
async start() {
- serve((req) => {
- return new Response("Hello world!");
- }, { port: this.props.port });
- }
-
- async prepare() {
- await super.prepare();
+ 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: () => { }
+ });
}
}
diff --git a/core/http/types.ts b/core/http/types.ts
new file mode 100644
index 0000000..bbba1b5
--- /dev/null
+++ b/core/http/types.ts
@@ -0,0 +1,25 @@
+import { ParseResult } from "../../language/types.ts";
+
+export interface APIRequest {
+ command: string;
+ options: any;
+};
+
+export interface APIRequestParseSentence extends APIRequest {
+ command: "parseSentence";
+ options: {
+ input: string;
+ };
+};
+
+export interface APIResponse {
+ command: string;
+ response: any;
+ // final: boolean;
+};
+
+export interface APIResponseParseSentence extends APIResponse {
+ command: "parseSentence";
+ response: ParseResult;
+};
+