From 8f96f7668a465dc0c3cfefe7b0b5ff84ae57e0bd Mon Sep 17 00:00:00 2001 From: bgoosmanviz <78885081+bgoosmanviz@users.noreply.github.com> Date: Mon, 28 Oct 2024 03:09:41 -0400 Subject: [PATCH] Update proxy.ex with more examples (#478) --- lib/kino/proxy.ex | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/kino/proxy.ex b/lib/kino/proxy.ex index 1894ad7d..1b7eed2f 100644 --- a/lib/kino/proxy.ex +++ b/lib/kino/proxy.ex @@ -29,7 +29,6 @@ defmodule Kino.Proxy do Using the proxy feature, we can use Livebook apps to build APIs. For example, we could provide a data export endpoint: - Kino.Proxy.listen(fn %{path_info: ["export", "data"]} = conn -> data = "some data" @@ -70,6 +69,44 @@ defmodule Kino.Proxy do > end > end) > ``` + + ## Using Plug modules with Kino.Proxy + + You can also provide a module plug as an argument to `Kino.Proxy.listen/1`, + like this: + + defmodule MyPlug do + def init([]), do: false + + def call(conn, _opts) do + Plug.Conn.send_resp(conn, 200, "hello world!") + end + end + + Kino.Proxy.listen(MyPlug) + + Or a more complex example, using `Plug.Router` to handle multiple endpoints: + + defmodule ApiRouter do + use Plug.Router + + plug :match + plug :dispatch + + get "/hello" do + send_resp(conn, 200, "hello from router") + end + + get "/echo/:message" do + send_resp(conn, 200, String.upcase(message)) + end + + match _ do + send_resp(conn, 404, "oops, not found") + end + end + + Kino.Proxy.listen(ApiRouter) """ @type plug() ::