From ef7351ccb81bca1ee4db2581fe836c35178ba438 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cristian=20=C3=81lvarez=20Belaustegui?=
Date: Tue, 17 Dec 2024 18:24:02 +0100
Subject: [PATCH] Update and unify dev scripts (#119)
This pull request replaces the previous `live.exs` and `dev.exs` scripts
with a new `dev.exs` script based on the Phoenix Playground project
that includes both the Controller and LiveView examples.
There are multiple benefits of this refactor:
- We can work using a single script instead of two
- Phoenix Plaground provides autoreload functionality by default
- Phoenix Playground simplifies process management and can start a
supervision tree
- Dependencies are automatically fetched and compiled separately from
those in the `mix.exs` file.
To start the dev server you just run:
```
iex dev.exs
```
---
README.md | 6 +-
dev.exs | 372 +++++++++++++++++++++++++++++++++---------------------
live.exs | 153 ----------------------
3 files changed, 227 insertions(+), 304 deletions(-)
delete mode 100644 live.exs
diff --git a/README.md b/README.md
index 98b1de1..5e79930 100644
--- a/README.md
+++ b/README.md
@@ -62,10 +62,8 @@ mix assets.watch
### Development server
-We have a `dev.exs` script that starts a development server.
-
-To run it together with an `IEx` console you can do:
+We have a `dev.exs` script based on [Phoenix Playground](https://github.com/phoenix-playground/phoenix_playground) that starts a development server.
```
-iex -S mix dev
+iex dev.exs
```
diff --git a/dev.exs b/dev.exs
index 895749d..a09f496 100644
--- a/dev.exs
+++ b/dev.exs
@@ -1,169 +1,217 @@
-#######################################
-# Development Server for ErrorTracker.
+# This is the development server for Errortracker built on the PhoenixLiveDashboard project.
+# To start the development server run:
+# $ iex dev.exs
#
-# Based on PhoenixLiveDashboard code.
-#
-# Usage:
-#
-# $ iex -S mix dev
-#######################################
-Logger.configure(level: :debug)
+Mix.install([
+ {:ecto_sqlite3, ">= 0.0.0"},
+ {:error_tracker, path: "."},
+ {:phoenix_playground, "~> 0.1.7"}
+])
-# Get configuration
-Config.Reader.read!("config/config.exs", env: :dev)
+otp_app = :error_tracker_dev
-# Prepare the repo
-adapter =
- case Application.get_env(:error_tracker, :ecto_adapter) do
- :postgres -> Ecto.Adapters.Postgres
- :mysql -> Ecto.Adapters.MyXQL
- :sqlite3 -> Ecto.Adapters.SQLite3
- end
+Application.put_all_env(
+ error_tracker_dev: [
+ {ErrorTrackerDev.Repo, [database: "priv/repo/dev.db"]}
+ ],
+ error_tracker: [
+ {:application, otp_app},
+ {:otp_app, otp_app},
+ {:repo, ErrorTrackerDev.Repo}
+ ]
+)
defmodule ErrorTrackerDev.Repo do
- use Ecto.Repo, otp_app: :error_tracker, adapter: adapter
+ require Logger
+ use Ecto.Repo, otp_app: otp_app, adapter: Ecto.Adapters.SQLite3
+
+ defmodule Migration do
+ use Ecto.Migration
+
+ def up, do: ErrorTracker.Migration.up()
+ def down, do: ErrorTracker.Migration.down()
+ end
+
+ def migrate do
+ Ecto.Migrator.run(__MODULE__, [{0, __MODULE__.Migration}], :up, all: true)
+ end
end
-_ = adapter.storage_up(ErrorTrackerDev.Repo.config())
-
-# Configures the endpoint
-Application.put_env(:error_tracker, ErrorTrackerDevWeb.Endpoint,
- url: [host: "localhost"],
- secret_key_base: "Hu4qQN3iKzTV4fJxhorPQlA/osH9fAMtbtjVS58PFgfw3ja5Z18Q/WSNR9wP4OfW",
- live_view: [signing_salt: "hMegieSe"],
- http: [port: System.get_env("PORT") || 4000],
- debug_errors: true,
- check_origin: false,
- pubsub_server: ErrorTrackerDev.PubSub,
- watchers: [
- tailwind: {Tailwind, :install_and_run, [:default, ~w(--watch)]}
- ],
- live_reload: [
- patterns: [
- ~r"dev.exs$",
- ~r"dist/.*(js|css|png|jpeg|jpg|gif|svg)$",
- ~r"lib/error_tracker/web/(live|views)/.*(ex)$",
- ~r"lib/error_tracker/web/templates/.*(ex)$"
- ]
- ]
-)
+defmodule ErrorTrackerDev.Controller do
+ use Phoenix.Controller, formats: [:html]
+ use Phoenix.Component
-# Setup up the ErrorTracker configuration
-Application.put_env(:error_tracker, :repo, ErrorTrackerDev.Repo)
-Application.put_env(:error_tracker, :otp_app, :error_tracker_dev)
-Application.put_env(:error_tracker, :prefix, "private")
+ plug :put_layout, false
+ plug :put_view, __MODULE__
-defmodule ErrorTrackerDevWeb.PageController do
- import Plug.Conn
+ def index(conn, _params) do
+ render(conn)
+ end
- def init(opts), do: opts
+ def index(assigns) do
+ ~H"""
+
+ """
end
- def call(conn, :noroute) do
- ErrorTracker.add_breadcrumb("ErrorTrackerDevWeb.PageController.no_route")
- raise Phoenix.Router.NoRouteError, conn: conn, router: ErrorTrackerDevWeb.Router
+ def noroute(conn, _params) do
+ ErrorTracker.add_breadcrumb("ErrorTrackerDev.Controller.noroute/2")
+
+ raise Phoenix.Router.NoRouteError, conn: conn, router: ErrorTrackerDev.Router
end
- def call(_conn, :exception) do
- ErrorTracker.add_breadcrumb("ErrorTrackerDevWeb.PageController.exception")
+ def exception(_conn, _params) do
+ ErrorTracker.add_breadcrumb("ErrorTrackerDev.Controller.exception/2")
- raise CustomException,
+ raise ErrorTrackerDev.Exception,
message: "This is a controller exception",
bread_crumbs: ["First", "Second"]
end
- def call(_conn, :exit) do
- ErrorTracker.add_breadcrumb("ErrorTrackerDevWeb.PageController.exit")
+ def exit(_conn, _params) do
+ ErrorTracker.add_breadcrumb("ErrorTrackerDev.Controller.exit/2")
+
exit(:timeout)
end
+end
- defp content(conn, content) do
- conn
- |> put_resp_header("content-type", "text/html")
- |> send_resp(200, "#{content}")
+defmodule ErrorTrackerDev.Live do
+ use Phoenix.LiveView
+
+ def mount(params, _session, socket) do
+ if params["crash_on_mount"] do
+ raise("Crashed on mount/3")
+ end
+
+ {:ok, socket}
end
-end
-defmodule CustomException do
- defexception [:message, :bread_crumbs]
-end
+ def handle_params(params, _uri, socket) do
+ if params["crash_on_handle_params"] do
+ raise "Crashed on handle_params/3"
+ end
-defmodule ErrorTrackerDevWeb.ErrorView do
- def render("404.html", _assigns) do
- "This is a 404"
+ {:noreply, socket}
end
- def render("500.html", _assigns) do
- "This is a 500"
+ def handle_event("crash_on_handle_event", _params, _socket) do
+ raise "Crashed on handle_event/3"
+ end
+
+ def handle_event("crash_on_render", _params, socket) do
+ {:noreply, assign(socket, crash_on_render: true)}
+ end
+
+ def handle_event("genserver-timeout", _params, socket) do
+ GenServer.call(ErrorTrackerDev.GenServer, :timeout, 2000)
+ {:noreply, socket}
+ end
+
+ def render(assigns) do
+ if Map.has_key?(assigns, :crash_on_render) do
+ raise "Crashed on render/1"
+ end
+
+ ~H"""
+
ErrorTracker Dev server
+
+ <.link href="/dev/errors" target="_blank">Open the ErrorTracker dashboard
+
+
+ Errors are stored in the priv/repo/dev.db
+ database, which is automatically created by this script.
+ If you want to clear the state stop the script, run the following command and start it again.