Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update proxy.ex with more examples #478

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading