have a server that like does stuff now

This commit is contained in:
Peter Harpending
2025-10-11 02:07:20 -06:00
parent fa16da8178
commit 6c45f30919
8 changed files with 107 additions and 24 deletions
+34 -3
View File
@@ -263,11 +263,42 @@ default_css(Sock) ->
http_err(Sock, 500)
end.
wfcin(Sock, #request{enctype = json, body = #{"wfcin" := Input}}) ->
tell("wfcin good request: ~tp", [Input]),
RespObj =
try
case wfc_read:expr(Input) of
%% FIXME support multiple expressions
{ok, Expr, _Rest} ->
case wfc_eval:eval(Expr, wfc_eval_context:default()) of
{ok, noop, _NewContext} -> jsgud("<noop>");
{ok, Sentence, _NewContext} -> jsgud(wfc_pp:sentence(Sentence));
{error, Message} -> jsbad(Message)
end;
{error, Message} ->
jsbad(Message)
end
catch
error:E:S ->
ErrorMessage = unicode:characters_to_list(io_lib:format("parser crashed: ~p:~p", [E, S])),
jsbad(ErrorMessage)
end,
Body = zj:encode(RespObj),
Response = #response{headers = [{"content-type", "application/json"}],
body = Body},
respond(Sock, Response);
wfcin(Sock, Request) ->
tell("wfcin request: ~tp", [Request]),
http_err(Sock, 501).
tell("wfcin: bad request: ~tp", [Request]),
http_err(Sock, 400).
jsgud(X) ->
#{"ok" => true,
"result" => X}.
jsbad(X) ->
#{"ok" => false,
"error" => X}.
%% FIXME
http_err(Sock, N) ->
Slogan = qhl:slogan(N),
Body = ["<!doctype html>"