### Elixir Application Supervisor Setup Source: https://github.com/keathley/twirp-elixir/blob/master/README.md Sets up the Elixir application's supervisor to start the Cowboy web server, using the configured Twirp router to handle incoming requests. ```Elixir defmodule Example.Application do use Application def start(_type, _args) do children = [ Plug.Cowboy.child_spec(scheme: :http, plug: Example.Router, options: [port: 4040]), ] opts = [strategy: :one_for_one, name: Example.Supervisor] Supervisor.start_link(children, opts) end end ``` -------------------------------- ### Configure Twirp Client Adapter Source: https://github.com/keathley/twirp-elixir/blob/master/README.md Demonstrates how to start a Twirp client, specifying the server URL and configuring the underlying HTTP client adapter. Supports Finch (default) or Hackney, with options for connection pooling and timeouts. ```elixir Client.start_link( url: "https://some.url", adapter: {Twirp.Client.Hackney, [pool_opts: [timeout: 30_000, max_connections: 100]]} ) ``` -------------------------------- ### Elixir Twirp Client Usage Example Source: https://github.com/keathley/twirp-elixir/blob/master/README.md Demonstrates how to use a generated Twirp client in Elixir to make an RPC call, handling successful responses and Twirp errors. ```Elixir defmodule AnotherService.GetHats do alias Example.HaberdasherClient, as: Client alias Example.{Size, Hat} def make_a_hat(inches) do case Client.make_hat(Size.new(inches: inches)) do {:ok, %Hat{}=hat} -> hat {:error, %Twirp.Error{msg: msg}} -> Logger.error(msg) end end end ``` -------------------------------- ### Generated Elixir Service and Client Definitions Source: https://github.com/keathley/twirp-elixir/blob/master/README.md Elixir modules generated for the Twirp service definition and client stub, specifying package, service name, and RPC methods. ```Elixir defmodule Example.HaberdasherService do @moduledoc false use Twirp.Service package "example" service "Haberdasher" rpc :MakeHat, Example.Size, Example.Hat, :make_hat end defmodule Example.HaberdasherClient do @moduledoc false # client implementation... end ``` -------------------------------- ### Protobuf Service Definition Source: https://github.com/keathley/twirp-elixir/blob/master/README.md Defines a sample Haberdasher service using Protobuf syntax, including the service interface and message structures for requests and responses. ```Protobuf syntax "proto3"; package example; // Haberdasher service makes hats for clients. service Haberdasher { // MakeHat produces a hat of mysterious, randomly-selected color! rpc MakeHat(Size) returns (Hat); } // Size of a Hat, in inches. message Size { int32 inches = 1; // must be > 0 } // A Hat is a piece of headwear made by a Haberdasher. message Hat { int32 inches = 1; string color = 2; // anything but "invisible" string name = 3; // i.e. "bowler" } ``` -------------------------------- ### Protoc Code Generation Command Source: https://github.com/keathley/twirp-elixir/blob/master/README.md Command to generate Elixir code from a Protobuf file using protoc, specifying output directories for Elixir and Twirp generated files. ```Shell protoc --proto_path=./priv/protos --elixir_out=./lib/example --twirp_elixir_out=./lib/example ./priv/protos/service.proto ``` -------------------------------- ### Elixir Project Dependency Source: https://github.com/keathley/twirp-elixir/blob/master/README.md Add the Twirp Elixir library to your project's dependencies in `mix.exs`. This enables Twirp RPC functionality. ```Elixir def deps do [ {:twirp, "~> 0.8"} ] end ``` -------------------------------- ### Elixir Plug Router for Twirp Server Source: https://github.com/keathley/twirp-elixir/blob/master/README.md Configures a Plug.Router to serve Twirp RPC requests by attaching the generated service definition with its handler implementation. ```Elixir defmodule Example.Router do use Plug.Router plug Twirp.Plug, service: Example.HaberdasherService, handler: Example.HaberdasherHandler end ``` -------------------------------- ### Integrate Twirp with Phoenix Router Source: https://github.com/keathley/twirp-elixir/blob/master/README.md Shows how to forward Twirp requests within a Phoenix Router. This allows Twirp services to be exposed via specific URL paths, handling requests for a given service and handler. ```elixir defmodule ExampleWeb.Router do use ExampleWeb, :router scope "/rpc" do forward "/hat", Twirp.Plug, service: Example.HaberdasherService, handler: Example.HaberdasherHandler end end ``` -------------------------------- ### Twirp RPC Endpoint Structure Source: https://github.com/keathley/twirp-elixir/blob/master/README.md Defines the standard URL structure for Twirp RPC endpoints. Requests are typically routed through a prefix, followed by the Twirp package, service name, and the specific method being called. ```APIDOC URL Pattern: /{prefix?}/twirp/{package}.{service}/{method} Example: /rpc/hat/twirp/example.Haberdasher/MakeHat ``` -------------------------------- ### Generated Elixir Message Definitions Source: https://github.com/keathley/twirp-elixir/blob/master/README.md Elixir modules generated from Protobuf message definitions, using `Protobuf` macro for schema definition and struct creation. ```Elixir defmodule Example.Size do @moduledoc false use Protobuf, syntax: :proto3 @type t :: %__MODULE__{ inches: integer } defstruct [:inches] field :inches, 1, type: :int32 end defmodule Example.Hat do @moduledoc false use Protobuf, syntax: :proto3 @type t :: %__MODULE__{ inches: integer, color: String.t(), name: String.t() } defstruct [:inches, :color, :name] field :inches, 1, type: :int32 field :color, 2, type: :string field :name, 3, type: :string end ``` -------------------------------- ### Elixir Twirp Server Handler Implementation Source: https://github.com/keathley/twirp-elixir/blob/master/README.md An Elixir module implementing the Haberdasher service handler, defining the logic for the `make_hat` RPC method, including error handling for invalid input. ```Elixir defmodule Example.HaberdasherHandler do @colors ~w|white black brown red blue| @names ["bowler", "baseball cap", "top hat", "derby"] def make_hat(_ctx, size) do if size.inches <= 0 do Twirp.Error.invalid_argument("I can't make a hat that small!") else %Example.Hat{ inches: size.inches, color: Enum.random(@colors), name: Enum.random(@names) } end end end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.