Skip to content

Commit

Permalink
Update proxy.ex with more examples (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoosmanviz authored Oct 28, 2024
1 parent 1f63f90 commit 8f96f76
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion lib/kino/proxy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() ::
Expand Down

0 comments on commit 8f96f76

Please sign in to comment.