├── LICENSE ├── README.md ├── demo ├── .formatter.exs ├── .gitignore ├── README.md ├── config │ └── config.exs ├── lib │ ├── application.ex │ ├── demo │ │ ├── event_consumer.ex │ │ ├── event_store.ex │ │ ├── events │ │ │ ├── destination_specified.ex │ │ │ └── shipment_registered.ex │ │ ├── my_service.ex │ │ └── supervisor.ex │ └── event_store │ │ ├── event_consumer.ex │ │ └── event_publisher.ex ├── mix.exs ├── mix.lock └── test │ └── test_helper.exs ├── extended_example ├── .formatter.exs ├── .gitignore ├── README.md ├── assets │ ├── .babelrc │ ├── css │ │ └── app.scss │ ├── js │ │ └── app.js │ ├── package-lock.json │ ├── package.json │ ├── postcss.config.js │ ├── static │ │ ├── favicon.ico │ │ ├── images │ │ │ └── phoenix.png │ │ └── robots.txt │ ├── tailwind.config.js │ └── webpack.config.js ├── config │ ├── config.exs │ ├── dev.exs │ ├── prod.exs │ ├── prod.secret.exs │ └── test.exs ├── lib │ ├── demo.ex │ ├── demo │ │ ├── analytics │ │ │ ├── analytics_service.ex │ │ │ └── catch_all_consumer.ex │ │ ├── application.ex │ │ ├── broadcaster.ex │ │ ├── events │ │ │ ├── delivery_failed.ex │ │ │ ├── shipment_delegated_to_vehicle.ex │ │ │ ├── shipment_delivered_successfully.ex │ │ │ ├── shipment_out_for_delivery.ex │ │ │ ├── shipment_registered.ex │ │ │ ├── shipment_scheduled_for_delivery.ex │ │ │ ├── vehicle_out_for_delivery.ex │ │ │ └── vehicle_returned.ex │ │ ├── fleet_service │ │ │ ├── fleet_service.ex │ │ │ ├── shipment_scheduled_consumer.ex │ │ │ └── supervisor.ex │ │ ├── fulfillment_service │ │ │ ├── fulfillment_service.ex │ │ │ ├── shipment_delegated_to_vehicle_consumer.ex │ │ │ ├── shipment_registered_consumer.ex │ │ │ ├── supervisor.ex │ │ │ └── vehicle_out_for_delivery_consumer.ex │ │ ├── order_service │ │ │ └── order_service.ex │ │ ├── schemas │ │ │ ├── shipment.ex │ │ │ └── vehicle.ex │ │ └── track_and_trace_service │ │ │ ├── catch_all_consumer.ex │ │ │ └── track_and_trace_service.ex │ ├── event_store │ │ ├── domain_event.ex │ │ ├── event_consumer.ex │ │ ├── event_publisher.ex │ │ ├── event_store.ex │ │ └── repo.ex │ ├── web.ex │ └── web │ │ ├── channels │ │ └── user_socket.ex │ │ ├── controllers │ │ └── dashboard_controller.ex │ │ ├── endpoint.ex │ │ ├── live │ │ ├── analytics │ │ │ └── analytics_live.ex │ │ ├── fleet │ │ │ ├── fleet_live.ex │ │ │ └── vehicle_component.ex │ │ ├── fulfillment │ │ │ ├── fulfillment_live.ex │ │ │ └── shipment_component.ex │ │ ├── order │ │ │ └── orders_live.ex │ │ └── track_and_trace │ │ │ ├── shipment_component.ex │ │ │ └── track_and_trace_live.ex │ │ ├── router.ex │ │ ├── telemetry.ex │ │ ├── templates │ │ ├── dashboard │ │ │ ├── analytics.html.slimleex │ │ │ ├── fleet │ │ │ │ ├── index.html.slimleex │ │ │ │ └── vehicle.html.slimleex │ │ │ ├── fulfillment │ │ │ │ ├── index.html.slimleex │ │ │ │ └── shipment.html.slimleex │ │ │ ├── index.html.slimleex │ │ │ ├── orders.html.slimleex │ │ │ └── track_and_trace │ │ │ │ ├── index.html.slimleex │ │ │ │ └── shipment.html.slimleex │ │ ├── icon │ │ │ ├── badge.html.eex │ │ │ ├── icon.html.eex │ │ │ └── sprite.html.eex │ │ └── layout │ │ │ ├── app.html.slimleex │ │ │ ├── live.html.slimleex │ │ │ └── root.html.slimleex │ │ └── views │ │ ├── dashboard_view.ex │ │ ├── error_helpers.ex │ │ ├── error_view.ex │ │ ├── icon_view.ex │ │ ├── layout_view.ex │ │ └── view_helpers.ex ├── mix.exs ├── mix.lock ├── priv │ └── repo │ │ ├── migrations │ │ └── .formatter.exs │ │ └── seeds.exs └── test │ ├── event_store │ └── event_consumer_test.exs │ ├── support │ ├── concurrency_helper.ex │ ├── conn_case.ex │ ├── data_case.ex │ └── test_case.ex │ ├── test_helper.exs │ └── web │ └── views │ ├── error_view_test.exs │ └── layout_view_test.exs └── slides.pdf /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Peter Ullrich 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Event Sourcing with Elixir - ElixirConf EU 2020 2 | 3 | This repository contains the auxiliary code for the talk `Event Sourcing with Elixir` at `ElixirConf EU 2020`. 4 | 5 | The `demo` folder contains the presented code. 6 | The `extended_example` folder contains an example project with 5 services all communicating with each other through the `commanded/eventstore` library. -------------------------------------------------------------------------------- /demo/.formatter.exs: -------------------------------------------------------------------------------- 1 | # Used by "mix format" 2 | [ 3 | inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] 4 | ] 5 | -------------------------------------------------------------------------------- /demo/.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build/ 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover/ 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps/ 9 | 10 | # Where third-party dependencies like ExDoc output generated docs. 11 | /doc/ 12 | 13 | # Ignore .fetch files in case you like to edit your project deps locally. 14 | /.fetch 15 | 16 | # If the VM crashes, it generates a dump, let's ignore it too. 17 | erl_crash.dump 18 | 19 | # Also ignore archive artifacts (built via "mix archive.build"). 20 | *.ez 21 | 22 | # Ignore package tarball (built via "mix hex.build"). 23 | event_store_example-*.tar 24 | 25 | -------------------------------------------------------------------------------- /demo/README.md: -------------------------------------------------------------------------------- 1 | # Demo 2 | 3 | **TODO: Add description** 4 | 5 | ## Installation 6 | 7 | If [available in Hex](https://hex.pm/docs/publish), the package can be installed 8 | by adding `event_store_example` to your list of dependencies in `mix.exs`: 9 | 10 | ```elixir 11 | def deps do 12 | [ 13 | {:event_store_example, "~> 0.1.0"} 14 | ] 15 | end 16 | ``` 17 | 18 | Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) 19 | and published on [HexDocs](https://hexdocs.pm). Once published, the docs can 20 | be found at [https://hexdocs.pm/event_store_example](https://hexdocs.pm/event_store_example). 21 | 22 | -------------------------------------------------------------------------------- /demo/config/config.exs: -------------------------------------------------------------------------------- 1 | use Mix.Config 2 | 3 | config :event_store_example, 4 | event_stores: [Demo.EventStore] 5 | 6 | config :event_store_example, Demo.EventStore, 7 | column_data_type: "jsonb", 8 | serializer: EventStore.JsonbSerializer, 9 | types: EventStore.PostgresTypes, 10 | username: "postgres", 11 | password: "postgres", 12 | database: "eventstore_example", 13 | hostname: "localhost" 14 | 15 | config :logger, :console, level: :info 16 | -------------------------------------------------------------------------------- /demo/lib/application.ex: -------------------------------------------------------------------------------- 1 | defmodule Demo.Application do 2 | use Application 3 | 4 | def start(_type, _args) do 5 | children = [ 6 | Demo.EventStore, 7 | Demo.Supervisor 8 | ] 9 | 10 | opts = [strategy: :one_for_one, name: Demo.Application] 11 | Supervisor.start_link(children, opts) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /demo/lib/demo/event_consumer.ex: -------------------------------------------------------------------------------- 1 | defmodule Demo.EventConsumer do 2 | use Shared.EventConsumer 3 | 4 | alias Demo.Events.ShipmentRegistered 5 | 6 | def handle(%ShipmentRegistered{} = event_data, %{metadata: metadata}) do 7 | IO.inspect(event_data, label: "Demo.EventConsumer received Event") 8 | IO.inspect(metadata, label: "Demo.EventConsumer received Metadata") 9 | 10 | :ok 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /demo/lib/demo/event_store.ex: -------------------------------------------------------------------------------- 1 | defmodule Demo.EventStore do 2 | use EventStore, otp_app: :event_store_example 3 | end 4 | -------------------------------------------------------------------------------- /demo/lib/demo/events/destination_specified.ex: -------------------------------------------------------------------------------- 1 | defmodule Demo.Events.DestinationSpecified do 2 | defstruct [:for_shipment, :with_address] 3 | end 4 | -------------------------------------------------------------------------------- /demo/lib/demo/events/shipment_registered.ex: -------------------------------------------------------------------------------- 1 | defmodule Demo.Events.ShipmentRegistered do 2 | defstruct [:with_id] 3 | end 4 | -------------------------------------------------------------------------------- /demo/lib/demo/my_service.ex: -------------------------------------------------------------------------------- 1 | defmodule Demo.MyService do 2 | alias Demo.Events.ShipmentRegistered 3 | 4 | def create_shipment(id) do 5 | # Persist new Shipment in Database 6 | # e.g. Repo.insert(%Shipment{id: id}) 7 | event = %ShipmentRegistered{with_id: id} 8 | Shared.EventPublisher.publish("#shipment-#{id}", event, %{enacted_by: "peter.ullrich"}) 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /demo/lib/demo/supervisor.ex: -------------------------------------------------------------------------------- 1 | defmodule Demo.Supervisor do 2 | use Supervisor 3 | 4 | def start_link(args) do 5 | Supervisor.start_link(__MODULE__, [args], name: __MODULE__) 6 | end 7 | 8 | def init([args]) do 9 | children = [Demo.EventConsumer] 10 | 11 | Supervisor.init(children, strategy: :one_for_one) 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /demo/lib/event_store/event_consumer.ex: -------------------------------------------------------------------------------- 1 | defmodule Shared.EventConsumer do 2 | # A more complete version of this event_consumer macro can be found here: 3 | # https://github.com/PJUllrich/event-sourcing-with-elixir/blob/master/lib/event_store/event_consumer.ex 4 | 5 | defmacro __using__(opts) do 6 | quote do 7 | use GenServer 8 | require Logger 9 | 10 | # Adds default handle method 11 | @before_compile unquote(__MODULE__) 12 | 13 | def start_link(_opts) do 14 | handler_module = __MODULE__ 15 | 16 | state = %{handler_module: handler_module} 17 | 18 | GenServer.start_link(handler_module, state, name: handler_module) 19 | end 20 | 21 | def init(opts), do: {:ok, subscribe(opts)} 22 | 23 | # Successfully subscribed to all streams 24 | def handle_info({:subscribed, subscription}, opts) do 25 | {:noreply, %{opts | subscription: subscription}} 26 | end 27 | 28 | # Event notification 29 | def handle_info({:events, events}, opts) do 30 | Enum.each(events, &handle_event(&1, opts)) 31 | {:noreply, opts} 32 | end 33 | 34 | ############################################################################################### 35 | # Private functions 36 | 37 | defp subscribe(%{handler_module: handler_module} = opts) do 38 | {:ok, subscription} = 39 | Demo.EventStore.subscribe_to_all_streams("#{handler_module}", self()) 40 | 41 | Map.merge(opts, %{subscription: subscription}) 42 | end 43 | 44 | defp handle_event( 45 | %{data: event_data} = event, 46 | %{handler_module: handler_module} = opts 47 | ) do 48 | event_data 49 | |> handler_module.handle(event) 50 | |> case do 51 | :ok -> 52 | ack_event(opts, event) 53 | 54 | error -> 55 | Logger.error(inspect(error)) 56 | end 57 | 58 | opts 59 | end 60 | 61 | defp ack_event(%{subscription: subscription} = opts, event) do 62 | :ok = Demo.EventStore.ack(subscription, event) 63 | end 64 | end 65 | end 66 | 67 | defmacro __before_compile__(_env) do 68 | quote generated: true do 69 | def handle(_event_data), do: :ok 70 | 71 | defoverridable handle: 1 72 | 73 | def handle(event_data, _event), do: handle(event_data) 74 | 75 | defoverridable handle: 2 76 | end 77 | end 78 | end 79 | -------------------------------------------------------------------------------- /demo/lib/event_store/event_publisher.ex: -------------------------------------------------------------------------------- 1 | defmodule Shared.EventPublisher do 2 | alias EventStore.EventData 3 | 4 | require Logger 5 | 6 | def publish(stream_uuid, event, metadata, version \\ :any_version) do 7 | event_data = %EventData{ 8 | event_type: to_string(event.__struct__), 9 | data: Map.from_struct(event), 10 | metadata: metadata 11 | } 12 | 13 | Demo.EventStore.append_to_stream(stream_uuid, version, [event_data]) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /demo/mix.exs: -------------------------------------------------------------------------------- 1 | defmodule Demo.MixProject do 2 | use Mix.Project 3 | 4 | def project do 5 | [ 6 | app: :event_store_example, 7 | version: "0.1.0", 8 | elixir: "~> 1.10", 9 | start_permanent: Mix.env() == :prod, 10 | aliases: aliases(), 11 | deps: deps() 12 | ] 13 | end 14 | 15 | # Run "mix help compile.app" to learn about applications. 16 | def application do 17 | [ 18 | mod: {Demo.Application, []} 19 | ] 20 | end 21 | 22 | # Run "mix help deps" to learn about dependencies. 23 | defp deps do 24 | [ 25 | {:eventstore, "~> 1.1"}, 26 | # Needed only if event data should be stored as JSON in Postgres 27 | {:jason, "~> 1.1"} 28 | ] 29 | end 30 | 31 | defp aliases do 32 | [ 33 | setup: ["event_store.create", "event_store.init"], 34 | test: ["event_store.init", "test"] 35 | ] 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /demo/mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"}, 3 | "db_connection": {:hex, :db_connection, "2.2.2", "3bbca41b199e1598245b716248964926303b5d4609ff065125ce98bcd368939e", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "642af240d8a8affb93b4ba5a6fcd2bbcbdc327e1a524b825d383711536f8070c"}, 4 | "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, 5 | "elixir_uuid": {:hex, :elixir_uuid, "1.2.1", "dce506597acb7e6b0daeaff52ff6a9043f5919a4c3315abb4143f0b00378c097", [:mix], [], "hexpm", "f7eba2ea6c3555cea09706492716b0d87397b88946e6380898c2889d68585752"}, 6 | "eventstore": {:hex, :eventstore, "1.1.0", "1f5f20feb343c78cf8a38e8d288f6c7a860056c341dbe1520e6faa728c5e633f", [:mix], [{:elixir_uuid, "~> 1.2", [hex: :elixir_uuid, repo: "hexpm", optional: false]}, {:fsm, "~> 0.3", [hex: :fsm, repo: "hexpm", optional: false]}, {:gen_stage, "~> 1.0", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:highlander, "~> 0.2", [hex: :highlander, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm", "0b61fa4972a1521efa9027c3fd3955afac2df396783357ab2295c9d424124046"}, 7 | "fsm": {:hex, :fsm, "0.3.1", "087aa9b02779a84320dc7a2d8464452b5308e29877921b2bde81cdba32a12390", [:mix], [], "hexpm", "fbf0d53f89e9082b326b0b5828b94b4c549ff9d1452bbfd00b4d1ac082208e96"}, 8 | "gen_stage": {:hex, :gen_stage, "1.0.0", "51c8ae56ff54f9a2a604ca583798c210ad245f415115453b773b621c49776df5", [:mix], [], "hexpm", "1d9fc978db5305ac54e6f5fec7adf80cd893b1000cf78271564c516aa2af7706"}, 9 | "highlander": {:hex, :highlander, "0.2.1", "e59b459f857e89daf73f2598bf2b2c0479a435481e6101ea389fd3625919b052", [:mix], [], "hexpm", "5ba19a18358803d82a923511acec8ee85fac30731c5ca056f2f934bc3d3afd9a"}, 10 | "jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"}, 11 | "postgrex": {:hex, :postgrex, "0.15.6", "a464c72010a56e3214fe2b99c1a76faab4c2bb0255cabdef30dea763a3569aa2", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "f99268325ac8f66ffd6c4964faab9e70fbf721234ab2ad238c00f9530b8cdd55"}, 12 | } 13 | -------------------------------------------------------------------------------- /demo/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | -------------------------------------------------------------------------------- /extended_example/.formatter.exs: -------------------------------------------------------------------------------- 1 | [ 2 | import_deps: [:ecto, :phoenix], 3 | inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"], 4 | subdirectories: ["priv/*/migrations"] 5 | ] 6 | -------------------------------------------------------------------------------- /extended_example/.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build/ 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover/ 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps/ 9 | 10 | # Where 3rd-party dependencies like ExDoc output generated docs. 11 | /doc/ 12 | 13 | # Ignore .fetch files in case you like to edit your project deps locally. 14 | /.fetch 15 | 16 | # If the VM crashes, it generates a dump, let's ignore it too. 17 | erl_crash.dump 18 | 19 | # Also ignore archive artifacts (built via "mix archive.build"). 20 | *.ez 21 | 22 | # Ignore package tarball (built via "mix hex.build"). 23 | demo-*.tar 24 | 25 | # If NPM crashes, it generates a log, let's ignore it too. 26 | npm-debug.log 27 | 28 | # The directory NPM downloads your dependencies sources to. 29 | /assets/node_modules/ 30 | 31 | # Since we are building assets from assets/, 32 | # we ignore priv/static. You may want to comment 33 | # this depending on your deployment strategy. 34 | /priv/static/ 35 | -------------------------------------------------------------------------------- /extended_example/README.md: -------------------------------------------------------------------------------- 1 | # Demo 2 | 3 | To start your Phoenix server: 4 | 5 | * Install dependencies with `mix deps.get` 6 | * Create and migrate your database with `mix ecto.setup` 7 | * Install Node.js dependencies with `npm install` inside the `assets` directory 8 | * Start Phoenix endpoint with `mix phx.server` 9 | 10 | Now you can visit [`localhost:4000`](http://localhost:4000) from your browser. 11 | 12 | Ready to run in production? Please [check our deployment guides](https://hexdocs.pm/phoenix/deployment.html). 13 | 14 | ## Learn more 15 | 16 | * Official website: https://www.phoenixframework.org/ 17 | * Guides: https://hexdocs.pm/phoenix/overview.html 18 | * Docs: https://hexdocs.pm/phoenix 19 | * Forum: https://elixirforum.com/c/phoenix-forum 20 | * Source: https://github.com/phoenixframework/phoenix 21 | -------------------------------------------------------------------------------- /extended_example/assets/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "@babel/preset-env" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /extended_example/assets/css/app.scss: -------------------------------------------------------------------------------- 1 | /* This file is for your main application css. */ 2 | @import "../node_modules/nprogress/nprogress.css"; 3 | 4 | @tailwind base; 5 | 6 | @tailwind components; 7 | 8 | @tailwind utilities; 9 | 10 | /* LiveView specific classes for your customizations */ 11 | .phx-no-feedback.invalid-feedback, 12 | .phx-no-feedback .invalid-feedback { 13 | display: none; 14 | } 15 | 16 | .phx-click-loading { 17 | opacity: 0.5; 18 | transition: opacity 1s ease-out; 19 | } 20 | 21 | .phx-disconnected { 22 | cursor: wait; 23 | } 24 | .phx-disconnected * { 25 | pointer-events: none; 26 | } 27 | 28 | .card { 29 | @apply p-5 h-auto flex flex-col items-stretch w-1/2; 30 | } 31 | 32 | .card-table-wrapper { 33 | @apply rounded shadow-lg bg-white flex-1 relative overflow-auto; 34 | } 35 | 36 | .table-header { 37 | @apply py-2 px-3 sticky top-0 border-b border-gray-200 bg-gray-100 text-gray-600 uppercase; 38 | } 39 | 40 | .cell { 41 | @apply border-dashed border-t border-gray-200 px-5; 42 | } 43 | 44 | .icon { 45 | width: 1em; 46 | height: 1em; 47 | vertical-align: -0.125em; 48 | display: inline-block; 49 | fill: currentColor; 50 | } 51 | 52 | .badge { 53 | @apply rounded px-3; 54 | display: inline-block; 55 | text-align: center; 56 | white-space: nowrap; 57 | vertical-align: baseline; 58 | 59 | // Empty badges collapse automatically 60 | &:empty { 61 | display: none; 62 | } 63 | } 64 | 65 | .badge-filled { 66 | color: color("white"); 67 | background-color: color("gray"); 68 | border-color: color("gray"); 69 | 70 | .badge-icon { 71 | color: color("white"); 72 | } 73 | } 74 | 75 | /* Alerts and form errors */ 76 | .alert { 77 | padding: 15px; 78 | margin-bottom: 20px; 79 | border: 1px solid transparent; 80 | border-radius: 4px; 81 | } 82 | .alert-info { 83 | color: #31708f; 84 | background-color: #d9edf7; 85 | border-color: #bce8f1; 86 | } 87 | .alert-warning { 88 | color: #8a6d3b; 89 | background-color: #fcf8e3; 90 | border-color: #faebcc; 91 | } 92 | .alert-danger { 93 | color: #a94442; 94 | background-color: #f2dede; 95 | border-color: #ebccd1; 96 | } 97 | .alert p { 98 | margin-bottom: 0; 99 | } 100 | .alert:empty { 101 | display: none; 102 | } 103 | .invalid-feedback { 104 | color: #a94442; 105 | display: block; 106 | margin: -1rem 0 2rem; 107 | } 108 | -------------------------------------------------------------------------------- /extended_example/assets/js/app.js: -------------------------------------------------------------------------------- 1 | // We need to import the CSS so that webpack will load it. 2 | // The MiniCssExtractPlugin is used to separate it out into 3 | // its own CSS file. 4 | import "../css/app.scss" 5 | 6 | // webpack automatically bundles all modules in your 7 | // entry points. Those entry points can be configured 8 | // in "webpack.config.js". 9 | // 10 | // Import deps with the dep name or local files with a relative path, for example: 11 | // 12 | // import {Socket} from "phoenix" 13 | // import socket from "./socket" 14 | // 15 | import "phoenix_html" 16 | import {Socket} from "phoenix" 17 | import NProgress from "nprogress" 18 | import {LiveSocket} from "phoenix_live_view" 19 | 20 | let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content") 21 | let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}}) 22 | 23 | // Show progress bar on live navigation and form submits 24 | window.addEventListener("phx:page-loading-start", info => NProgress.start()) 25 | window.addEventListener("phx:page-loading-stop", info => NProgress.done()) 26 | 27 | // connect if there are any LiveViews on the page 28 | liveSocket.connect() 29 | 30 | // expose liveSocket on window for web console debug logs and latency simulation: 31 | // >> liveSocket.enableDebug() 32 | // >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session 33 | // >> liveSocket.disableLatencySim() 34 | window.liveSocket = liveSocket 35 | 36 | -------------------------------------------------------------------------------- /extended_example/assets/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "repository": {}, 3 | "description": " ", 4 | "license": "MIT", 5 | "scripts": { 6 | "deploy": "webpack --mode production", 7 | "watch": "webpack --mode development --watch" 8 | }, 9 | "dependencies": { 10 | "nprogress": "^0.2.0", 11 | "phoenix": "file:../deps/phoenix", 12 | "phoenix_html": "file:../deps/phoenix_html", 13 | "phoenix_live_view": "file:../deps/phoenix_live_view" 14 | }, 15 | "devDependencies": { 16 | "@babel/core": "^7.0.0", 17 | "@babel/preset-env": "^7.0.0", 18 | "babel-loader": "^8.0.0", 19 | "copy-webpack-plugin": "^5.1.1", 20 | "css-loader": "^3.4.2", 21 | "hard-source-webpack-plugin": "^0.13.1", 22 | "mini-css-extract-plugin": "^0.9.0", 23 | "node-sass": "^4.13.1", 24 | "optimize-css-assets-webpack-plugin": "^5.0.1", 25 | "postcss-loader": "^4.0.1", 26 | "sass-loader": "^8.0.2", 27 | "tailwindcss": "^1.8.10", 28 | "terser-webpack-plugin": "^2.3.2", 29 | "webpack": "4.41.5", 30 | "webpack-cli": "^3.3.2" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /extended_example/assets/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: [ 3 | require('tailwindcss'), 4 | require('autoprefixer') 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /extended_example/assets/static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PJUllrich/event-sourcing-with-elixir/7f70e6bc49d9d93f1d86513a1f358e41e07b8304/extended_example/assets/static/favicon.ico -------------------------------------------------------------------------------- /extended_example/assets/static/images/phoenix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PJUllrich/event-sourcing-with-elixir/7f70e6bc49d9d93f1d86513a1f358e41e07b8304/extended_example/assets/static/images/phoenix.png -------------------------------------------------------------------------------- /extended_example/assets/static/robots.txt: -------------------------------------------------------------------------------- 1 | # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file 2 | # 3 | # To ban all spiders from the entire site uncomment the next two lines: 4 | # User-agent: * 5 | # Disallow: / 6 | -------------------------------------------------------------------------------- /extended_example/assets/tailwind.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | future: { 3 | // removeDeprecatedGapUtilities: true, 4 | // purgeLayersByDefault: true, 5 | }, 6 | purge: [], 7 | theme: { 8 | maxHeight: { 9 | '0': '0', 10 | '1/4': '25%', 11 | '1/2': '50%', 12 | '3/4': '75%', 13 | '99': '99%', 14 | 'full': '100%', 15 | } 16 | }, 17 | variants: {}, 18 | plugins: [], 19 | } 20 | -------------------------------------------------------------------------------- /extended_example/assets/webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const glob = require('glob'); 3 | const HardSourceWebpackPlugin = require('hard-source-webpack-plugin'); 4 | const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 5 | const TerserPlugin = require('terser-webpack-plugin'); 6 | const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); 7 | const CopyWebpackPlugin = require('copy-webpack-plugin'); 8 | 9 | module.exports = (env, options) => { 10 | const devMode = options.mode !== 'production'; 11 | 12 | return { 13 | optimization: { 14 | minimizer: [ 15 | new TerserPlugin({ cache: true, parallel: true, sourceMap: devMode }), 16 | new OptimizeCSSAssetsPlugin({}) 17 | ] 18 | }, 19 | entry: { 20 | 'app': glob.sync('./vendor/**/*.js').concat(['./js/app.js']) 21 | }, 22 | output: { 23 | filename: '[name].js', 24 | path: path.resolve(__dirname, '../priv/static/js'), 25 | publicPath: '/js/' 26 | }, 27 | devtool: devMode ? 'eval-cheap-module-source-map' : undefined, 28 | module: { 29 | rules: [ 30 | { 31 | test: /\.js$/, 32 | exclude: /node_modules/, 33 | use: { 34 | loader: 'babel-loader' 35 | } 36 | }, 37 | { 38 | test: /\.[s]?css$/, 39 | use: [ 40 | MiniCssExtractPlugin.loader, 41 | 'css-loader', 42 | 'sass-loader', 43 | 'postcss-loader' 44 | ], 45 | } 46 | ] 47 | }, 48 | plugins: [ 49 | new MiniCssExtractPlugin({ filename: '../css/app.css' }), 50 | new CopyWebpackPlugin([{ from: 'static/', to: '../' }]) 51 | ] 52 | .concat(devMode ? [new HardSourceWebpackPlugin()] : []) 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /extended_example/config/config.exs: -------------------------------------------------------------------------------- 1 | # This file is responsible for configuring your application 2 | # and its dependencies with the aid of the Mix.Config module. 3 | # 4 | # This configuration file is loaded before any dependency and 5 | # is restricted to this project. 6 | 7 | # General application configuration 8 | use Mix.Config 9 | 10 | config :demo, 11 | ecto_repos: [EventStore.Repo], 12 | event_stores: [Shared.EventStore] 13 | 14 | # Configures the endpoint 15 | config :demo, Web.Endpoint, 16 | url: [host: "localhost"], 17 | secret_key_base: "N4HX2rebF2fWFq0BcjMQKxAFMrDhTcW4evDHp1oTsW6Cz42O0iuVq0l6c52r8q+l", 18 | render_errors: [view: Web.ErrorView, accepts: ~w(html json), layout: false], 19 | pubsub_server: Demo.PubSub, 20 | live_view: [signing_salt: "Ew3EZ6mO"] 21 | 22 | config :phoenix, :template_engines, 23 | slim: PhoenixSlime.Engine, 24 | slime: PhoenixSlime.Engine, 25 | slimleex: PhoenixSlime.LiveViewEngine 26 | 27 | # Configures Elixir's Logger 28 | config :logger, :console, 29 | level: :info, 30 | format: "$time $metadata[$level] $message\n", 31 | metadata: [:request_id] 32 | 33 | # Use Jason for JSON parsing in Phoenix 34 | config :phoenix, :json_library, Jason 35 | 36 | config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase 37 | 38 | # Import environment specific config. This must remain at the bottom 39 | # of this file so it overrides the configuration defined above. 40 | import_config "#{Mix.env()}.exs" 41 | -------------------------------------------------------------------------------- /extended_example/config/dev.exs: -------------------------------------------------------------------------------- 1 | use Mix.Config 2 | 3 | # Configure your database 4 | config :demo, EventStore.Repo, 5 | username: "postgres", 6 | password: "postgres", 7 | database: "eventstore_dev", 8 | hostname: "localhost", 9 | show_sensitive_data_on_connection_error: true, 10 | pool_size: 10 11 | 12 | config :demo, Shared.EventStore, 13 | column_data_type: "jsonb", 14 | serializer: EventStore.JsonbSerializer, 15 | types: EventStore.PostgresTypes, 16 | database: "eventstore_dev", 17 | username: "postgres", 18 | password: "postgres", 19 | hostname: "localhost" 20 | 21 | # For development, we disable any cache and enable 22 | # debugging and code reloading. 23 | # 24 | # The watchers configuration can be used to run external 25 | # watchers to your application. For example, we use it 26 | # with webpack to recompile .js and .css sources. 27 | config :demo, Web.Endpoint, 28 | http: [port: 4000], 29 | debug_errors: true, 30 | code_reloader: true, 31 | check_origin: false, 32 | watchers: [ 33 | node: [ 34 | "node_modules/webpack/bin/webpack.js", 35 | "--mode", 36 | "development", 37 | "--watch-stdin", 38 | cd: Path.expand("../assets", __DIR__) 39 | ] 40 | ] 41 | 42 | # ## SSL Support 43 | # 44 | # In order to use HTTPS in development, a self-signed 45 | # certificate can be generated by running the following 46 | # Mix task: 47 | # 48 | # mix phx.gen.cert 49 | # 50 | # Note that this task requires Erlang/OTP 20 or later. 51 | # Run `mix help phx.gen.cert` for more information. 52 | # 53 | # The `http:` config above can be replaced with: 54 | # 55 | # https: [ 56 | # port: 4001, 57 | # cipher_suite: :strong, 58 | # keyfile: "priv/cert/selfsigned_key.pem", 59 | # certfile: "priv/cert/selfsigned.pem" 60 | # ], 61 | # 62 | # If desired, both `http:` and `https:` keys can be 63 | # configured to run both http and https servers on 64 | # different ports. 65 | 66 | # Watch static and templates for browser reloading. 67 | config :demo, Web.Endpoint, 68 | live_reload: [ 69 | patterns: [ 70 | ~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$", 71 | ~r"lib/web/(live|views)/*.*(ex)$", 72 | ~r"lib/web/templates/.*(eex|slim|slime)$" 73 | ] 74 | ] 75 | 76 | # Do not include metadata nor timestamps in development logs 77 | config :logger, :info, format: "[$level] $message\n" 78 | 79 | # Set a higher stacktrace during development. Avoid configuring such 80 | # in production as building large stacktraces may be expensive. 81 | config :phoenix, :stacktrace_depth, 20 82 | 83 | # Initialize plugs at runtime for faster development compilation 84 | config :phoenix, :plug_init_mode, :runtime 85 | -------------------------------------------------------------------------------- /extended_example/config/prod.exs: -------------------------------------------------------------------------------- 1 | use Mix.Config 2 | 3 | # For production, don't forget to configure the url host 4 | # to something meaningful, Phoenix uses this information 5 | # when generating URLs. 6 | # 7 | # Note we also include the path to a cache manifest 8 | # containing the digested version of static files. This 9 | # manifest is generated by the `mix phx.digest` task, 10 | # which you should run after static files are built and 11 | # before starting your production server. 12 | config :demo, Web.Endpoint, 13 | url: [host: "example.com", port: 80], 14 | cache_static_manifest: "priv/static/cache_manifest.json" 15 | 16 | # Do not print debug messages in production 17 | config :logger, level: :info 18 | 19 | # ## SSL Support 20 | # 21 | # To get SSL working, you will need to add the `https` key 22 | # to the previous section and set your `:url` port to 443: 23 | # 24 | # config :demo, Web.Endpoint, 25 | # ... 26 | # url: [host: "example.com", port: 443], 27 | # https: [ 28 | # port: 443, 29 | # cipher_suite: :strong, 30 | # keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"), 31 | # certfile: System.get_env("SOME_APP_SSL_CERT_PATH"), 32 | # transport_options: [socket_opts: [:inet6]] 33 | # ] 34 | # 35 | # The `cipher_suite` is set to `:strong` to support only the 36 | # latest and more secure SSL ciphers. This means old browsers 37 | # and clients may not be supported. You can set it to 38 | # `:compatible` for wider support. 39 | # 40 | # `:keyfile` and `:certfile` expect an absolute path to the key 41 | # and cert in disk or a relative path inside priv, for example 42 | # "priv/ssl/server.key". For all supported SSL configuration 43 | # options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1 44 | # 45 | # We also recommend setting `force_ssl` in your endpoint, ensuring 46 | # no data is ever sent via http, always redirecting to https: 47 | # 48 | # config :demo, Web.Endpoint, 49 | # force_ssl: [hsts: true] 50 | # 51 | # Check `Plug.SSL` for all available options in `force_ssl`. 52 | 53 | # Finally import the config/prod.secret.exs which loads secrets 54 | # and configuration from environment variables. 55 | import_config "prod.secret.exs" 56 | -------------------------------------------------------------------------------- /extended_example/config/prod.secret.exs: -------------------------------------------------------------------------------- 1 | # In this file, we load production configuration and secrets 2 | # from environment variables. You can also hardcode secrets, 3 | # although such is generally not recommended and you have to 4 | # remember to add this file to your .gitignore. 5 | use Mix.Config 6 | 7 | database_url = 8 | System.get_env("DATABASE_URL") || 9 | raise """ 10 | environment variable DATABASE_URL is missing. 11 | For example: ecto://USER:PASS@HOST/DATABASE 12 | """ 13 | 14 | secret_key_base = 15 | System.get_env("SECRET_KEY_BASE") || 16 | raise """ 17 | environment variable SECRET_KEY_BASE is missing. 18 | You can generate one by calling: mix phx.gen.secret 19 | """ 20 | 21 | config :demo, Web.Endpoint, 22 | http: [ 23 | port: String.to_integer(System.get_env("PORT") || "4000"), 24 | transport_options: [socket_opts: [:inet6]] 25 | ], 26 | secret_key_base: secret_key_base 27 | 28 | # ## Using releases (Elixir v1.9+) 29 | # 30 | # If you are doing OTP releases, you need to instruct Phoenix 31 | # to start each relevant endpoint: 32 | # 33 | # config :demo, Web.Endpoint, server: true 34 | # 35 | # Then you can assemble a release by calling `mix release`. 36 | # See `mix help release` for more information. 37 | -------------------------------------------------------------------------------- /extended_example/config/test.exs: -------------------------------------------------------------------------------- 1 | use Mix.Config 2 | 3 | # Configure your database 4 | # 5 | # The MIX_TEST_PARTITION environment variable can be used 6 | # to provide built-in test partitioning in CI environment. 7 | # Run `mix help test` for more information. 8 | config :demo, EventStore.Repo, 9 | username: "postgres", 10 | password: "postgres", 11 | database: "eventstore_test#{System.get_env("MIX_TEST_PARTITION")}", 12 | hostname: "localhost", 13 | pool: Ecto.Adapters.SQL.Sandbox 14 | 15 | config :demo, Shared.EventStore, 16 | column_data_type: "jsonb", 17 | serializer: EventStore.JsonbSerializer, 18 | types: EventStore.PostgresTypes, 19 | database: "eventstore_test#{System.get_env("MIX_TEST_PARTITION")}", 20 | username: "postgres", 21 | password: "postgres", 22 | hostname: "localhost" 23 | 24 | # We don't run a server during test. If one is required, 25 | # you can enable the server option below. 26 | config :demo, Web.Endpoint, 27 | http: [port: 4002], 28 | server: false 29 | 30 | # Print only warnings and errors during test 31 | config :logger, level: :warn 32 | -------------------------------------------------------------------------------- /extended_example/lib/demo.ex: -------------------------------------------------------------------------------- 1 | defmodule Demo do 2 | @moduledoc """ 3 | Demo keeps the contexts that define your domain 4 | and business logic. 5 | 6 | Contexts are also responsible for managing your data, regardless 7 | if it comes from the database, an external API or others. 8 | """ 9 | end 10 | -------------------------------------------------------------------------------- /extended_example/lib/demo/analytics/analytics_service.ex: -------------------------------------------------------------------------------- 1 | defmodule AnalyticsService.Supervisor do 2 | use Supervisor 3 | 4 | def start_link(args) do 5 | Supervisor.start_link(__MODULE__, [args], name: __MODULE__) 6 | end 7 | 8 | def init([_args]) do 9 | children = [ 10 | AnalyticsService.CatchAllConsumer 11 | ] 12 | 13 | Supervisor.init(children, strategy: :one_for_one) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /extended_example/lib/demo/analytics/catch_all_consumer.ex: -------------------------------------------------------------------------------- 1 | defmodule AnalyticsService.CatchAllConsumer do 2 | use Shared.EventConsumer 3 | 4 | def handle(event, state) do 5 | Broadcaster.broadcast("AnalyticsService", event) 6 | {:ok, state} 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /extended_example/lib/demo/application.ex: -------------------------------------------------------------------------------- 1 | defmodule Demo.Application do 2 | # See https://hexdocs.pm/elixir/Application.html 3 | # for more information on OTP Applications 4 | @moduledoc false 5 | 6 | use Application 7 | 8 | def start(_type, _args) do 9 | children = 10 | environment_specific_children(Mix.env()) ++ 11 | [ 12 | EventStore.Repo, 13 | # Start the Telemetry supervisor 14 | Web.Telemetry, 15 | # Start the PubSub system 16 | {Phoenix.PubSub, name: Demo.PubSub}, 17 | # Start the Endpoint (http/https) 18 | Web.Endpoint, 19 | {Demo.OrderService, []}, 20 | {FulfillmentService.Supervisor, []}, 21 | {TrackAndTraceService.Supervisor, []}, 22 | {AnalyticsService.Supervisor, []}, 23 | {FleetService.Supervisor, []} 24 | ] 25 | 26 | Faker.start() 27 | 28 | opts = [strategy: :one_for_one, name: Demo.Supervisor] 29 | Supervisor.start_link(children, opts) 30 | end 31 | 32 | # Tell Phoenix to update the endpoint configuration 33 | # whenever the application is updated. 34 | def config_change(changed, _new, removed) do 35 | Web.Endpoint.config_change(changed, removed) 36 | :ok 37 | end 38 | 39 | defp environment_specific_children(:test), do: [] 40 | defp environment_specific_children(_), do: [{Shared.EventStore, []}] 41 | end 42 | -------------------------------------------------------------------------------- /extended_example/lib/demo/broadcaster.ex: -------------------------------------------------------------------------------- 1 | defmodule Broadcaster do 2 | def broadcast(topic, event) do 3 | Phoenix.PubSub.broadcast_from!( 4 | Demo.PubSub, 5 | self(), 6 | topic, 7 | event 8 | ) 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /extended_example/lib/demo/events/delivery_failed.ex: -------------------------------------------------------------------------------- 1 | defmodule DeliveryFailed do 2 | defstruct [:shipment_id] 3 | end 4 | -------------------------------------------------------------------------------- /extended_example/lib/demo/events/shipment_delegated_to_vehicle.ex: -------------------------------------------------------------------------------- 1 | defmodule ShipmentDelegatedToVehicle do 2 | defstruct [:shipment_id, :vehicle_id] 3 | end 4 | -------------------------------------------------------------------------------- /extended_example/lib/demo/events/shipment_delivered_successfully.ex: -------------------------------------------------------------------------------- 1 | defmodule ShipmentDeliveredSuccessfully do 2 | defstruct [:shipment_id] 3 | end 4 | -------------------------------------------------------------------------------- /extended_example/lib/demo/events/shipment_out_for_delivery.ex: -------------------------------------------------------------------------------- 1 | defmodule ShipmentOutForDelivery do 2 | defstruct [:shipment_id, :vehicle_id] 3 | end 4 | -------------------------------------------------------------------------------- /extended_example/lib/demo/events/shipment_registered.ex: -------------------------------------------------------------------------------- 1 | defmodule ShipmentRegistered do 2 | defstruct [:shipment_id, :destination] 3 | end 4 | -------------------------------------------------------------------------------- /extended_example/lib/demo/events/shipment_scheduled_for_delivery.ex: -------------------------------------------------------------------------------- 1 | defmodule ShipmentScheduledForDelivery do 2 | defstruct [:shipment_id, :scheduled_for] 3 | end 4 | -------------------------------------------------------------------------------- /extended_example/lib/demo/events/vehicle_out_for_delivery.ex: -------------------------------------------------------------------------------- 1 | defmodule VehicleOutForDelivery do 2 | defstruct [:vehicle_id] 3 | end 4 | -------------------------------------------------------------------------------- /extended_example/lib/demo/events/vehicle_returned.ex: -------------------------------------------------------------------------------- 1 | defmodule VehicleReturned do 2 | defstruct [:vehicle_id] 3 | end 4 | -------------------------------------------------------------------------------- /extended_example/lib/demo/fleet_service/fleet_service.ex: -------------------------------------------------------------------------------- 1 | defmodule FleetService do 2 | use GenServer 3 | 4 | ############################################################################################### 5 | # Client API 6 | 7 | def start_link(opts \\ []) do 8 | delivery_time = opts[:delivery_time] || 10_000 9 | 10 | vehicles = [ 11 | %Vehicle{vehicle_id: "1", capacity: 2, planned_shipment_count: 0, out_for_delivery: false}, 12 | %Vehicle{vehicle_id: "2", capacity: 2, planned_shipment_count: 0, out_for_delivery: false}, 13 | %Vehicle{vehicle_id: "3", capacity: 2, planned_shipment_count: 0, out_for_delivery: false} 14 | ] 15 | 16 | opts = 17 | opts 18 | |> Keyword.merge( 19 | vehicles: vehicles, 20 | delivery_time: delivery_time 21 | ) 22 | |> Map.new() 23 | 24 | GenServer.start_link(__MODULE__, opts, name: __MODULE__) 25 | end 26 | 27 | def delegate_shipment_to_vehicle(shipment_id), 28 | do: GenServer.call(__MODULE__, {:delegate_shipment, shipment_id}) 29 | 30 | def list_vehicles(), do: GenServer.call(__MODULE__, :list_vehicles) 31 | 32 | ############################################################################################### 33 | # GenServer Callbacks 34 | 35 | def init(opts), do: {:ok, opts} 36 | 37 | def handle_call(:list_vehicles, _from, %{vehicles: vehicles} = opts), 38 | do: {:reply, vehicles, opts} 39 | 40 | def handle_call( 41 | {:delegate_shipment, shipment_id}, 42 | _from, 43 | %{vehicles: vehicles} = opts 44 | ) do 45 | %{vehicle_id: vehicle_id} = vehicle = get_available_vehicle(vehicles) 46 | 47 | event = %ShipmentDelegatedToVehicle{ 48 | shipment_id: shipment_id, 49 | vehicle_id: vehicle_id 50 | } 51 | 52 | updated_vehicle = Map.update!(vehicle, :planned_shipment_count, &(&1 + 1)) 53 | 54 | vehicles = update_vehicle(updated_vehicle, vehicles) 55 | 56 | :ok = Shared.EventPublisher.publish(shipment_id, event, %{enacted_by: "FleetService"}) 57 | Broadcaster.broadcast("FleetService", event) 58 | 59 | if updated_vehicle.capacity <= updated_vehicle.planned_shipment_count do 60 | send(self(), {:vehicle_out_for_delivery, updated_vehicle.vehicle_id}) 61 | end 62 | 63 | {:reply, :ok, %{opts | vehicles: vehicles}} 64 | end 65 | 66 | def handle_info({:vehicle_out_for_delivery, vehicle_id}, opts) do 67 | event = %VehicleOutForDelivery{vehicle_id: vehicle_id} 68 | 69 | opts = update_vehicle(vehicle_id, opts, %{out_for_delivery: true}) 70 | :ok = Shared.EventPublisher.publish(vehicle_id, event, %{enacted_by: "FleetService"}) 71 | Broadcaster.broadcast("FleetService", event) 72 | 73 | Process.send_after(self(), {:vehicle_returned, vehicle_id}, opts[:delivery_time]) 74 | {:noreply, opts} 75 | end 76 | 77 | def handle_info({:vehicle_returned, vehicle_id}, opts) do 78 | event = %VehicleReturned{vehicle_id: vehicle_id} 79 | 80 | opts = update_vehicle(vehicle_id, opts, %{out_for_delivery: false, planned_shipment_count: 0}) 81 | :ok = Shared.EventPublisher.publish(vehicle_id, event, %{enacted_by: "FleetService"}) 82 | Broadcaster.broadcast("FleetService", event) 83 | 84 | {:noreply, opts} 85 | end 86 | 87 | ############################################################################################### 88 | # Private functions 89 | 90 | defp get_available_vehicle(vehicles) do 91 | vehicles 92 | |> Stream.filter(&(not &1.out_for_delivery)) 93 | |> Enum.random() 94 | end 95 | 96 | defp update_vehicle(vehicle, vehicles) do 97 | Enum.map(vehicles, fn old_vehicle -> 98 | if old_vehicle.vehicle_id == vehicle.vehicle_id, do: vehicle, else: old_vehicle 99 | end) 100 | end 101 | 102 | defp update_vehicle(vehicle_id, %{vehicles: vehicles} = opts, attrs) do 103 | vehicles = 104 | vehicles 105 | |> Enum.find(&(&1.vehicle_id == vehicle_id)) 106 | |> Map.merge(attrs) 107 | |> update_vehicle(vehicles) 108 | 109 | %{opts | vehicles: vehicles} 110 | end 111 | end 112 | -------------------------------------------------------------------------------- /extended_example/lib/demo/fleet_service/shipment_scheduled_consumer.ex: -------------------------------------------------------------------------------- 1 | defmodule FleetService.ShipmentScheduledForDeliveryConsumer do 2 | use Shared.EventConsumer 3 | 4 | def handle(%ShipmentScheduledForDelivery{} = %{shipment_id: shipment_id}, state) do 5 | FleetService.delegate_shipment_to_vehicle(shipment_id) 6 | {:ok, state} 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /extended_example/lib/demo/fleet_service/supervisor.ex: -------------------------------------------------------------------------------- 1 | defmodule FleetService.Supervisor do 2 | use Supervisor 3 | 4 | def start_link(args) do 5 | Supervisor.start_link(__MODULE__, [args], name: __MODULE__) 6 | end 7 | 8 | def init([_args]) do 9 | children = [ 10 | FleetService.ShipmentScheduledForDeliveryConsumer, 11 | FleetService 12 | ] 13 | 14 | Supervisor.init(children, strategy: :one_for_one) 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /extended_example/lib/demo/fulfillment_service/fulfillment_service.ex: -------------------------------------------------------------------------------- 1 | defmodule FulfillmentService do 2 | use GenServer 3 | 4 | ############################################################################################### 5 | # Client API 6 | 7 | def start_link(opts \\ []) do 8 | fulfillment_delay = opts[:fulfillment_delay] || 5_000 9 | delivery_delay = opts[:delivery_delay] || 10_000 10 | scheduling_delay = opts[:scheduling_delay] || 1_000 11 | 12 | opts = 13 | opts 14 | |> Keyword.merge( 15 | fulfillment_delay: fulfillment_delay, 16 | delivery_delay: delivery_delay, 17 | scheduling_delay: scheduling_delay, 18 | shipment_delegations: %{} 19 | ) 20 | |> Map.new() 21 | 22 | GenServer.start_link(__MODULE__, opts, name: __MODULE__) 23 | end 24 | 25 | def schedule_shipment(shipment_id), 26 | do: GenServer.call(__MODULE__, {:schedule_shipment, shipment_id}) 27 | 28 | def delegate_shipment_to_vehicle(event_data) do 29 | GenServer.call(__MODULE__, {:delegate_shipment, event_data}) 30 | end 31 | 32 | def mark_shipments_as_out_for_delivery_for_vehicle(vehicle_id) do 33 | GenServer.call(__MODULE__, {:mark_shimpents_as_out_for_delivery, vehicle_id}) 34 | end 35 | 36 | ############################################################################################### 37 | # GenServer Callbacks 38 | 39 | def init(opts), do: {:ok, opts} 40 | 41 | def handle_call({:schedule_shipment, shipment_id}, _from, opts) do 42 | Process.send_after(self(), {:schedule_shipment, shipment_id}, opts[:scheduling_delay]) 43 | {:reply, :ok, opts} 44 | end 45 | 46 | def handle_call( 47 | {:mark_shimpents_as_out_for_delivery, vehicle_id}, 48 | _from, 49 | %{shipment_delegations: shipment_delegations} = opts 50 | ) do 51 | shipment_ids = Map.get(shipment_delegations, vehicle_id, []) 52 | 53 | for shipment_id <- shipment_ids do 54 | event = %ShipmentOutForDelivery{shipment_id: shipment_id, vehicle_id: vehicle_id} 55 | 56 | :ok = Shared.EventPublisher.publish(shipment_id, event, %{enacted_by: __MODULE__}) 57 | Broadcaster.broadcast("FulfillmentService", event) 58 | 59 | Process.send_after( 60 | self(), 61 | {:shipment_delivered, shipment_id}, 62 | :rand.uniform(opts[:delivery_delay]) 63 | ) 64 | end 65 | 66 | shipment_delegations = Map.put(shipment_delegations, vehicle_id, []) 67 | 68 | {:reply, :ok, %{opts | shipment_delegations: shipment_delegations}} 69 | end 70 | 71 | def handle_call( 72 | {:delegate_shipment, %{shipment_id: shipment_id, vehicle_id: vehicle_id} = event_data}, 73 | _from, 74 | %{shipment_delegations: shipment_delegations} = opts 75 | ) do 76 | shipment_delegations = 77 | Map.update(shipment_delegations, vehicle_id, [], fn shipment_ids -> 78 | [shipment_id | shipment_ids] 79 | end) 80 | 81 | Broadcaster.broadcast("FulfillmentService", event_data) 82 | 83 | {:reply, :ok, %{opts | shipment_delegations: shipment_delegations}} 84 | end 85 | 86 | def handle_info({:schedule_shipment, shipment_id}, opts) do 87 | event = %ShipmentScheduledForDelivery{ 88 | shipment_id: shipment_id, 89 | scheduled_for: gen_scheduled_time(opts) 90 | } 91 | 92 | :ok = Shared.EventPublisher.publish(shipment_id, event, %{enacted_by: __MODULE__}) 93 | Broadcaster.broadcast("FulfillmentService", event) 94 | 95 | {:noreply, opts} 96 | end 97 | 98 | def handle_info({:shipment_delivered, shipment_id}, opts) do 99 | event = 100 | if Enum.random([true, true, true, false]) do 101 | %ShipmentDeliveredSuccessfully{shipment_id: shipment_id} 102 | else 103 | %DeliveryFailed{shipment_id: shipment_id} 104 | end 105 | 106 | :ok = Shared.EventPublisher.publish(shipment_id, event, %{enacted_by: __MODULE__}) 107 | Broadcaster.broadcast("FulfillmentService", event) 108 | 109 | {:noreply, opts} 110 | end 111 | 112 | ############################################################################################### 113 | # Private functions 114 | 115 | defp gen_scheduled_time(%{fulfillment_delay: fulfillment_delay}) do 116 | DateTime.now!("Europe/Berlin") 117 | |> DateTime.to_time() 118 | |> Time.add(fulfillment_delay, :millisecond) 119 | end 120 | end 121 | -------------------------------------------------------------------------------- /extended_example/lib/demo/fulfillment_service/shipment_delegated_to_vehicle_consumer.ex: -------------------------------------------------------------------------------- 1 | defmodule FulfillmentService.ShipmentDelegatedToVehicleConsumer do 2 | use Shared.EventConsumer 3 | 4 | def handle(%ShipmentDelegatedToVehicle{} = event_data, state) do 5 | :ok = FulfillmentService.delegate_shipment_to_vehicle(event_data) 6 | {:ok, state} 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /extended_example/lib/demo/fulfillment_service/shipment_registered_consumer.ex: -------------------------------------------------------------------------------- 1 | defmodule FulfillmentService.ShipmentRegisteredConsumer do 2 | use Shared.EventConsumer 3 | 4 | def handle(%ShipmentRegistered{} = event_data, state) do 5 | FulfillmentService.schedule_shipment(event_data.shipment_id) 6 | Broadcaster.broadcast("FulfillmentService", event_data) 7 | {:ok, state} 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /extended_example/lib/demo/fulfillment_service/supervisor.ex: -------------------------------------------------------------------------------- 1 | defmodule FulfillmentService.Supervisor do 2 | use Supervisor 3 | 4 | def start_link(opts) do 5 | Supervisor.start_link(__MODULE__, opts, name: __MODULE__) 6 | end 7 | 8 | def init(_args) do 9 | children = [ 10 | FulfillmentService, 11 | FulfillmentService.ShipmentRegisteredConsumer, 12 | FulfillmentService.ShipmentDelegatedToVehicleConsumer, 13 | FulfillmentService.VehicleOutForDeliveryConsumer 14 | ] 15 | 16 | Supervisor.init(children, strategy: :one_for_one) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /extended_example/lib/demo/fulfillment_service/vehicle_out_for_delivery_consumer.ex: -------------------------------------------------------------------------------- 1 | defmodule FulfillmentService.VehicleOutForDeliveryConsumer do 2 | use Shared.EventConsumer 3 | 4 | def handle(%VehicleOutForDelivery{} = %{vehicle_id: vehicle_id}, state) do 5 | :ok = FulfillmentService.mark_shipments_as_out_for_delivery_for_vehicle(vehicle_id) 6 | {:ok, state} 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /extended_example/lib/demo/order_service/order_service.ex: -------------------------------------------------------------------------------- 1 | defmodule Demo.OrderService do 2 | use GenServer 3 | 4 | alias ShipmentRegistered 5 | 6 | ############################################################################################### 7 | # Client API 8 | 9 | def start_link(opts \\ []) do 10 | shipment_creation_interval = opts[:shipment_creation_interval] || 7_000 11 | 12 | last_shipment_id = 13 | Shared.EventStore.stream_all_forward() 14 | |> Stream.filter(&(&1.event_type == "#{ShipmentRegistered}")) 15 | |> Enum.to_list() 16 | |> length() 17 | 18 | opts = 19 | opts 20 | |> Keyword.merge( 21 | shipment_creation_interval: shipment_creation_interval, 22 | running_id: last_shipment_id 23 | ) 24 | |> Map.new() 25 | 26 | GenServer.start_link(__MODULE__, opts, name: __MODULE__) 27 | end 28 | 29 | def set_interval(interval) do 30 | GenServer.call(__MODULE__, {:set_interval, interval}) 31 | end 32 | 33 | ############################################################################################### 34 | # GenServer Callbacks 35 | 36 | def init(opts) do 37 | schedule_shipment_creation(opts) 38 | {:ok, opts} 39 | end 40 | 41 | def handle_info(:create_shipment, %{running_id: running_id} = opts) do 42 | next_id = running_id + 1 43 | 44 | event = %ShipmentRegistered{ 45 | shipment_id: Integer.to_string(next_id), 46 | destination: Faker.Address.En.street_address() 47 | } 48 | 49 | :ok = Shared.EventPublisher.publish("shipment-#{next_id}", event, %{enacted_by: __MODULE__}) 50 | Broadcaster.broadcast("OrderService", event) 51 | 52 | schedule_shipment_creation(opts) 53 | {:noreply, %{opts | running_id: next_id}} 54 | end 55 | 56 | def handle_call({:set_interval, interval}, _from, opts) do 57 | {:reply, :ok, Keyword.put(opts, :shipment_creation_interval, interval)} 58 | end 59 | 60 | ############################################################################################### 61 | # Private functions 62 | 63 | defp schedule_shipment_creation(opts) do 64 | Process.send_after(self(), :create_shipment, opts[:shipment_creation_interval]) 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /extended_example/lib/demo/schemas/shipment.ex: -------------------------------------------------------------------------------- 1 | defmodule Shipment do 2 | defstruct [ 3 | :shipment_id, 4 | :destination, 5 | :delivered_successfully, 6 | :out_for_delivery, 7 | :delivering_vehicle, 8 | :scheduled_for_vehicle, 9 | :scheduled_for 10 | ] 11 | end 12 | -------------------------------------------------------------------------------- /extended_example/lib/demo/schemas/vehicle.ex: -------------------------------------------------------------------------------- 1 | defmodule Vehicle do 2 | defstruct [:vehicle_id, :capacity, :planned_shipment_count, :out_for_delivery] 3 | end 4 | -------------------------------------------------------------------------------- /extended_example/lib/demo/track_and_trace_service/catch_all_consumer.ex: -------------------------------------------------------------------------------- 1 | defmodule TrackAndTraceService.CatchAllConsumer do 2 | use Shared.EventConsumer 3 | 4 | def handle(event, state) do 5 | Broadcaster.broadcast("TrackAndTraceService", event) 6 | {:ok, state} 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /extended_example/lib/demo/track_and_trace_service/track_and_trace_service.ex: -------------------------------------------------------------------------------- 1 | defmodule TrackAndTraceService.Supervisor do 2 | use Supervisor 3 | 4 | def start_link(args) do 5 | Supervisor.start_link(__MODULE__, [args], name: __MODULE__) 6 | end 7 | 8 | def init([_args]) do 9 | children = [ 10 | TrackAndTraceService.CatchAllConsumer 11 | ] 12 | 13 | Supervisor.init(children, strategy: :one_for_one) 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /extended_example/lib/event_store/domain_event.ex: -------------------------------------------------------------------------------- 1 | defmodule Shared.DomainEvent do 2 | defmacro __using__(_opts) do 3 | quote do 4 | defimpl String.Chars, for: __MODULE__ do 5 | def to_string(event) do 6 | event |> Map.from_struct() |> Map.to_list() |> List.to_string() 7 | end 8 | end 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /extended_example/lib/event_store/event_consumer.ex: -------------------------------------------------------------------------------- 1 | defmodule Shared.EventConsumer do 2 | defmacro __using__(opts) do 3 | quote do 4 | use GenServer 5 | require Logger 6 | 7 | # Adds default handle method 8 | @before_compile unquote(__MODULE__) 9 | 10 | @opts unquote(opts) || [] 11 | 12 | ############################################################################################### 13 | # Client API 14 | 15 | def start_link(opts \\ []) do 16 | opts = Keyword.merge(@opts, opts) 17 | handler_module = @opts[:handler_module] || __MODULE__ 18 | initial_state = @opts[:initial_state] || %{} 19 | start_from = @opts[:start_from] || :origin 20 | event_store = @opts[:event_store] || Shared.EventStore 21 | 22 | opts = 23 | opts 24 | |> Keyword.merge( 25 | handler_module: handler_module, 26 | start_from: start_from, 27 | event_store: event_store, 28 | state: initial_state 29 | ) 30 | |> Map.new() 31 | 32 | GenServer.start_link(handler_module, opts, name: handler_module) 33 | end 34 | 35 | def get_state(subscriber), do: GenServer.call(subscriber, :get_state) 36 | 37 | def pause(subscriber), do: GenServer.call(subscriber, :pause) 38 | 39 | def resume(subscriber), do: GenServer.call(subscriber, :subscribe) 40 | 41 | ############################################################################################### 42 | # GenServer Callbacks 43 | 44 | def init(opts), do: {:ok, subscribe(opts)} 45 | 46 | # Successfully subscribed to all streams 47 | def handle_info({:subscribed, subscription}, opts) do 48 | {:noreply, %{opts | subscription: subscription}} 49 | end 50 | 51 | # Event notification 52 | def handle_info({:events, events}, opts) do 53 | opts = Enum.reduce(events, opts, &handle_event/2) 54 | opts = inc_event_counter(opts, events) 55 | 56 | {:noreply, opts} 57 | end 58 | 59 | def handle_call(:subscribe, _from, opts) do 60 | {:reply, :ok, subscribe(opts)} 61 | end 62 | 63 | def handle_call(:get_state, _from, %{state: state} = opts) do 64 | {:reply, state, opts} 65 | end 66 | 67 | def handle_call( 68 | :pause, 69 | _from, 70 | %{event_store: event_store, subscription: subscription} = opts 71 | ) do 72 | :ok = event_store.unsubscribe_from_all_streams(subscription_name(opts)) 73 | {:reply, :ok, opts} 74 | end 75 | 76 | ############################################################################################### 77 | # Private functions 78 | 79 | defp subscribe( 80 | %{event_store: event_store, handler_module: handler_module, start_from: start_from} = 81 | opts 82 | ) do 83 | {:ok, subscription} = 84 | event_store.subscribe_to_all_streams( 85 | subscription_name(opts), 86 | self(), 87 | start_from: start_from 88 | ) 89 | 90 | Map.merge(opts, %{subscription: subscription}) 91 | end 92 | 93 | defp handle_event( 94 | %{data: event_data} = event, 95 | %{handler_module: handler_module, state: old_state} = opts 96 | ) do 97 | new_state = 98 | event_data 99 | |> handler_module.handle(old_state, event) 100 | |> case do 101 | {:ok, new_state} -> 102 | # Only acknowledge the event 103 | ack_event(opts, event) 104 | new_state 105 | 106 | error -> 107 | Logger.error(inspect(error)) 108 | old_state 109 | end 110 | 111 | %{opts | state: new_state} 112 | end 113 | 114 | defp ack_event(%{event_store: event_store, subscription: subscription} = opts, event) do 115 | :ok = event_store.ack(subscription, event) 116 | end 117 | 118 | defp subscription_name(%{handler_module: handler_module} = _opts) do 119 | "#{handler_module}" 120 | end 121 | 122 | def inc_event_counter(%{start_from: :origin} = opts, events), 123 | do: %{opts | start_from: length(events)} 124 | 125 | def inc_event_counter(%{start_from: start_form} = opts, events) do 126 | start_form = start_form + length(events) 127 | %{opts | start_from: start_form} 128 | end 129 | end 130 | end 131 | 132 | defmacro __before_compile__(_env) do 133 | quote generated: true do 134 | def handle(_event_data, state), do: {:ok, state} 135 | 136 | defoverridable handle: 2 137 | 138 | def handle(event_data, state, _event), do: handle(event_data, state) 139 | 140 | defoverridable handle: 3 141 | end 142 | end 143 | end 144 | -------------------------------------------------------------------------------- /extended_example/lib/event_store/event_publisher.ex: -------------------------------------------------------------------------------- 1 | defmodule Shared.EventPublisher do 2 | alias EventStore.EventData 3 | 4 | require Logger 5 | 6 | def publish(stream_uuid, event, metadata) do 7 | Logger.info("Publishing: #{inspect(event)}") 8 | 9 | event_data = %EventData{ 10 | event_type: to_string(event.__struct__), 11 | data: Map.from_struct(event), 12 | metadata: metadata 13 | } 14 | 15 | Shared.EventStore.append_to_stream(stream_uuid, :any_version, [event_data]) 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /extended_example/lib/event_store/event_store.ex: -------------------------------------------------------------------------------- 1 | defmodule Shared.EventStore do 2 | use EventStore, otp_app: :demo 3 | end 4 | -------------------------------------------------------------------------------- /extended_example/lib/event_store/repo.ex: -------------------------------------------------------------------------------- 1 | defmodule EventStore.Repo do 2 | use Ecto.Repo, 3 | otp_app: :demo, 4 | adapter: Ecto.Adapters.Postgres 5 | end 6 | -------------------------------------------------------------------------------- /extended_example/lib/web.ex: -------------------------------------------------------------------------------- 1 | defmodule Web do 2 | @moduledoc """ 3 | The entrypoint for defining your web interface, such 4 | as controllers, views, channels and so on. 5 | 6 | This can be used in your application as: 7 | 8 | use Web, :controller 9 | use Web, :view 10 | 11 | The definitions below will be executed for every view, 12 | controller, etc, so keep them short and clean, focused 13 | on imports, uses and aliases. 14 | 15 | Do NOT define functions inside the quoted expressions 16 | below. Instead, define any helper function in modules 17 | and import those modules here. 18 | """ 19 | 20 | def controller do 21 | quote do 22 | use Phoenix.Controller, namespace: Web 23 | 24 | import Plug.Conn 25 | alias Web.Router.Helpers, as: Routes 26 | end 27 | end 28 | 29 | def view do 30 | quote do 31 | use Phoenix.View, 32 | root: "lib/web/templates", 33 | pattern: "**/*", 34 | namespace: Web 35 | 36 | # Import convenience functions from controllers 37 | import Phoenix.Controller, 38 | only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1] 39 | 40 | # Include shared imports and aliases for views 41 | unquote(view_helpers()) 42 | end 43 | end 44 | 45 | def live_view do 46 | quote do 47 | use Phoenix.LiveView, 48 | layout: {Web.LayoutView, "live.html"}, 49 | container: {:div, class: "h-full"} 50 | 51 | unquote(view_helpers()) 52 | end 53 | end 54 | 55 | def live_component do 56 | quote do 57 | use Phoenix.LiveComponent 58 | 59 | unquote(view_helpers()) 60 | end 61 | end 62 | 63 | def router do 64 | quote do 65 | use Phoenix.Router 66 | 67 | import Plug.Conn 68 | import Phoenix.Controller 69 | import Phoenix.LiveView.Router 70 | end 71 | end 72 | 73 | def channel do 74 | quote do 75 | use Phoenix.Channel 76 | end 77 | end 78 | 79 | defp view_helpers do 80 | quote do 81 | # Use all HTML functionality (forms, tags, etc) 82 | use Phoenix.HTML 83 | 84 | # Import LiveView helpers (live_render, live_component, live_patch, etc) 85 | import Phoenix.LiveView.Helpers 86 | 87 | # Import basic rendering functionality (render, render_layout, etc) 88 | import Phoenix.View 89 | import Web.ViewHelpers 90 | 91 | import Web.ErrorHelpers 92 | alias Web.Router.Helpers, as: Routes 93 | end 94 | end 95 | 96 | @doc """ 97 | When used, dispatch to the appropriate controller/view/etc. 98 | """ 99 | defmacro __using__(which) when is_atom(which) do 100 | apply(__MODULE__, which, []) 101 | end 102 | end 103 | -------------------------------------------------------------------------------- /extended_example/lib/web/channels/user_socket.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.UserSocket do 2 | use Phoenix.Socket 3 | 4 | ## Channels 5 | # channel "room:*", Web.RoomChannel 6 | 7 | # Socket params are passed from the client and can 8 | # be used to verify and authenticate a user. After 9 | # verification, you can put default assigns into 10 | # the socket that will be set for all channels, ie 11 | # 12 | # {:ok, assign(socket, :user_id, verified_user_id)} 13 | # 14 | # To deny connection, return `:error`. 15 | # 16 | # See `Phoenix.Token` documentation for examples in 17 | # performing token verification on connect. 18 | @impl true 19 | def connect(_params, socket, _connect_info) do 20 | {:ok, socket} 21 | end 22 | 23 | # Socket id's are topics that allow you to identify all sockets for a given user: 24 | # 25 | # def id(socket), do: "user_socket:#{socket.assigns.user_id}" 26 | # 27 | # Would allow you to broadcast a "disconnect" event and terminate 28 | # all active sockets and channels for a given user: 29 | # 30 | # Web.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{}) 31 | # 32 | # Returning `nil` makes this socket anonymous. 33 | @impl true 34 | def id(_socket), do: nil 35 | end 36 | -------------------------------------------------------------------------------- /extended_example/lib/web/controllers/dashboard_controller.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.DashboardController do 2 | use Web, :controller 3 | 4 | def index(conn, _params) do 5 | render(conn, "index.html") 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /extended_example/lib/web/endpoint.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.Endpoint do 2 | use Phoenix.Endpoint, otp_app: :demo 3 | 4 | # The session will be stored in the cookie and signed, 5 | # this means its contents can be read but not tampered with. 6 | # Set :encryption_salt if you would also like to encrypt it. 7 | @session_options [ 8 | store: :cookie, 9 | key: "_demo_key", 10 | signing_salt: "Jwrt30oM" 11 | ] 12 | 13 | socket "/socket", Web.UserSocket, 14 | websocket: true, 15 | longpoll: false 16 | 17 | socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]] 18 | 19 | # Serve at "/" the static files from "priv/static" directory. 20 | # 21 | # You should set gzip to true if you are running phx.digest 22 | # when deploying your static files in production. 23 | plug Plug.Static, 24 | at: "/", 25 | from: :demo, 26 | gzip: false, 27 | only: ~w(css fonts images js favicon.ico robots.txt) 28 | 29 | # Code reloading can be explicitly enabled under the 30 | # :code_reloader configuration of your endpoint. 31 | if code_reloading? do 32 | socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket 33 | plug Phoenix.LiveReloader 34 | plug Phoenix.CodeReloader 35 | plug Phoenix.Ecto.CheckRepoStatus, otp_app: :demo 36 | end 37 | 38 | plug Plug.RequestId 39 | plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint] 40 | 41 | plug Plug.Parsers, 42 | parsers: [:urlencoded, :multipart, :json], 43 | pass: ["*/*"], 44 | json_decoder: Phoenix.json_library() 45 | 46 | plug Plug.MethodOverride 47 | plug Plug.Head 48 | plug Plug.Session, @session_options 49 | plug Web.Router 50 | end 51 | -------------------------------------------------------------------------------- /extended_example/lib/web/live/analytics/analytics_live.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.AnalyticsLive do 2 | use Web, :live_view 3 | 4 | require Logger 5 | 6 | @impl true 7 | def render(assigns), do: Web.DashboardView.render("analytics.html", assigns) 8 | 9 | @impl true 10 | def mount(_params, _session, socket) do 11 | if connected?(socket), do: Phoenix.PubSub.subscribe(Demo.PubSub, "AnalyticsService") 12 | 13 | socket = 14 | socket 15 | |> assign(:scheduled, 0) 16 | |> assign(:out_for_delivery, 0) 17 | |> assign(:shipment_count, 0) 18 | |> assign(:successfull_deliveries, 0) 19 | |> assign(:failed_deliveries, 0) 20 | |> fetch_all_events() 21 | 22 | {:ok, socket} 23 | end 24 | 25 | @impl true 26 | def handle_info(%ShipmentRegistered{} = _event, socket) do 27 | {:noreply, update(socket, :shipment_count, &(&1 + 1))} 28 | end 29 | 30 | @impl true 31 | def handle_info(%ShipmentScheduledForDelivery{} = _event, socket) do 32 | {:noreply, update(socket, :scheduled, &(&1 + 1))} 33 | end 34 | 35 | @impl true 36 | def handle_info(%ShipmentOutForDelivery{} = _event_data, socket) do 37 | socket = 38 | socket 39 | |> update(:out_for_delivery, &(&1 + 1)) 40 | |> update(:scheduled, &(&1 - 1)) 41 | 42 | {:noreply, socket} 43 | end 44 | 45 | @impl true 46 | def handle_info(%ShipmentDeliveredSuccessfully{} = _event, socket) do 47 | socket = 48 | socket 49 | |> update(:successfull_deliveries, &(&1 + 1)) 50 | |> update(:out_for_delivery, &(&1 - 1)) 51 | 52 | {:noreply, socket} 53 | end 54 | 55 | @impl true 56 | def handle_info(%DeliveryFailed{} = _event, socket) do 57 | socket = 58 | socket 59 | |> update(:failed_deliveries, &(&1 + 1)) 60 | |> update(:out_for_delivery, &(&1 - 1)) 61 | 62 | {:noreply, socket} 63 | end 64 | 65 | @impl true 66 | def handle_info(_event, socket), do: {:noreply, socket} 67 | 68 | defp fetch_all_events(socket) do 69 | {:noreply, socket} = 70 | Shared.EventStore.stream_all_forward() 71 | |> Enum.map(& &1.data) 72 | |> Enum.reduce({:noreply, socket}, fn event_data, {:noreply, socket} -> 73 | handle_info(event_data, socket) 74 | end) 75 | 76 | socket 77 | end 78 | end 79 | -------------------------------------------------------------------------------- /extended_example/lib/web/live/fleet/fleet_live.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.FleetLive do 2 | use Web, :live_view 3 | 4 | require Logger 5 | 6 | alias Web.FleetLive.VehicleComponent 7 | 8 | @impl true 9 | def render(assigns), do: Web.DashboardView.render("fleet/index.html", assigns) 10 | 11 | @impl true 12 | def mount(_params, _session, socket) do 13 | if connected?(socket), do: Phoenix.PubSub.subscribe(Demo.PubSub, "FleetService") 14 | 15 | socket = 16 | socket 17 | |> assign(:vehicles, FleetService.list_vehicles()) 18 | |> fetch_all_events() 19 | 20 | {:ok, socket} 21 | end 22 | 23 | @impl true 24 | def handle_info(%ShipmentDelegatedToVehicle{} = %{vehicle_id: vehicle_id}, socket) do 25 | send_update(VehicleComponent, id: vehicle_id, planned_shipment_count_inc: 1) 26 | {:noreply, socket} 27 | end 28 | 29 | @impl true 30 | def handle_info(%VehicleOutForDelivery{} = %{vehicle_id: vehicle_id}, socket) do 31 | send_update(VehicleComponent, id: vehicle_id, out_for_delivery: true) 32 | {:noreply, socket} 33 | end 34 | 35 | @impl true 36 | def handle_info(%VehicleReturned{} = %{vehicle_id: vehicle_id}, socket) do 37 | send_update(VehicleComponent, 38 | id: vehicle_id, 39 | planned_shipment_count: 0, 40 | out_for_delivery: false 41 | ) 42 | 43 | {:noreply, socket} 44 | end 45 | 46 | @impl true 47 | def handle_info(_event, socket), do: {:noreply, socket} 48 | 49 | defp fetch_all_events(socket) do 50 | {:noreply, socket} = 51 | Shared.EventStore.stream_all_forward() 52 | |> Enum.sort_by(& &1.created_at) 53 | |> Enum.map(& &1.data) 54 | |> Enum.reduce({:noreply, socket}, fn event_data, {:noreply, socket} -> 55 | handle_info(event_data, socket) 56 | end) 57 | 58 | socket 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /extended_example/lib/web/live/fleet/vehicle_component.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.FleetLive.VehicleComponent do 2 | use Web, :live_component 3 | 4 | @impl true 5 | def render(assigns), do: Web.DashboardView.render("fleet/vehicle.html", assigns) 6 | 7 | @impl true 8 | def update(assigns, socket) do 9 | out_for_delivery = 10 | if is_nil(assigns[:out_for_delivery]), do: false, else: assigns[:out_for_delivery] 11 | 12 | vehicle = 13 | (socket.assigns[:vehicle] || assigns[:vehicle]) 14 | |> Map.update!(:planned_shipment_count, &(assigns[:planned_shipment_count] || &1)) 15 | |> Map.update!( 16 | :planned_shipment_count, 17 | &(&1 + (assigns[:planned_shipment_count_inc] || 0)) 18 | ) 19 | |> Map.put(:out_for_delivery, out_for_delivery) 20 | 21 | {:ok, assign(socket, :vehicle, vehicle)} 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /extended_example/lib/web/live/fulfillment/fulfillment_live.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.FulfillmentLive do 2 | use Web, :live_view 3 | 4 | alias Web.FulfillmentLive.ShipmentComponent 5 | 6 | @impl true 7 | def render(assigns), do: Web.DashboardView.render("fulfillment/index.html", assigns) 8 | 9 | @impl true 10 | def mount(_params, _session, socket) do 11 | if connected?(socket), do: Phoenix.PubSub.subscribe(Demo.PubSub, "FulfillmentService") 12 | 13 | socket = 14 | socket 15 | |> fetch_shipments() 16 | |> fetch_all_events() 17 | 18 | {:ok, socket, temporary_assigns: [shipments: []]} 19 | end 20 | 21 | @impl true 22 | def handle_info(%ShipmentRegistered{} = event_data, socket) do 23 | shipment = create_shipment(event_data) 24 | socket = update(socket, :shipments, fn shipments -> [shipment | shipments] end) 25 | {:noreply, socket} 26 | end 27 | 28 | @impl true 29 | def handle_info( 30 | %ShipmentOutForDelivery{} = %{shipment_id: shipment_id, vehicle_id: vehicle_id}, 31 | socket 32 | ) do 33 | update_shipment(%{ 34 | shipment_id: shipment_id, 35 | delivering_vehicle: vehicle_id, 36 | out_for_delivery: true 37 | }) 38 | 39 | {:noreply, socket} 40 | end 41 | 42 | @impl true 43 | def handle_info( 44 | %ShipmentDelegatedToVehicle{} = %{shipment_id: shipment_id, vehicle_id: vehicle_id}, 45 | socket 46 | ) do 47 | update_shipment(%{shipment_id: shipment_id, scheduled_for_vehicle: vehicle_id}) 48 | {:noreply, socket} 49 | end 50 | 51 | @impl true 52 | def handle_info( 53 | %ShipmentDeliveredSuccessfully{} = %{shipment_id: shipment_id}, 54 | socket 55 | ) do 56 | update_shipment(%{ 57 | shipment_id: shipment_id, 58 | delivered_successfully: true, 59 | out_for_delivery: false 60 | }) 61 | 62 | {:noreply, socket} 63 | end 64 | 65 | @impl true 66 | def handle_info( 67 | %DeliveryFailed{} = %{shipment_id: shipment_id}, 68 | socket 69 | ) do 70 | update_shipment(%{ 71 | shipment_id: shipment_id, 72 | delivered_successfully: false, 73 | out_for_delivery: false 74 | }) 75 | 76 | {:noreply, socket} 77 | end 78 | 79 | @impl true 80 | def handle_info(event_data, socket) do 81 | update_shipment(event_data) 82 | {:noreply, socket} 83 | end 84 | 85 | defp fetch_all_events(socket) do 86 | {:noreply, socket} = 87 | Shared.EventStore.stream_all_forward() 88 | |> Stream.filter(&(&1.event_type != "#{ShipmentRegistered}")) 89 | |> Enum.sort_by(& &1.created_at) 90 | |> Enum.map(& &1.data) 91 | |> Enum.reduce({:noreply, socket}, fn event_data, {:noreply, socket} -> 92 | handle_info(event_data, socket) 93 | end) 94 | 95 | socket 96 | end 97 | 98 | defp fetch_shipments(socket) do 99 | shipments = 100 | Shared.EventStore.stream_all_forward() 101 | |> Stream.filter(&(&1.event_type == "#{ShipmentRegistered}")) 102 | |> Enum.sort_by(& &1.created_at) 103 | |> Stream.map(& &1.data) 104 | |> Stream.map(&create_shipment/1) 105 | |> Enum.sort_by(&String.to_integer(&1.shipment_id), :desc) 106 | 107 | assign(socket, :shipments, shipments) 108 | end 109 | 110 | defp create_shipment(event_data) do 111 | Map.merge(%Shipment{}, event_data) 112 | end 113 | 114 | defp update_shipment(%{shipment_id: shipment_id} = event_data) do 115 | send_update(ShipmentComponent, id: shipment_id, shipment: event_data) 116 | end 117 | 118 | defp update_shipment(_event_data), do: nil 119 | end 120 | -------------------------------------------------------------------------------- /extended_example/lib/web/live/fulfillment/shipment_component.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.FulfillmentLive.ShipmentComponent do 2 | use Web, :live_component 3 | 4 | @impl true 5 | def render(assigns), do: Web.DashboardView.render("fulfillment/shipment.html", assigns) 6 | 7 | @impl true 8 | def update(assigns, socket) do 9 | old_shipment = socket.assigns[:shipment] || %Shipment{} 10 | update = assigns[:shipment] || %{} 11 | shipment = Map.merge(old_shipment, update) 12 | 13 | {:ok, assign(socket, :shipment, shipment)} 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /extended_example/lib/web/live/order/orders_live.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.OrdersLive do 2 | use Web, :live_view 3 | 4 | @impl true 5 | def render(assigns), do: Web.DashboardView.render("orders.html", assigns) 6 | 7 | @impl true 8 | def mount(_params, _session, socket) do 9 | if connected?(socket), do: Phoenix.PubSub.subscribe(Demo.PubSub, "OrderService") 10 | 11 | socket = 12 | socket 13 | |> assign(:shipments, socket.assigns[:shipments] || []) 14 | |> fetch_all_events() 15 | 16 | {:ok, socket, temporary_assigns: [shipments: []]} 17 | end 18 | 19 | @impl true 20 | def handle_info(event_data, socket) do 21 | {:noreply, handle(event_data, socket)} 22 | end 23 | 24 | defp handle( 25 | %ShipmentRegistered{} = %{shipment_id: shipment_id, destination: destination}, 26 | socket 27 | ) do 28 | shipment = %Shipment{shipment_id: shipment_id, destination: destination} 29 | update(socket, :shipments, fn shipments -> [shipment | shipments] end) 30 | end 31 | 32 | defp handle(_event, socket), do: socket 33 | 34 | defp fetch_all_events(socket) do 35 | Shared.EventStore.stream_all_forward() 36 | |> Enum.map(& &1.data) 37 | |> Enum.reduce(socket, &handle/2) 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /extended_example/lib/web/live/track_and_trace/shipment_component.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.TrackAndTraceLive.ShipmentComponent do 2 | use Web, :live_component 3 | 4 | @impl true 5 | def render(assigns), do: Web.DashboardView.render("track_and_trace/shipment.html", assigns) 6 | 7 | @impl true 8 | def update(assigns, socket) do 9 | old_shipment = socket.assigns[:shipment] || %Shipment{} 10 | update = assigns[:shipment] || %{} 11 | shipment = Map.merge(old_shipment, update) 12 | 13 | {:ok, assign(socket, :shipment, shipment)} 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /extended_example/lib/web/live/track_and_trace/track_and_trace_live.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.TrackAndTraceLive do 2 | use Web, :live_view 3 | 4 | alias Web.TrackAndTraceLive.ShipmentComponent 5 | 6 | @impl true 7 | def render(assigns), do: Web.DashboardView.render("track_and_trace/index.html", assigns) 8 | 9 | @impl true 10 | def mount(_params, _session, socket) do 11 | if connected?(socket), do: Phoenix.PubSub.subscribe(Demo.PubSub, "TrackAndTraceService") 12 | 13 | socket = 14 | socket 15 | |> fetch_shipments() 16 | |> fetch_all_events() 17 | 18 | {:ok, socket, temporary_assigns: [shipments: []]} 19 | end 20 | 21 | @impl true 22 | def handle_info(%ShipmentRegistered{} = event_data, socket) do 23 | shipment = create_shipment(event_data) 24 | socket = update(socket, :shipments, fn shipments -> [shipment | shipments] end) 25 | {:noreply, socket} 26 | end 27 | 28 | @impl true 29 | def handle_info(%ShipmentOutForDelivery{} = %{shipment_id: shipment_id}, socket) do 30 | update_shipment(%{shipment_id: shipment_id, out_for_delivery: true}) 31 | {:noreply, socket} 32 | end 33 | 34 | @impl true 35 | def handle_info(%ShipmentDeliveredSuccessfully{} = %{shipment_id: shipment_id}, socket) do 36 | update_shipment(%{shipment_id: shipment_id, delivered_successfully: true}) 37 | {:noreply, socket} 38 | end 39 | 40 | @impl true 41 | def handle_info(%DeliveryFailed{} = %{shipment_id: shipment_id}, socket) do 42 | update_shipment(%{shipment_id: shipment_id, delivered_successfully: false}) 43 | {:noreply, socket} 44 | end 45 | 46 | @impl true 47 | def handle_info(event_data, socket) do 48 | update_shipment(event_data) 49 | {:noreply, socket} 50 | end 51 | 52 | defp fetch_all_events(socket) do 53 | {:noreply, socket} = 54 | Shared.EventStore.stream_all_forward() 55 | |> Stream.filter(&(&1.event_type != "#{ShipmentRegistered}")) 56 | |> Enum.sort_by(& &1.created_at) 57 | |> Stream.map(& &1.data) 58 | |> Enum.reduce({:noreply, socket}, fn event_data, {:noreply, socket} -> 59 | handle_info(event_data, socket) 60 | end) 61 | 62 | socket 63 | end 64 | 65 | defp fetch_shipments(socket) do 66 | shipments = 67 | Shared.EventStore.stream_all_forward() 68 | |> Stream.filter(&(&1.event_type == "#{ShipmentRegistered}")) 69 | |> Stream.map(& &1.data) 70 | |> Stream.map(&create_shipment/1) 71 | |> Enum.sort_by(&String.to_integer(&1.shipment_id), :desc) 72 | 73 | assign(socket, :shipments, shipments) 74 | end 75 | 76 | defp create_shipment(event_data) do 77 | Map.merge(%Shipment{}, event_data) 78 | end 79 | 80 | defp update_shipment(%{shipment_id: shipment_id} = event_data) do 81 | send_update(ShipmentComponent, id: shipment_id, shipment: event_data) 82 | end 83 | 84 | defp update_shipment(_event_data), do: nil 85 | end 86 | -------------------------------------------------------------------------------- /extended_example/lib/web/router.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.Router do 2 | use Web, :router 3 | 4 | pipeline :browser do 5 | plug :accepts, ["html"] 6 | plug :fetch_session 7 | plug :fetch_live_flash 8 | plug :put_root_layout, {Web.LayoutView, :root} 9 | plug :protect_from_forgery 10 | plug :put_secure_browser_headers 11 | end 12 | 13 | pipeline :api do 14 | plug :accepts, ["json"] 15 | end 16 | 17 | scope "/", Web do 18 | pipe_through :browser 19 | 20 | get "/", DashboardController, :index 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /extended_example/lib/web/telemetry.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.Telemetry do 2 | use Supervisor 3 | import Telemetry.Metrics 4 | 5 | def start_link(arg) do 6 | Supervisor.start_link(__MODULE__, arg, name: __MODULE__) 7 | end 8 | 9 | @impl true 10 | def init(_arg) do 11 | children = [ 12 | # Telemetry poller will execute the given period measurements 13 | # every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics 14 | {:telemetry_poller, measurements: periodic_measurements(), period: 10_000} 15 | # Add reporters as children of your supervision tree. 16 | # {Telemetry.Metrics.ConsoleReporter, metrics: metrics()} 17 | ] 18 | 19 | Supervisor.init(children, strategy: :one_for_one) 20 | end 21 | 22 | def metrics do 23 | [ 24 | # Phoenix Metrics 25 | summary("phoenix.endpoint.stop.duration", 26 | unit: {:native, :millisecond} 27 | ), 28 | summary("phoenix.router_dispatch.stop.duration", 29 | tags: [:route], 30 | unit: {:native, :millisecond} 31 | ), 32 | 33 | # Database Metrics 34 | summary("demo.repo.query.total_time", unit: {:native, :millisecond}), 35 | summary("demo.repo.query.decode_time", unit: {:native, :millisecond}), 36 | summary("demo.repo.query.query_time", unit: {:native, :millisecond}), 37 | summary("demo.repo.query.queue_time", unit: {:native, :millisecond}), 38 | summary("demo.repo.query.idle_time", unit: {:native, :millisecond}), 39 | 40 | # VM Metrics 41 | summary("vm.memory.total", unit: {:byte, :kilobyte}), 42 | summary("vm.total_run_queue_lengths.total"), 43 | summary("vm.total_run_queue_lengths.cpu"), 44 | summary("vm.total_run_queue_lengths.io") 45 | ] 46 | end 47 | 48 | defp periodic_measurements do 49 | [ 50 | # A module, function and arguments to be invoked periodically. 51 | # This function must call :telemetry.execute/3 and a metric must be added above. 52 | # {Web, :count_users, []} 53 | ] 54 | end 55 | end 56 | -------------------------------------------------------------------------------- /extended_example/lib/web/templates/dashboard/analytics.html.slimleex: -------------------------------------------------------------------------------- 1 | .bg-white.flex.flex-col.p-5 2 | .row.flex.flex-1 3 | .flex-1 4 | .text-xl Shipment Count 5 | .text-3xl = @shipment_count 6 | .flex-1 7 | .text-xl Scheduled 8 | .text-3xl = @scheduled 9 | .flex-1 10 | .text-xl Out for Delivery 11 | .text-3xl = @out_for_delivery 12 | .row.flex.flex-1.mt-5 13 | .flex-1 14 | .text-xl Successful Deliveries 15 | .text-3xl = @successfull_deliveries 16 | .flex-1 17 | .text-xl Failed Deliveries 18 | .text-3xl = @failed_deliveries 19 | .flex-1 20 | .text-xl Failed Deliveries Ratio 21 | = if @failed_deliveries > 0 do 22 | .text-3xl #{(@failed_deliveries / (@successfull_deliveries + @failed_deliveries)) * 100 |> :erlang.float_to_binary([decimals: 2])} % 23 | - else 24 | .text-3xl 0.00% -------------------------------------------------------------------------------- /extended_example/lib/web/templates/dashboard/fleet/index.html.slimleex: -------------------------------------------------------------------------------- 1 | table.border-collapse.table-fixed.w-full.max-h-full.whitespace-no-wrap.table-striped.absolute.bg-white 2 | thead 3 | tr.text-left 4 | th.table-header[class="w-1/5"] Vehicle ID 5 | th.table-header[class="w-1/4"] Status 6 | th.table-header[class="w-1/4"] Shipments Planned 7 | tbody#fleet-vehicles phx-update="replace" 8 | = for vehicle <- @vehicles do 9 | = live_component @socket, Web.FleetLive.VehicleComponent, id: vehicle.vehicle_id, vehicle: vehicle 10 | -------------------------------------------------------------------------------- /extended_example/lib/web/templates/dashboard/fleet/vehicle.html.slimleex: -------------------------------------------------------------------------------- 1 | tr[id="vehicle-#{@vehicle.vehicle_id}"] 2 | td.cell = @vehicle.vehicle_id 3 | td.cell = status(@vehicle) 4 | td.cell = @vehicle.planned_shipment_count -------------------------------------------------------------------------------- /extended_example/lib/web/templates/dashboard/fulfillment/index.html.slimleex: -------------------------------------------------------------------------------- 1 | table.border-collapse.table-fixed.w-full.max-h-full.whitespace-no-wrap.table-striped.absolute.bg-white 2 | thead 3 | tr.text-left 4 | th.table-header[class="w-1/5"] Shipment ID 5 | th.table-header[class="w-1/5"] Scheduled For 6 | th.table-header[class="w-1/4"] Delivery Info 7 | th.table-header[class="w-1/4"] Delivered 8 | tbody#fulfillment-shipments phx-update="prepend" 9 | = for shipment <- @shipments do 10 | = live_component @socket, Web.FulfillmentLive.ShipmentComponent, id: shipment.shipment_id, shipment: shipment 11 | -------------------------------------------------------------------------------- /extended_example/lib/web/templates/dashboard/fulfillment/shipment.html.slimleex: -------------------------------------------------------------------------------- 1 | tr[id="fulfillment-#{@shipment.shipment_id}"] 2 | td.cell = @shipment.shipment_id 3 | td.cell = time(@shipment.scheduled_for) 4 | td.cell = out_for_delivery_badge(@shipment) 5 | td.cell = delivered_badge(@shipment) -------------------------------------------------------------------------------- /extended_example/lib/web/templates/dashboard/index.html.slimleex: -------------------------------------------------------------------------------- 1 | 2 | .flex.flex-wrap.h-full[class="-mx-5"] 3 | .card 4 | .text-3xl Order Service 5 | .card-table-wrapper 6 | = live_render @conn, Web.OrdersLive 7 | 8 | .card 9 | .text-3xl Fleet Service 10 | .card-table-wrapper 11 | = live_render @conn, Web.FleetLive 12 | 13 | .card 14 | .text-3xl Fulfillment Service 15 | .card-table-wrapper 16 | = live_render @conn, Web.FulfillmentLive 17 | 18 | .card 19 | .text-3xl Track and Trace Service 20 | .card-table-wrapper 21 | = live_render @conn, Web.TrackAndTraceLive 22 | 23 | .text-3xl.pt-3 Analytics Service 24 | .card-table-wrapper 25 | = live_render @conn, Web.AnalyticsLive 26 | -------------------------------------------------------------------------------- /extended_example/lib/web/templates/dashboard/orders.html.slimleex: -------------------------------------------------------------------------------- 1 | table.border-collapse.table-fixed.w-full.max-h-full.whitespace-no-wrap.table-striped.absolute.bg-white 2 | thead 3 | tr.text-left 4 | th.table-header[class="w-1/4"] Shipment ID 5 | th.table-header Destination 6 | tbody#orders-shipments phx-update="prepend" 7 | = for shipment <- @shipments do 8 | tr[id="#{shipment.shipment_id}"] 9 | td.cell = shipment.shipment_id 10 | td.cell = shipment.destination -------------------------------------------------------------------------------- /extended_example/lib/web/templates/dashboard/track_and_trace/index.html.slimleex: -------------------------------------------------------------------------------- 1 | table.border-collapse.table-fixed.w-full.max-h-full.whitespace-no-wrap.table-striped.absolute.bg-white 2 | thead 3 | tr.text-left 4 | th.table-header[class="w-1/5"] Shipment ID 5 | th.table-header[class="w-1/4"] Status 6 | th.table-header[class="w-2/4"] Destination 7 | tbody#track-and-trace-shipments phx-update="prepend" 8 | = for shipment <- @shipments do 9 | = live_component @socket, Web.TrackAndTraceLive.ShipmentComponent, id: shipment.shipment_id, shipment: shipment 10 | -------------------------------------------------------------------------------- /extended_example/lib/web/templates/dashboard/track_and_trace/shipment.html.slimleex: -------------------------------------------------------------------------------- 1 | tr[id="tracked-shipment-#{@shipment.shipment_id}"] 2 | td.cell = @shipment.shipment_id 3 | td.cell = status(@shipment) 4 | td.cell = @shipment.destination -------------------------------------------------------------------------------- /extended_example/lib/web/templates/icon/badge.html.eex: -------------------------------------------------------------------------------- 1 | 2 | <%= icon(@icon, class: "badge-icon mr-2") %><%= @label %> 3 | 4 | -------------------------------------------------------------------------------- /extended_example/lib/web/templates/icon/icon.html.eex: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /extended_example/lib/web/templates/icon/sprite.html.eex: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /extended_example/lib/web/templates/layout/app.html.slimleex: -------------------------------------------------------------------------------- 1 | main.pt-4.h-full.bg-gray-200 role="main" 2 | p.alert.alert-info role="alert" 3 | = get_flash(@conn, :info) 4 | 5 | p.alert.alert-danger role="alert" 6 | = get_flash(@conn, :error) 7 | 8 | .container.mx-auto.h-full 9 | = @inner_content 10 | 11 | -------------------------------------------------------------------------------- /extended_example/lib/web/templates/layout/live.html.slimleex: -------------------------------------------------------------------------------- 1 | main.h-full.bg-white role="main" 2 | p.alert.alert-info role="alert" phx-click="lv:clear-flash" phx-value-key="info" 3 | = live_flash(@flash, :info) 4 | 5 | p.alert.alert-danger role="alert" phx-click="lv:clear-flash" phx-value-key="error" 6 | = live_flash(@flash, :error) 7 | 8 | .container.mx-auto.h-full 9 | = @inner_content 10 | -------------------------------------------------------------------------------- /extended_example/lib/web/templates/layout/root.html.slimleex: -------------------------------------------------------------------------------- 1 | doctype html 2 | html.h-full lang="en" 3 | head 4 | meta charset="utf-8" 5 | meta http-equiv="X-UA-Compatible" content="IE=edge" 6 | meta name="viewport" content="width=device-width, initial-scale=1.0" 7 | = csrf_meta_tag() 8 | title Shippy Demo 9 | link phx-track-static="" rel="stylesheet" href=Routes.static_path(@conn, "/css/app.css") 10 | script defer="" phx-track-static="" type="text/javascript" src=Routes.static_path(@conn, "/js/app.js") 11 | body.h-full 12 | = render Web.IconView, "sprite.html" 13 | .wrapper.flex.flex-col.h-full 14 | header.border-b-2.bg-gray-100 15 | section.container.mx-auto.py-3 16 | p.text-4xl Shippy 17 | p.text-1xl The Shipment System 18 | 19 | = @inner_content 20 | -------------------------------------------------------------------------------- /extended_example/lib/web/views/dashboard_view.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.DashboardView do 2 | use Web, :view 3 | 4 | def status(%Vehicle{out_for_delivery: true}) do 5 | render(Web.IconView, "badge.html", 6 | color: "yellow", 7 | label: "Out for Delivery", 8 | icon: "fa-truck" 9 | ) 10 | end 11 | 12 | def status(%Vehicle{out_for_delivery: false}) do 13 | render(Web.IconView, "badge.html", color: "teal", label: "Packing", icon: "fa-box-open") 14 | end 15 | 16 | def status(%{delivered_successfully: true}) do 17 | render(Web.IconView, "badge.html", color: "green", label: "Delivered", icon: "fa-check") 18 | end 19 | 20 | def status(%{delivered_successfully: false}) do 21 | render(Web.IconView, "badge.html", 22 | color: "red", 23 | label: "Delivery failed", 24 | icon: "fa-times-circle" 25 | ) 26 | end 27 | 28 | def status(%{out_for_delivery: true}) do 29 | render(Web.IconView, "badge.html", 30 | color: "yellow", 31 | label: "Out for Delivery", 32 | icon: "fa-truck" 33 | ) 34 | end 35 | 36 | def status(%{scheduled_for: scheduled_for}) when not is_nil(scheduled_for) do 37 | render(Web.IconView, "badge.html", 38 | color: "teal", 39 | label: "Scheduled", 40 | icon: "fa-clock" 41 | ) 42 | end 43 | 44 | def status(_shipment) do 45 | render(Web.IconView, "badge.html", color: "gray", label: "Registered", icon: "fa-plus") 46 | end 47 | 48 | def out_for_delivery_badge(%{delivered_successfully: true, delivering_vehicle: vehicle_id}) do 49 | render(Web.IconView, "badge.html", 50 | color: "green", 51 | label: "Truck #{vehicle_id}", 52 | icon: "fa-check" 53 | ) 54 | end 55 | 56 | def out_for_delivery_badge(%{delivered_successfully: false, delivering_vehicle: vehicle_id}) do 57 | render(Web.IconView, "badge.html", 58 | color: "red", 59 | label: "Truck #{vehicle_id}", 60 | icon: "fa-times-circle" 61 | ) 62 | end 63 | 64 | def out_for_delivery_badge(%{out_for_delivery: true, delivering_vehicle: vehicle_id}) do 65 | render(Web.IconView, "badge.html", 66 | color: "yellow", 67 | label: "Truck #{vehicle_id}", 68 | icon: "fa-truck" 69 | ) 70 | end 71 | 72 | def out_for_delivery_badge(%{scheduled_for_vehicle: vehicle_id}) when is_binary(vehicle_id) do 73 | render(Web.IconView, "badge.html", 74 | color: "teal", 75 | label: "Truck #{vehicle_id}", 76 | icon: "fa-clock" 77 | ) 78 | end 79 | 80 | def out_for_delivery_badge(_), do: pill(nil) 81 | 82 | def delivered_badge(%{delivered_successfully: true}) do 83 | render(Web.IconView, "badge.html", 84 | color: "green", 85 | label: "Delivered", 86 | icon: "fa-check" 87 | ) 88 | end 89 | 90 | def delivered_badge(%{delivered_successfully: false}) do 91 | render(Web.IconView, "badge.html", 92 | color: "red", 93 | label: "Delivery failed", 94 | icon: "fa-times-circle" 95 | ) 96 | end 97 | 98 | def delivered_badge(%{delivered_successfully: nil}), do: pill(nil) 99 | end 100 | -------------------------------------------------------------------------------- /extended_example/lib/web/views/error_helpers.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.ErrorHelpers do 2 | @moduledoc """ 3 | Conveniences for translating and building error messages. 4 | """ 5 | 6 | use Phoenix.HTML 7 | 8 | @doc """ 9 | Generates tag for inlined form input errors. 10 | """ 11 | def error_tag(form, field) do 12 | Enum.map(Keyword.get_values(form.errors, field), fn error -> 13 | content_tag(:span, error, 14 | class: "invalid-feedback", 15 | phx_feedback_for: input_id(form, field) 16 | ) 17 | end) 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /extended_example/lib/web/views/error_view.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.ErrorView do 2 | use Web, :view 3 | 4 | # If you want to customize a particular status code 5 | # for a certain format, you may uncomment below. 6 | # def render("500.html", _assigns) do 7 | # "Internal Server Error" 8 | # end 9 | 10 | # By default, Phoenix returns the status message from 11 | # the template name. For example, "404.html" becomes 12 | # "Not Found". 13 | def template_not_found(template, _assigns) do 14 | Phoenix.Controller.status_message_from_template(template) 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /extended_example/lib/web/views/icon_view.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.IconView do 2 | use Web, :view 3 | end 4 | -------------------------------------------------------------------------------- /extended_example/lib/web/views/layout_view.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.LayoutView do 2 | use Web, :view 3 | end 4 | -------------------------------------------------------------------------------- /extended_example/lib/web/views/view_helpers.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.ViewHelpers do 2 | def time(nil), do: "" 3 | 4 | def time(time) when is_binary(time) do 5 | time 6 | |> Time.from_iso8601!() 7 | |> time() 8 | end 9 | 10 | def time(%Time{} = time) do 11 | time 12 | |> Time.truncate(:second) 13 | |> Time.to_string() 14 | end 15 | 16 | def icon(name, opts \\ []) do 17 | assigns = [name: name, class: opts[:class] || ""] 18 | Web.IconView.render("icon.html", assigns) 19 | end 20 | 21 | def pill(nil) do 22 | icon("fa-clock", class: "text-gray-400") 23 | end 24 | 25 | def pill(true) do 26 | icon("fa-check-circle", class: "text-green-500") 27 | end 28 | 29 | def pill(false) do 30 | icon("fa-times-circle", class: "text-red-500") 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /extended_example/mix.exs: -------------------------------------------------------------------------------- 1 | defmodule Demo.MixProject do 2 | use Mix.Project 3 | 4 | def project do 5 | [ 6 | app: :demo, 7 | version: "0.1.0", 8 | build_path: "./_build", 9 | config_path: "./config/config.exs", 10 | deps_path: "./deps", 11 | lockfile: "mix.lock", 12 | elixir: "~> 1.7", 13 | elixirc_paths: elixirc_paths(Mix.env()), 14 | compilers: [:phoenix] ++ Mix.compilers(), 15 | start_permanent: Mix.env() == :prod, 16 | aliases: aliases(), 17 | deps: deps() 18 | ] 19 | end 20 | 21 | # Configuration for the OTP application. 22 | # 23 | # Type `mix help compile.app` for more information. 24 | def application do 25 | [ 26 | mod: {Demo.Application, []}, 27 | extra_applications: [:logger, :runtime_tools] 28 | ] 29 | end 30 | 31 | # Specifies which paths to compile per environment. 32 | defp elixirc_paths(:test), do: ["lib", "test/support"] 33 | defp elixirc_paths(_), do: ["lib"] 34 | 35 | # Specifies your project dependencies. 36 | # 37 | # Type `mix help deps` for examples and options. 38 | defp deps do 39 | [ 40 | {:phoenix, "~> 1.5.4"}, 41 | {:phoenix_ecto, "~> 4.1"}, 42 | {:ecto_sql, "~> 3.4"}, 43 | {:postgrex, ">= 0.0.0"}, 44 | {:tzdata, "~> 1.0.1"}, 45 | {:phoenix_live_view, "~> 0.14.4"}, 46 | {:floki, ">= 0.0.0", only: :test}, 47 | {:phoenix_html, "~> 2.11"}, 48 | {:phoenix_live_reload, "~> 1.2", only: :dev}, 49 | {:telemetry_metrics, "~> 0.4"}, 50 | {:telemetry_poller, "~> 0.4"}, 51 | {:jason, "~> 1.0"}, 52 | {:plug_cowboy, "~> 2.0"}, 53 | {:eventstore, "~> 1.1"}, 54 | {:phoenix_slime, "~> 0.13.1"}, 55 | {:faker, "~> 0.15"} 56 | ] 57 | end 58 | 59 | # Aliases are shortcuts or tasks specific to the current project. 60 | # For example, to install project dependencies and perform other setup tasks, run: 61 | # 62 | # $ mix setup 63 | # 64 | # See the documentation for `Mix` for more info on aliases. 65 | defp aliases do 66 | [ 67 | setup: [ 68 | "deps.get", 69 | "ecto.setup", 70 | "cmd npm install --prefix assets" 71 | ], 72 | "ecto.setup": ["ecto.create", "event_store.init", "run priv/repo/seeds.exs"], 73 | "ecto.reset": ["ecto.drop", "ecto.setup"], 74 | test: ["ecto.create --quiet", "event_store.init", "test"] 75 | ] 76 | end 77 | end 78 | -------------------------------------------------------------------------------- /extended_example/mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "certifi": {:hex, :certifi, "2.5.2", "b7cfeae9d2ed395695dd8201c57a2d019c0c43ecaf8b8bcb9320b40d6662f340", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "3b3b5f36493004ac3455966991eaf6e768ce9884693d9968055aeeeb1e575040"}, 3 | "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"}, 4 | "cowboy": {:hex, :cowboy, "2.8.0", "f3dc62e35797ecd9ac1b50db74611193c29815401e53bac9a5c0577bd7bc667d", [:rebar3], [{:cowlib, "~> 2.9.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "4643e4fba74ac96d4d152c75803de6fad0b3fa5df354c71afdd6cbeeb15fac8a"}, 5 | "cowlib": {:hex, :cowlib, "2.9.1", "61a6c7c50cf07fdd24b2f45b89500bb93b6686579b069a89f88cb211e1125c78", [:rebar3], [], "hexpm", "e4175dc240a70d996156160891e1c62238ede1729e45740bdd38064dad476170"}, 6 | "db_connection": {:hex, :db_connection, "2.2.2", "3bbca41b199e1598245b716248964926303b5d4609ff065125ce98bcd368939e", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm", "642af240d8a8affb93b4ba5a6fcd2bbcbdc327e1a524b825d383711536f8070c"}, 7 | "decimal": {:hex, :decimal, "1.9.0", "83e8daf59631d632b171faabafb4a9f4242c514b0a06ba3df493951c08f64d07", [:mix], [], "hexpm", "b1f2343568eed6928f3e751cf2dffde95bfaa19dd95d09e8a9ea92ccfd6f7d85"}, 8 | "ecto": {:hex, :ecto, "3.4.6", "08f7afad3257d6eb8613309af31037e16c36808dfda5a3cd0cb4e9738db030e4", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6f13a9e2a62e75c2dcfc7207bfc65645ab387af8360db4c89fee8b5a4bf3f70b"}, 9 | "ecto_sql": {:hex, :ecto_sql, "3.4.5", "30161f81b167d561a9a2df4329c10ae05ff36eca7ccc84628f2c8b9fa1e43323", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.4.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0 or ~> 0.4.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.0", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "31990c6a3579b36a3c0841d34a94c275e727de8b84f58509da5f1b2032c98ac2"}, 10 | "elixir_uuid": {:hex, :elixir_uuid, "1.2.1", "dce506597acb7e6b0daeaff52ff6a9043f5919a4c3315abb4143f0b00378c097", [:mix], [], "hexpm", "f7eba2ea6c3555cea09706492716b0d87397b88946e6380898c2889d68585752"}, 11 | "eventstore": {:hex, :eventstore, "1.1.0", "1f5f20feb343c78cf8a38e8d288f6c7a860056c341dbe1520e6faa728c5e633f", [:mix], [{:elixir_uuid, "~> 1.2", [hex: :elixir_uuid, repo: "hexpm", optional: false]}, {:fsm, "~> 0.3", [hex: :fsm, repo: "hexpm", optional: false]}, {:gen_stage, "~> 1.0", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:highlander, "~> 0.2", [hex: :highlander, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm", "0b61fa4972a1521efa9027c3fd3955afac2df396783357ab2295c9d424124046"}, 12 | "faker": {:hex, :faker, "0.15.0", "7b91646b97aef21f4b514367ce95a177c9871fcf301336b33e931d2519343bce", [:mix], [], "hexpm", "73ce103e4dca83a147198bdf40d78b5840be520c7bd15ee5b59b48550654b932"}, 13 | "file_system": {:hex, :file_system, "0.2.8", "f632bd287927a1eed2b718f22af727c5aeaccc9a98d8c2bd7bff709e851dc986", [:mix], [], "hexpm", "97a3b6f8d63ef53bd0113070102db2ce05352ecf0d25390eb8d747c2bde98bca"}, 14 | "floki": {:hex, :floki, "0.28.0", "0d0795a17189510ee01323e6990f906309e9fc6e8570219135211f1264d78c7f", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "db1549560874ebba5a6367e46c3aec5fedd41f2757ad6efe567efb04b4d4ee55"}, 15 | "fsm": {:hex, :fsm, "0.3.1", "087aa9b02779a84320dc7a2d8464452b5308e29877921b2bde81cdba32a12390", [:mix], [], "hexpm", "fbf0d53f89e9082b326b0b5828b94b4c549ff9d1452bbfd00b4d1ac082208e96"}, 16 | "gen_stage": {:hex, :gen_stage, "1.0.0", "51c8ae56ff54f9a2a604ca583798c210ad245f415115453b773b621c49776df5", [:mix], [], "hexpm", "1d9fc978db5305ac54e6f5fec7adf80cd893b1000cf78271564c516aa2af7706"}, 17 | "hackney": {:hex, :hackney, "1.16.0", "5096ac8e823e3a441477b2d187e30dd3fff1a82991a806b2003845ce72ce2d84", [:rebar3], [{:certifi, "2.5.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.0", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.6", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "3bf0bebbd5d3092a3543b783bf065165fa5d3ad4b899b836810e513064134e18"}, 18 | "highlander": {:hex, :highlander, "0.2.1", "e59b459f857e89daf73f2598bf2b2c0479a435481e6101ea389fd3625919b052", [:mix], [], "hexpm", "5ba19a18358803d82a923511acec8ee85fac30731c5ca056f2f934bc3d3afd9a"}, 19 | "html_entities": {:hex, :html_entities, "0.5.1", "1c9715058b42c35a2ab65edc5b36d0ea66dd083767bef6e3edb57870ef556549", [:mix], [], "hexpm", "30efab070904eb897ff05cd52fa61c1025d7f8ef3a9ca250bc4e6513d16c32de"}, 20 | "idna": {:hex, :idna, "6.0.1", "1d038fb2e7668ce41fbf681d2c45902e52b3cb9e9c77b55334353b222c2ee50c", [:rebar3], [{:unicode_util_compat, "0.5.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a02c8a1c4fd601215bb0b0324c8a6986749f807ce35f25449ec9e69758708122"}, 21 | "jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"}, 22 | "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, 23 | "mime": {:hex, :mime, "1.4.0", "5066f14944b470286146047d2f73518cf5cca82f8e4815cf35d196b58cf07c47", [:mix], [], "hexpm", "75fa42c4228ea9a23f70f123c74ba7cece6a03b1fd474fe13f6a7a85c6ea4ff6"}, 24 | "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, 25 | "neotoma": {:hex, :neotoma, "1.7.3", "d8bd5404b73273989946e4f4f6d529e5c2088f5fa1ca790b4dbe81f4be408e61", [:rebar], [], "hexpm", "2da322b9b1567ffa0706a7f30f6bbbde70835ae44a1050615f4b4a3d436e0f28"}, 26 | "parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"}, 27 | "phoenix": {:hex, :phoenix, "1.5.4", "0fca9ce7e960f9498d6315e41fcd0c80bfa6fbeb5fa3255b830c67fdfb7e703f", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.13", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.1.2 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4e516d131fde87b568abd62e1b14aa07ba7d5edfd230bab4e25cc9dedbb39135"}, 28 | "phoenix_ecto": {:hex, :phoenix_ecto, "4.2.0", "4ac3300a22240a37ed54dfe6c0be1b5623304385d1a2c210a70f011d9e7af7ac", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 2.15", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "59e7e2a550d7ea082a665c0fc29485f06f55d1a51dd02f513aafdb9d16fc72c4"}, 29 | "phoenix_html": {:hex, :phoenix_html, "2.14.2", "b8a3899a72050f3f48a36430da507dd99caf0ac2d06c77529b1646964f3d563e", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "58061c8dfd25da5df1ea0ca47c972f161beb6c875cd293917045b92ffe1bf617"}, 30 | "phoenix_live_dashboard": {:hex, :phoenix_live_dashboard, "0.2.6", "1b4e1b7d797386b7f9d70d2af931dc9843a5f2f2423609d22cef1eec4e4dba7d", [:mix], [{:phoenix_html, "~> 2.14.1 or ~> 2.15", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.13.1", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.4.0 or ~> 0.5.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "b20dcad98c4ca63d38a7f5e7a40936e1e8e9da983d3d722b88ae33afb866c9ca"}, 31 | "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.2.4", "940c0344b1d66a2e46eef02af3a70e0c5bb45a4db0bf47917add271b76cd3914", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "38f9308357dea4cc77f247e216da99fcb0224e05ada1469167520bed4cb8cccd"}, 32 | "phoenix_live_view": {:hex, :phoenix_live_view, "0.14.4", "7286a96287cd29b594ce4a7314249cea7311af04a06c0fa3e50932e188e73996", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.5.3", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 0.5", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "fc4f8cf205c784eeccee35de8afbfeb995ce5511ac4839db63d6d67a5ba091d1"}, 33 | "phoenix_pubsub": {:hex, :phoenix_pubsub, "2.0.0", "a1ae76717bb168cdeb10ec9d92d1480fec99e3080f011402c0a2d68d47395ffb", [:mix], [], "hexpm", "c52d948c4f261577b9c6fa804be91884b381a7f8f18450c5045975435350f771"}, 34 | "phoenix_slime": {:hex, :phoenix_slime, "0.13.1", "a5d4d8febb87a618b02d690519f7106832c8bd0b4d1937fbba73d6e8666f2891", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.13", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:slime, "~> 1.0", [hex: :slime, repo: "hexpm", optional: false]}], "hexpm", "ff818744be2c903fb0174ba22b230c1c335238578fccf274b1d95d08f4844377"}, 35 | "plug": {:hex, :plug, "1.10.4", "41eba7d1a2d671faaf531fa867645bd5a3dce0957d8e2a3f398ccff7d2ef017f", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ad1e233fe73d2eec56616568d260777b67f53148a999dc2d048f4eb9778fe4a0"}, 36 | "plug_cowboy": {:hex, :plug_cowboy, "2.3.0", "149a50e05cb73c12aad6506a371cd75750c0b19a32f81866e1a323dda9e0e99d", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "bc595a1870cef13f9c1e03df56d96804db7f702175e4ccacdb8fc75c02a7b97e"}, 37 | "plug_crypto": {:hex, :plug_crypto, "1.1.2", "bdd187572cc26dbd95b87136290425f2b580a116d3fb1f564216918c9730d227", [:mix], [], "hexpm", "6b8b608f895b6ffcfad49c37c7883e8df98ae19c6a28113b02aa1e9c5b22d6b5"}, 38 | "postgrex": {:hex, :postgrex, "0.15.5", "aec40306a622d459b01bff890fa42f1430dac61593b122754144ad9033a2152f", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "ed90c81e1525f65a2ba2279dbcebf030d6d13328daa2f8088b9661eb9143af7f"}, 39 | "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"}, 40 | "slime": {:hex, :slime, "1.2.1", "71e036056051f0a6fae136af34eaa1322e8e11cdd2da3a56196fd31bca34dd49", [:mix], [{:neotoma, "~> 1.7", [hex: :neotoma, repo: "hexpm", optional: false]}], "hexpm", "298568e64291fed4eb690be094f6c46400daa03b594bab34fcaa0167e139c263"}, 41 | "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, 42 | "telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"}, 43 | "telemetry_metrics": {:hex, :telemetry_metrics, "0.5.0", "1b796e74add83abf844e808564275dfb342bcc930b04c7577ab780e262b0d998", [:mix], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "31225e6ce7a37a421a0a96ec55244386aec1c190b22578bd245188a4a33298fd"}, 44 | "telemetry_poller": {:hex, :telemetry_poller, "0.5.1", "21071cc2e536810bac5628b935521ff3e28f0303e770951158c73eaaa01e962a", [:rebar3], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4cab72069210bc6e7a080cec9afffad1b33370149ed5d379b81c7c5f0c663fd4"}, 45 | "tzdata": {:hex, :tzdata, "1.0.3", "73470ad29dde46e350c60a66e6b360d3b99d2d18b74c4c349dbebbc27a09a3eb", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a6e1ee7003c4d04ecbd21dd3ec690d4c6662db5d3bbdd7262d53cdf5e7c746c1"}, 46 | "unicode_util_compat": {:hex, :unicode_util_compat, "0.5.0", "8516502659002cec19e244ebd90d312183064be95025a319a6c7e89f4bccd65b", [:rebar3], [], "hexpm", "d48d002e15f5cc105a696cf2f1bbb3fc72b4b770a184d8420c8db20da2674b38"}, 47 | } 48 | -------------------------------------------------------------------------------- /extended_example/priv/repo/migrations/.formatter.exs: -------------------------------------------------------------------------------- 1 | [ 2 | import_deps: [:ecto_sql], 3 | inputs: ["*.exs"] 4 | ] 5 | -------------------------------------------------------------------------------- /extended_example/priv/repo/seeds.exs: -------------------------------------------------------------------------------- 1 | # Script for populating the database. You can run it as: 2 | # 3 | # mix run priv/repo/seeds.exs 4 | # 5 | # Inside the script, you can read and write to any of your 6 | # repositories directly: 7 | # 8 | # Demo.Repo.insert!(%Demo.SomeSchema{}) 9 | # 10 | # We recommend using the bang functions (`insert!`, `update!` 11 | # and so on) as they will fail if something goes wrong. 12 | -------------------------------------------------------------------------------- /extended_example/test/event_store/event_consumer_test.exs: -------------------------------------------------------------------------------- 1 | defmodule EventStore.EventConsumerTest do 2 | use Support.TestCase 3 | 4 | defmodule ExampleEvent do 5 | defstruct [:key] 6 | end 7 | 8 | defmodule AnotherExampleEvent do 9 | defstruct [:key] 10 | end 11 | 12 | defmodule TestEventConsumer do 13 | use Shared.EventConsumer, 14 | initial_state: %{events: []} 15 | 16 | def handle(%ExampleEvent{} = _event_data, %{events: events} = state, event) do 17 | {:ok, %{state | events: [event | events]}} 18 | end 19 | end 20 | 21 | describe "EventConsumer" do 22 | test "subscribes to an event stream" do 23 | {:ok, subscriber} = TestEventConsumer.start_link() 24 | 25 | event = %ExampleEvent{key: "test"} 26 | 27 | :ok = Shared.EventPublisher.publish("foo", event, %{user: "someuser@example.com"}) 28 | 29 | wait_until(fn -> 30 | assert %{events: [event]} = TestEventConsumer.get_state(subscriber) 31 | assert event.event_type == ExampleEvent |> to_string() 32 | assert event.data == %ExampleEvent{key: "test"} 33 | assert event.event_id 34 | assert event.metadata == %{"user" => "someuser@example.com"} 35 | assert event.stream_uuid == "foo" 36 | end) 37 | end 38 | 39 | test "ignores events for which no handle/3 function was defined" do 40 | {:ok, subscriber} = TestEventConsumer.start_link() 41 | 42 | event = %ExampleEvent{key: "test"} 43 | not_handled_event = %AnotherExampleEvent{key: "test"} 44 | 45 | :ok = Shared.EventPublisher.publish("foo1", event, %{user: "someuser@example.com"}) 46 | 47 | :ok = 48 | Shared.EventPublisher.publish("foo1", not_handled_event, %{user: "someuser@example.com"}) 49 | 50 | wait_until(fn -> 51 | assert %{events: [event]} = TestEventConsumer.get_state(subscriber) 52 | end) 53 | end 54 | 55 | test "can be paused" do 56 | {:ok, subscriber} = TestEventConsumer.start_link() 57 | event = %ExampleEvent{key: "first event"} 58 | :ok = Shared.EventPublisher.publish("foo2", event, %{user: "someuser@example.com"}) 59 | 60 | wait_until(fn -> 61 | assert %{events: [event]} = TestEventConsumer.get_state(subscriber) 62 | end) 63 | 64 | :ok = TestEventConsumer.pause(subscriber) 65 | 66 | event = %ExampleEvent{key: "first event"} 67 | :ok = Shared.EventPublisher.publish("foo2", event, %{user: "someuser@example.com"}) 68 | 69 | wait_until(fn -> 70 | assert %{events: [event]} = TestEventConsumer.get_state(subscriber) 71 | end) 72 | 73 | :ok = TestEventConsumer.resume(subscriber) 74 | 75 | wait_until(fn -> 76 | assert %{events: [second_event, first_event]} = TestEventConsumer.get_state(subscriber) 77 | end) 78 | end 79 | end 80 | end 81 | -------------------------------------------------------------------------------- /extended_example/test/support/concurrency_helper.ex: -------------------------------------------------------------------------------- 1 | defmodule Support.ConcurrencyHelper do 2 | def wait_until(fun), do: wait_until(500, fun) 3 | 4 | def wait_until(0, fun), do: fun.() 5 | 6 | def wait_until(timeout, fun) do 7 | try do 8 | fun.() 9 | rescue 10 | ExUnit.AssertionError -> 11 | :timer.sleep(100) 12 | wait_until(max(0, timeout - 100), fun) 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /extended_example/test/support/conn_case.ex: -------------------------------------------------------------------------------- 1 | defmodule Web.ConnCase do 2 | @moduledoc """ 3 | This module defines the test case to be used by 4 | tests that require setting up a connection. 5 | 6 | Such tests rely on `Phoenix.ConnTest` and also 7 | import other functionality to make it easier 8 | to build common data structures and query the data layer. 9 | 10 | Finally, if the test case interacts with the database, 11 | we enable the SQL sandbox, so changes done to the database 12 | are reverted at the end of every test. If you are using 13 | PostgreSQL, you can even run database tests asynchronously 14 | by setting `use Web.ConnCase, async: true`, although 15 | this option is not recommended for other databases. 16 | """ 17 | 18 | use ExUnit.CaseTemplate 19 | 20 | using do 21 | quote do 22 | # Import conveniences for testing with connections 23 | import Plug.Conn 24 | import Phoenix.ConnTest 25 | import Web.ConnCase 26 | 27 | alias Web.Router.Helpers, as: Routes 28 | 29 | # The default endpoint for testing 30 | @endpoint Web.Endpoint 31 | end 32 | end 33 | 34 | setup tags do 35 | :ok = Ecto.Adapters.SQL.Sandbox.checkout(EventStore.Repo) 36 | 37 | unless tags[:async] do 38 | Ecto.Adapters.SQL.Sandbox.mode(EventStore.Repo, {:shared, self()}) 39 | end 40 | 41 | {:ok, conn: Phoenix.ConnTest.build_conn()} 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /extended_example/test/support/data_case.ex: -------------------------------------------------------------------------------- 1 | defmodule Demo.DataCase do 2 | @moduledoc """ 3 | This module defines the setup for tests requiring 4 | access to the application's data layer. 5 | 6 | You may define functions here to be used as helpers in 7 | your tests. 8 | 9 | Finally, if the test case interacts with the database, 10 | we enable the SQL sandbox, so changes done to the database 11 | are reverted at the end of every test. If you are using 12 | PostgreSQL, you can even run database tests asynchronously 13 | by setting `use Demo.DataCase, async: true`, although 14 | this option is not recommended for other databases. 15 | """ 16 | 17 | use ExUnit.CaseTemplate 18 | 19 | using do 20 | quote do 21 | alias Demo.Repo 22 | 23 | import Ecto 24 | import Ecto.Changeset 25 | import Ecto.Query 26 | import Demo.DataCase 27 | end 28 | end 29 | 30 | setup tags do 31 | :ok = Ecto.Adapters.SQL.Sandbox.checkout(EventStore.Repo) 32 | 33 | unless tags[:async] do 34 | Ecto.Adapters.SQL.Sandbox.mode(EventStore.Repo, {:shared, self()}) 35 | end 36 | 37 | {:ok, eventstore_connection} = 38 | Shared.EventStore 39 | |> EventStore.Config.parsed(:demo) 40 | |> EventStore.Config.default_postgrex_opts() 41 | |> Postgrex.start_link() 42 | 43 | EventStore.Storage.Initializer.reset!(eventstore_connection) 44 | {:ok, _} = Application.ensure_all_started(:eventstore) 45 | start_supervised!(Shared.EventStore) 46 | 47 | on_exit(fn -> 48 | Application.stop(:eventstore) 49 | Process.exit(eventstore_connection, :shutdown) 50 | end) 51 | 52 | :ok 53 | end 54 | 55 | @doc """ 56 | A helper that transforms changeset errors into a map of messages. 57 | 58 | assert {:error, changeset} = Accounts.create_user(%{password: "short"}) 59 | assert "password is too short" in errors_on(changeset).password 60 | assert %{password: ["password is too short"]} = errors_on(changeset) 61 | 62 | """ 63 | def errors_on(changeset) do 64 | Ecto.Changeset.traverse_errors(changeset, fn {message, opts} -> 65 | Regex.replace(~r"%{(\w+)}", message, fn _, key -> 66 | opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string() 67 | end) 68 | end) 69 | end 70 | end 71 | -------------------------------------------------------------------------------- /extended_example/test/support/test_case.ex: -------------------------------------------------------------------------------- 1 | defmodule Support.TestCase do 2 | @moduledoc """ 3 | This module defines the setup for tests requiring 4 | access to the application's data layer. 5 | 6 | You may define functions here to be used as helpers in 7 | your tests. 8 | 9 | Finally, if the test case interacts with the database, 10 | we enable the SQL sandbox, so changes done to the database 11 | are reverted at the end of every test. If you are using 12 | PostgreSQL, you can even run database tests asynchronously 13 | by setting `use Arbeitszeitbestaetigung.DataCase, async: true`, although 14 | this option is not recommended for other databases. 15 | """ 16 | 17 | use ExUnit.CaseTemplate 18 | 19 | using do 20 | quote do 21 | import Support.ConcurrencyHelper 22 | end 23 | end 24 | 25 | setup tags do 26 | :ok = Ecto.Adapters.SQL.Sandbox.checkout(EventStore.Repo) 27 | 28 | unless tags[:async] do 29 | Ecto.Adapters.SQL.Sandbox.mode(EventStore.Repo, {:shared, self()}) 30 | end 31 | 32 | {:ok, eventstore_connection} = 33 | Shared.EventStore 34 | |> EventStore.Config.parsed(:demo) 35 | |> EventStore.Config.default_postgrex_opts() 36 | |> Postgrex.start_link() 37 | 38 | EventStore.Storage.Initializer.reset!(eventstore_connection) 39 | {:ok, _} = Application.ensure_all_started(:eventstore) 40 | start_supervised!(Shared.EventStore) 41 | 42 | on_exit(fn -> 43 | Application.stop(:eventstore) 44 | Process.exit(eventstore_connection, :shutdown) 45 | end) 46 | 47 | :ok 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /extended_example/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | Ecto.Adapters.SQL.Sandbox.mode(EventStore.Repo, :manual) 3 | -------------------------------------------------------------------------------- /extended_example/test/web/views/error_view_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Web.ErrorViewTest do 2 | use Web.ConnCase, async: true 3 | 4 | # Bring render/3 and render_to_string/3 for testing custom views 5 | import Phoenix.View 6 | 7 | test "renders 404.html" do 8 | assert render_to_string(Web.ErrorView, "404.html", []) == "Not Found" 9 | end 10 | 11 | test "renders 500.html" do 12 | assert render_to_string(Web.ErrorView, "500.html", []) == "Internal Server Error" 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /extended_example/test/web/views/layout_view_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Web.LayoutViewTest do 2 | use Web.ConnCase, async: true 3 | 4 | # When testing helpers, you may want to import Phoenix.HTML and 5 | # use functions such as safe_to_string() to convert the helper 6 | # result into an HTML string. 7 | # import Phoenix.HTML 8 | end 9 | -------------------------------------------------------------------------------- /slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PJUllrich/event-sourcing-with-elixir/7f70e6bc49d9d93f1d86513a1f358e41e07b8304/slides.pdf --------------------------------------------------------------------------------