caching works correctly

This commit is contained in:
Peter Harpending
2025-10-11 17:24:11 -06:00
parent a942d79869
commit 673bc3ae52
5 changed files with 172 additions and 17 deletions
+28 -10
View File
@@ -263,34 +263,52 @@ default_css(Sock) ->
http_err(Sock, 500)
end.
wfcin(Sock, #request{enctype = json, body = #{"wfcin" := Input}}) ->
wfcin(Sock, #request{enctype = json,
cookies = Cookies,
body = #{"wfcin" := Input}}) ->
tell("wfcin good request: ~tp", [Input]),
RespObj =
{Cookie, Ctx0} = ctx(Cookies),
{RespObj, NewCtx} =
%% FIXME: this should really be a new process
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)
case wfc_eval:eval(Expr, Ctx0) of
{ok, noop, Ctx1} -> {jsgud("<noop>"), Ctx1};
{ok, Sentence, Ctx1} -> {jsgud(wfc_pp:sentence(Sentence)), Ctx1};
{error, Message} -> {jsbad(Message), Ctx0}
end;
{error, Message} ->
jsbad(Message)
{jsbad(Message), Ctx0}
end
catch
error:E:S ->
ErrorMessage = unicode:characters_to_list(io_lib:format("parser crashed: ~p:~p", [E, S])),
jsbad(ErrorMessage)
{jsbad(ErrorMessage), Ctx0}
end,
% update cache with new context
ok = fd_cache:set(Cookie, NewCtx),
Body = zj:encode(RespObj),
Response = #response{headers = [{"content-type", "application/json"}],
Response = #response{headers = [{"content-type", "application/json"},
{"set-cookie", ["wfc=", Cookie]}],
body = Body},
respond(Sock, Response);
wfcin(Sock, Request) ->
tell("wfcin: bad request: ~tp", [Request]),
http_err(Sock, 400).
ctx(#{<<"wfc">> := Cookie}) ->
case fd_cache:query(Cookie) of
{ok, Context} -> {Cookie, Context};
error -> {Cookie, wfc_eval_context:default()}
end;
ctx(_) ->
{new_cookie(), wfc_eval_context:default()}.
new_cookie() ->
binary:encode_hex(crypto:strong_rand_bytes(8)).
jsgud(X) ->
#{"ok" => true,
"result" => X}.