From 89140cb2a528f672b7a1c71b2028a0812dc492e2 Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 9 May 2024 18:52:15 +0200 Subject: [PATCH] Refactor the webserver into r2ai.web and more options --- r2ai/interpreter.py | 3 +++ r2ai/repl.py | 38 +++----------------------------------- r2ai/web.py | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 r2ai/web.py diff --git a/r2ai/interpreter.py b/r2ai/interpreter.py index eb842bb7..07ea9683 100644 --- a/r2ai/interpreter.py +++ b/r2ai/interpreter.py @@ -564,6 +564,9 @@ def __init__(self): self.env["data.hist"] = "false" self.env["key.mastodon"] = "" self.env["key.openai"] = "" + self.env["http.port"] = "8080" + self.env["http.path"] = "" + self.env["http.chatctx"] = "false" if have_rlang: self.env["chat.live"] = "false" else: diff --git a/r2ai/repl.py b/r2ai/repl.py index 3a7adf4d..dce358ba 100644 --- a/r2ai/repl.py +++ b/r2ai/repl.py @@ -4,6 +4,7 @@ import traceback have_readline = False from .const import R2AI_HISTFILE, R2AI_HOMEDIR, R2AI_RCFILE, R2AI_USERDIR +from .web import start_http_server import r2ai import sys import os @@ -34,40 +35,6 @@ def r2_cmd(x): r2.cmd('e scr.color=' + oc) return res -# TODO : move into r2ai/http.py -def start_http_server(ai): - import http.server - import socketserver - - PORT = 8000 - BASEPATH = "" - - Handler = http.server.SimpleHTTPRequestHandler - - class SimpleHTTPRequestHandler(Handler): - def do_GET(self): - self.send_response(404) - self.end_headers() - self.wfile.write(bytes(f'Invalid request. Use POST and /{BASEPATH}', 'utf-8')) - def do_POST(self): - if self.path.startswith(BASEPATH): - content_length = int(self.headers['Content-Length']) - msg = self.rfile.read(content_length).decode('utf-8') - self.send_response(200) - self.end_headers() - res = runline2(ai, msg) - self.wfile.write(bytes(f'{res}','utf-8')) - else: - self.send_response(404) - self.end_headers() - self.wfile.write(bytes(f'Invalid request. Use {BASEPATH}')) - - Handler.protocol_version = "HTTP/1.0" - server = socketserver.TCPServer(("", PORT), SimpleHTTPRequestHandler) - server.allow_reuse_address = True - server.allow_reuse_port = True - print("Serving at port", PORT) - server.serve_forever() help_message = """Usage: r2ai [-option] ([query] | [script.py]) r2ai . [file] interpret r2ai script with access to globals @@ -266,8 +233,9 @@ def runline(ai, usertext): print("Invalid config key", file=sys.stderr) pass elif usertext.startswith("-w"): - start_http_server(ai) + start_http_server(ai, runline2) elif usertext.startswith("-s"): + ai.runline2 = runline2 r2ai_repl(ai) elif usertext.startswith("-rf"): if len(usertext) > 2: diff --git a/r2ai/web.py b/r2ai/web.py new file mode 100644 index 00000000..10a1fd12 --- /dev/null +++ b/r2ai/web.py @@ -0,0 +1,37 @@ +def start_http_server(ai, runline2): + import http.server + import socketserver + + WANTCTX = ai.env["http.chatctx"] == "true" + PORT = int(ai.env["http.port"]) + BASEPATH = ai.env["http.path"] + + Handler = http.server.SimpleHTTPRequestHandler + + class SimpleHTTPRequestHandler(Handler): + def do_GET(self): + self.send_response(404) + self.end_headers() + self.wfile.write(bytes(f'Invalid request. Use POST and /{BASEPATH}', 'utf-8')) + def do_POST(self): + if self.path.startswith(BASEPATH): + content_length = int(self.headers['Content-Length']) + msg = self.rfile.read(content_length).decode('utf-8') + self.send_response(200) + self.end_headers() + if WANTCTX: + runline2(ai, "-R") + res = runline2(ai, msg) + self.wfile.write(bytes(f'{res}','utf-8')) + else: + self.send_response(404) + self.end_headers() + self.wfile.write(bytes(f'Invalid request. Use {BASEPATH}')) + + print("[r2ai] Serving at port", PORT) + Handler.protocol_version = "HTTP/1.0" + server = socketserver.TCPServer(("", PORT), SimpleHTTPRequestHandler) + server.allow_reuse_address = True + server.allow_reuse_port = True + server.serve_forever() +