let serve _ r w =
let rec loop r w =
Reader.read_line r >>= function
| `Eof -> return ()
| `Ok s -> begin
match String.split ~on:' ' s with
| ["GET"; k] -> begin
match Hashtbl.find store k with
| None -> (Writer.write_line w ("Error: key " ^ k ^ " not found"); loop r w)
| Some v -> (Writer.write_line w v; loop r w)
end
| ["SET"; k; v] -> begin
Hashtbl.replace store ~key:k ~data:v;
Writer.write_line w "OK";
loop r w
end
| _ -> (Writer.write_line w ("Error: invalid request " ^ s); loop r w)
end
in
loop r w