|
| 1 | +defmodule Hyper.Grpc do |
| 2 | + @moduledoc """ |
| 3 | + Public gRPC interface to a Hyper cluster. |
| 4 | +
|
| 5 | + The service contract is `hyper.grpc.v0.Hyper` (see |
| 6 | + `proto/hyper/grpc/v0/hyper.proto`). Any gRPC client, in any language, can |
| 7 | + create, stop, locate, and list microVMs. Off-BEAM clients generate their own |
| 8 | + stubs from the `.proto`; BEAM clients can use the generated |
| 9 | + `Hyper.Grpc.V0.Hyper.Stub` together with `connect/2`. |
| 10 | + """ |
| 11 | + |
| 12 | + defmodule Config do |
| 13 | + @moduledoc """ |
| 14 | + gRPC server configuration, read from application env into a struct: |
| 15 | +
|
| 16 | + config :hyper, Hyper.Grpc.Config, |
| 17 | + enabled: true, |
| 18 | + port: 50_051, |
| 19 | + cred: GRPC.Credential.new(ssl: [certfile: "/path/cert.pem", keyfile: "/path/key.pem"]) |
| 20 | +
|
| 21 | + Fields: |
| 22 | +
|
| 23 | + * `:enabled` -- whether the server starts. Defaults to `false`. |
| 24 | + * `:port` -- the listen port. Defaults to `50051`. |
| 25 | + * `:cred` -- a `GRPC.Credential` for TLS, or `nil` (the default) for |
| 26 | + plaintext. |
| 27 | + * `:adapter_opts` -- options forwarded to the server adapter, e.g. |
| 28 | + `[ip: {0, 0, 0, 0}]`. |
| 29 | +
|
| 30 | + Build the credential where you load your keys (e.g. `config/runtime.exs`); |
| 31 | + Hyper never reads the filesystem on your behalf. |
| 32 | +
|
| 33 | + > #### Co-located nodes {: .info} |
| 34 | + > |
| 35 | + > Every node binds `:port`. Running multiple nodes on one host (e.g. a local |
| 36 | + > cluster) requires giving each a distinct port via its own config. |
| 37 | + """ |
| 38 | + |
| 39 | + @default_port 50_051 |
| 40 | + |
| 41 | + defstruct enabled: false, port: @default_port, cred: nil, adapter_opts: [] |
| 42 | + |
| 43 | + @type t :: %__MODULE__{ |
| 44 | + enabled: boolean(), |
| 45 | + port: :inet.port_number(), |
| 46 | + cred: GRPC.Credential.t() | nil, |
| 47 | + adapter_opts: keyword() |
| 48 | + } |
| 49 | + |
| 50 | + @doc "Load the gRPC server configuration from application env." |
| 51 | + @spec load() :: t() |
| 52 | + def load, do: struct!(__MODULE__, Application.get_env(:hyper, __MODULE__, [])) |
| 53 | + |
| 54 | + @doc """ |
| 55 | + The `GRPC.Server.Supervisor` options for this config: the endpoint and port, |
| 56 | + plus the TLS credential and adapter options when set. |
| 57 | + """ |
| 58 | + @spec server_options(t()) :: keyword() |
| 59 | + def server_options(%__MODULE__{} = config) do |
| 60 | + [endpoint: Hyper.Grpc.Endpoint, port: config.port, start_server: true] |
| 61 | + |> put_unless(:cred, config.cred, nil) |
| 62 | + |> put_unless(:adapter_opts, config.adapter_opts, []) |
| 63 | + end |
| 64 | + |
| 65 | + @spec put_unless(keyword(), atom(), term(), term()) :: keyword() |
| 66 | + defp put_unless(opts, _key, skip, skip), do: opts |
| 67 | + defp put_unless(opts, key, value, _skip), do: Keyword.put(opts, key, value) |
| 68 | + end |
| 69 | + |
| 70 | + @doc """ |
| 71 | + The gRPC server's supervisor child, or `[]` when the server is disabled (the |
| 72 | + default). Spliced into the app supervision tree by `Hyper.Application`. |
| 73 | + """ |
| 74 | + @spec server_children() :: [{module(), keyword()}] |
| 75 | + def server_children do |
| 76 | + config = Config.load() |
| 77 | + |
| 78 | + if config.enabled do |
| 79 | + [{GRPC.Server.Supervisor, Config.server_options(config)}] |
| 80 | + else |
| 81 | + [] |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments