aboutsummaryrefslogtreecommitdiff
path: root/core/http/server.ts
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-06-29 14:25:29 +0200
committerlonkaars <loek@pipeframe.xyz>2023-06-29 14:25:29 +0200
commit3f7ae147fb969db479c10eaa871a840a30e281b3 (patch)
tree2e99c35f59f0effb260c26118e9edf3199bb8de7 /core/http/server.ts
parentc998e1c0477d51c886f9e4246e102dec4d7ef8dd (diff)
http proof of concept api
Diffstat (limited to 'core/http/server.ts')
-rw-r--r--core/http/server.ts39
1 files changed, 25 insertions, 14 deletions
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: () => { }
+ });
}
}