From 9ad9f919f0cf86023d6b2fec15c3c5c6441b1788 Mon Sep 17 00:00:00 2001 From: Thanabodee Charoenpiriyakij Date: Fri, 22 Jul 2022 23:09:07 +0700 Subject: [PATCH] refactor: revise route_guide example * Change supervisor declration in tree. * Cleanup dependencies. * Use jason instead of poison. * Auto start server. * Move route_guide.proto to priv/protos. --- examples/route_guide/config/config.exs | 2 +- examples/route_guide/lib/app.ex | 6 +- examples/route_guide/lib/data.ex | 10 ++- examples/route_guide/lib/route_guide.pb.ex | 78 +++++++------------ examples/route_guide/mix.exs | 9 +-- examples/route_guide/mix.lock | 7 +- examples/route_guide/priv/client.exs | 3 +- .../priv/{ => protos}/route_guide.proto | 0 8 files changed, 47 insertions(+), 68 deletions(-) rename examples/route_guide/priv/{ => protos}/route_guide.proto (100%) diff --git a/examples/route_guide/config/config.exs b/examples/route_guide/config/config.exs index 0234afa9..28aeed1a 100644 --- a/examples/route_guide/config/config.exs +++ b/examples/route_guide/config/config.exs @@ -1,3 +1,3 @@ import Config -# config :grpc, start_server: true +config :grpc, start_server: true diff --git a/examples/route_guide/lib/app.ex b/examples/route_guide/lib/app.ex index 5410d767..035d24cb 100644 --- a/examples/route_guide/lib/app.ex +++ b/examples/route_guide/lib/app.ex @@ -5,11 +5,9 @@ defmodule Routeguide.App do @key_path Path.expand("./tls/server1.key", :code.priv_dir(:route_guide)) def start(_type, _args) do - import Supervisor.Spec - children = [ - supervisor(RouteGuide.Data, []), - supervisor(GRPC.Server.Supervisor, [start_args()]) + RouteGuide.Data, + {GRPC.Server.Supervisor, start_args()} ] opts = [strategy: :one_for_one, name: Routeguide] diff --git a/examples/route_guide/lib/data.ex b/examples/route_guide/lib/data.ex index 11cf0343..7d1fe528 100644 --- a/examples/route_guide/lib/data.ex +++ b/examples/route_guide/lib/data.ex @@ -1,7 +1,9 @@ defmodule RouteGuide.Data do + use Agent + @json_path Path.expand("../priv/route_guide_db.json", __DIR__) - def start_link do + def start_link(_) do features = load_features() Agent.start_link(fn -> %{features: features, notes: %{}} end, name: __MODULE__) end @@ -20,13 +22,13 @@ defmodule RouteGuide.Data do defp load_features(path \\ @json_path) do data = File.read!(path) - items = Poison.Parser.parse!(data) + items = Jason.decode!(data) - Enum.map(items, fn %{"location" => location, "name" => name} -> + for %{"location" => location, "name" => name} <- items do point = Routeguide.Point.new(latitude: location["latitude"], longitude: location["longitude"]) Routeguide.Feature.new(name: name, location: point) - end) + end end end diff --git a/examples/route_guide/lib/route_guide.pb.ex b/examples/route_guide/lib/route_guide.pb.ex index f9ecee43..66a64c91 100644 --- a/examples/route_guide/lib/route_guide.pb.ex +++ b/examples/route_guide/lib/route_guide.pb.ex @@ -1,81 +1,59 @@ defmodule Routeguide.Point do - use Protobuf + @moduledoc false + use Protobuf, protoc_gen_elixir_version: "0.10.0", syntax: :proto3 - @type t :: %__MODULE__{ - latitude: integer(), - longitude: integer() - } - defstruct [:latitude, :longitude] - - field :latitude, 1, optional: true, type: :int32 - field :longitude, 2, optional: true, type: :int32 + field :latitude, 1, type: :int32 + field :longitude, 2, type: :int32 end defmodule Routeguide.Rectangle do - use Protobuf - - @type t :: %__MODULE__{ - lo: Routeguide.Point.t(), - hi: Routeguide.Point.t() - } - defstruct [:lo, :hi] + @moduledoc false + use Protobuf, protoc_gen_elixir_version: "0.10.0", syntax: :proto3 - field :lo, 1, optional: true, type: Routeguide.Point - field :hi, 2, optional: true, type: Routeguide.Point + field :lo, 1, type: Routeguide.Point + field :hi, 2, type: Routeguide.Point end defmodule Routeguide.Feature do - use Protobuf + @moduledoc false + use Protobuf, protoc_gen_elixir_version: "0.10.0", syntax: :proto3 - @type t :: %__MODULE__{ - name: String.t(), - location: Routeguide.Point.t() - } - defstruct [:name, :location] - - field :name, 1, optional: true, type: :string - field :location, 2, optional: true, type: Routeguide.Point + field :name, 1, type: :string + field :location, 2, type: Routeguide.Point end defmodule Routeguide.RouteNote do - use Protobuf - - @type t :: %__MODULE__{ - location: Routeguide.Point.t(), - message: String.t() - } - defstruct [:location, :message] + @moduledoc false + use Protobuf, protoc_gen_elixir_version: "0.10.0", syntax: :proto3 - field :location, 1, optional: true, type: Routeguide.Point - field :message, 2, optional: true, type: :string + field :location, 1, type: Routeguide.Point + field :message, 2, type: :string end defmodule Routeguide.RouteSummary do - use Protobuf + @moduledoc false + use Protobuf, protoc_gen_elixir_version: "0.10.0", syntax: :proto3 - @type t :: %__MODULE__{ - point_count: integer(), - feature_count: integer(), - distance: integer(), - elapsed_time: integer() - } - defstruct [:point_count, :feature_count, :distance, :elapsed_time] - - field :point_count, 1, optional: true, type: :int32 - field :feature_count, 2, optional: true, type: :int32 - field :distance, 3, optional: true, type: :int32 - field :elapsed_time, 4, optional: true, type: :int32 + field :point_count, 1, type: :int32, json_name: "pointCount" + field :feature_count, 2, type: :int32, json_name: "featureCount" + field :distance, 3, type: :int32 + field :elapsed_time, 4, type: :int32, json_name: "elapsedTime" end defmodule Routeguide.RouteGuide.Service do - use GRPC.Service, name: "routeguide.RouteGuide" + @moduledoc false + use GRPC.Service, name: "routeguide.RouteGuide", protoc_gen_elixir_version: "0.10.0" rpc :GetFeature, Routeguide.Point, Routeguide.Feature + rpc :ListFeatures, Routeguide.Rectangle, stream(Routeguide.Feature) + rpc :RecordRoute, stream(Routeguide.Point), Routeguide.RouteSummary + rpc :RouteChat, stream(Routeguide.RouteNote), stream(Routeguide.RouteNote) end defmodule Routeguide.RouteGuide.Stub do + @moduledoc false use GRPC.Stub, service: Routeguide.RouteGuide.Service end diff --git a/examples/route_guide/mix.exs b/examples/route_guide/mix.exs index 78b43b4b..045792a6 100644 --- a/examples/route_guide/mix.exs +++ b/examples/route_guide/mix.exs @@ -4,7 +4,7 @@ defmodule RouteGuide.Mixfile do def project do [app: :route_guide, version: "0.1.0", - elixir: "~> 1.3", + elixir: "~> 1.11", build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, deps: deps()] @@ -15,7 +15,7 @@ defmodule RouteGuide.Mixfile do # Type "mix help compile.app" for more information def application do [mod: {Routeguide.App, []}, - applications: [:logger, :grpc, :poison]] + applications: [:logger, :grpc, :protobuf, :jason]] end # Dependencies can be Hex packages: @@ -31,9 +31,8 @@ defmodule RouteGuide.Mixfile do [ {:grpc, path: "../../"}, {:protobuf, "~> 0.10"}, - {:poison, "~> 3.0"}, - {:cowlib, "~> 2.8.0", hex: :grpc_cowlib, override: true}, - {:dialyxir, "~> 0.5", only: [:dev, :test], runtime: false}, + {:jason, "~> 1.2"}, + {:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false}, ] end end diff --git a/examples/route_guide/mix.lock b/examples/route_guide/mix.lock index 46d99448..c7f7f964 100644 --- a/examples/route_guide/mix.lock +++ b/examples/route_guide/mix.lock @@ -1,9 +1,10 @@ %{ "cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"}, - "cowlib": {:hex, :grpc_cowlib, "2.8.1", "ddaf77f3b89bd8e6c76df67b28a4b069688eef91c0c497a246cf9bfcdf87f7d3", [:rebar3], [], "hexpm", "0366f82719d24af4ce45a6591f52a7fc859785823fde4cd84e0dc45119b5ed89"}, - "dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm", "6c32a70ed5d452c6650916555b1f96c79af5fc4bf286997f8b15f213de786f73"}, + "cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"}, + "dialyxir": {:hex, :dialyxir, "1.2.0", "58344b3e87c2e7095304c81a9ae65cb68b613e28340690dfe1a5597fd08dec37", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "61072136427a851674cab81762be4dbeae7679f85b1272b6d25c3a839aff8463"}, + "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, "gun": {:hex, :gun, "2.0.0-rc.2", "7c489a32dedccb77b6e82d1f3c5a7dadfbfa004ec14e322cdb5e579c438632d2", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}], "hexpm", "6b9d1eae146410d727140dbf8b404b9631302ecc2066d1d12f22097ad7d254fc"}, - "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm", "fec8660eb7733ee4117b85f55799fd3833eb769a6df71ccf8903e8dc5447cfce"}, + "jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"}, "protobuf": {:hex, :protobuf, "0.10.0", "4e8e3cf64c5be203b329f88bb8b916cb8d00fb3a12b2ac1f545463ae963c869f", [:mix], [{:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "4ae21a386142357aa3d31ccf5f7d290f03f3fa6f209755f6e87fc2c58c147893"}, "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, } diff --git a/examples/route_guide/priv/client.exs b/examples/route_guide/priv/client.exs index d3c3178e..79a33f0f 100644 --- a/examples/route_guide/priv/client.exs +++ b/examples/route_guide/priv/client.exs @@ -1,9 +1,10 @@ opts = [interceptors: [GRPC.Logger.Client]] + opts = if System.get_env("TLS") do ca_path = Path.expand("./tls/ca.pem", :code.priv_dir(:route_guide)) cred = GRPC.Credential.new(ssl: [cacertfile: ca_path]) - [{:cred, cred}|opts] + [{:cred, cred} | opts] else opts end diff --git a/examples/route_guide/priv/route_guide.proto b/examples/route_guide/priv/protos/route_guide.proto similarity index 100% rename from examples/route_guide/priv/route_guide.proto rename to examples/route_guide/priv/protos/route_guide.proto