|
| 1 | +####################################### |
| 2 | +# Development Server for ErrorTracker. |
| 3 | +# |
| 4 | +# Based on PhoenixLiveDashboard code. |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# |
| 8 | +# $ iex -S mix dev |
| 9 | +####################################### |
| 10 | +Logger.configure(level: :debug) |
| 11 | + |
| 12 | +# Get configuration |
| 13 | +Config.Reader.read!("config/config.exs", env: :dev) |
| 14 | + |
| 15 | +# Prepare the repo |
| 16 | +adapter = |
| 17 | + case Application.get_env(:error_tracker, :ecto_adapter) do |
| 18 | + :postgres -> Ecto.Adapters.Postgres |
| 19 | + :mysql -> Ecto.Adapters.MyXQL |
| 20 | + :sqlite3 -> Ecto.Adapters.SQLite3 |
| 21 | + end |
| 22 | + |
| 23 | +defmodule ErrorTrackerDev.Repo do |
| 24 | + use Ecto.Repo, otp_app: :error_tracker, adapter: adapter |
| 25 | +end |
| 26 | + |
| 27 | +_ = adapter.storage_up(ErrorTrackerDev.Repo.config()) |
| 28 | + |
| 29 | +# Configures the endpoint |
| 30 | +Application.put_env(:error_tracker, ErrorTrackerDevWeb.Endpoint, |
| 31 | + url: [host: "localhost"], |
| 32 | + secret_key_base: "Hu4qQN3iKzTV4fJxhorPQlA/osH9fAMtbtjVS58PFgfw3ja5Z18Q/WSNR9wP4OfW", |
| 33 | + live_view: [signing_salt: "hMegieSe"], |
| 34 | + http: [port: System.get_env("PORT") || 4000], |
| 35 | + debug_errors: true, |
| 36 | + check_origin: false, |
| 37 | + pubsub_server: ErrorTrackerDev.PubSub, |
| 38 | + watchers: [ |
| 39 | + tailwind: {Tailwind, :install_and_run, [:default, ~w(--watch)]} |
| 40 | + ], |
| 41 | + live_reload: [ |
| 42 | + patterns: [ |
| 43 | + ~r"dev.exs$", |
| 44 | + ~r"dist/.*(js|css|png|jpeg|jpg|gif|svg)$", |
| 45 | + ~r"lib/error_tracker/web/(live|views)/.*(ex)$", |
| 46 | + ~r"lib/error_tracker/web/templates/.*(ex)$" |
| 47 | + ] |
| 48 | + ] |
| 49 | +) |
| 50 | + |
| 51 | +# Setup up the ErrorTracker configuration |
| 52 | +Application.put_env(:error_tracker, :repo, ErrorTrackerDev.Repo) |
| 53 | +Application.put_env(:error_tracker, :otp_app, :error_tracker_dev) |
| 54 | +Application.put_env(:error_tracker, :prefix, "private") |
| 55 | + |
| 56 | +defmodule ErrorTrackerDevWeb.PageController do |
| 57 | + import Plug.Conn |
| 58 | + |
| 59 | + def init(opts), do: opts |
| 60 | + |
| 61 | + def call(conn, :index) do |
| 62 | + content(conn, """ |
| 63 | + <h2>ErrorTracker Dev Server</h2> |
| 64 | + <div><a href="/dev/errors">Open ErrorTracker</a></div> |
| 65 | + <div><a href="/plug-exception">Generate Plug exception</a></div> |
| 66 | + <div><a href="/404">Generate Router 404</a></div> |
| 67 | + <div><a href="/noroute">Raise NoRouteError from a controller</a></div> |
| 68 | + <div><a href="/exception">Generate Exception</a></div> |
| 69 | + <div><a href="/exit">Generate Exit</a></div> |
| 70 | + """) |
| 71 | + end |
| 72 | + |
| 73 | + def call(conn, :noroute) do |
| 74 | + ErrorTracker.add_breadcrumb("ErrorTrackerDevWeb.PageController.no_route") |
| 75 | + raise Phoenix.Router.NoRouteError, conn: conn, router: ErrorTrackerDevWeb.Router |
| 76 | + end |
| 77 | + |
| 78 | + def call(_conn, :exception) do |
| 79 | + ErrorTracker.add_breadcrumb("ErrorTrackerDevWeb.PageController.exception") |
| 80 | + |
| 81 | + raise CustomException, |
| 82 | + message: "This is a controller exception", |
| 83 | + bread_crumbs: ["First", "Second"] |
| 84 | + end |
| 85 | + |
| 86 | + def call(_conn, :exit) do |
| 87 | + ErrorTracker.add_breadcrumb("ErrorTrackerDevWeb.PageController.exit") |
| 88 | + exit(:timeout) |
| 89 | + end |
| 90 | + |
| 91 | + defp content(conn, content) do |
| 92 | + conn |
| 93 | + |> put_resp_header("content-type", "text/html") |
| 94 | + |> send_resp(200, "<!doctype html><html><body>#{content}</body></html>") |
| 95 | + end |
| 96 | +end |
| 97 | + |
| 98 | +defmodule CustomException do |
| 99 | + defexception [:message, :bread_crumbs] |
| 100 | +end |
| 101 | + |
| 102 | +defmodule ErrorTrackerDevWeb.ErrorView do |
| 103 | + def render("404.html", _assigns) do |
| 104 | + "This is a 404" |
| 105 | + end |
| 106 | + |
| 107 | + def render("500.html", _assigns) do |
| 108 | + "This is a 500" |
| 109 | + end |
| 110 | +end |
| 111 | + |
| 112 | +defmodule ErrorTrackerDevWeb.Router do |
| 113 | + use Phoenix.Router |
| 114 | + use ErrorTracker.Web, :router |
| 115 | + |
| 116 | + pipeline :browser do |
| 117 | + plug :fetch_session |
| 118 | + plug :protect_from_forgery |
| 119 | + end |
| 120 | + |
| 121 | + scope "/" do |
| 122 | + pipe_through :browser |
| 123 | + get "/", ErrorTrackerDevWeb.PageController, :index |
| 124 | + get "/noroute", ErrorTrackerDevWeb.PageController, :noroute |
| 125 | + get "/exception", ErrorTrackerDevWeb.PageController, :exception |
| 126 | + get "/exit", ErrorTrackerDevWeb.PageController, :exit |
| 127 | + |
| 128 | + scope "/dev" do |
| 129 | + error_tracker_dashboard "/errors", csp_nonce_assign_key: :my_csp_nonce |
| 130 | + end |
| 131 | + end |
| 132 | +end |
| 133 | + |
| 134 | +defmodule ErrorTrackerDevWeb.Endpoint do |
| 135 | + use Phoenix.Endpoint, otp_app: :error_tracker |
| 136 | + use ErrorTracker.Integrations.Plug |
| 137 | + |
| 138 | + @session_options [ |
| 139 | + store: :cookie, |
| 140 | + key: "_error_tracker_dev", |
| 141 | + signing_salt: "/VEDsdfsffMnp5", |
| 142 | + same_site: "Lax" |
| 143 | + ] |
| 144 | + |
| 145 | + socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]] |
| 146 | + socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket |
| 147 | + |
| 148 | + plug Phoenix.LiveReloader |
| 149 | + plug Phoenix.CodeReloader |
| 150 | + |
| 151 | + plug Plug.Session, @session_options |
| 152 | + |
| 153 | + plug Plug.RequestId |
| 154 | + plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint] |
| 155 | + plug :add_breadcrumb |
| 156 | + plug :maybe_exception |
| 157 | + plug :set_csp |
| 158 | + plug ErrorTrackerDevWeb.Router |
| 159 | + |
| 160 | + def add_breadcrumb(conn, _) do |
| 161 | + ErrorTracker.add_breadcrumb("ErrorTrackerDevWeb.Endpoint.add_breadcrumb") |
| 162 | + conn |
| 163 | + end |
| 164 | + |
| 165 | + def maybe_exception(%Plug.Conn{path_info: ["plug-exception"]}, _), do: raise("Plug exception") |
| 166 | + def maybe_exception(conn, _), do: conn |
| 167 | + |
| 168 | + defp set_csp(conn, _opts) do |
| 169 | + nonce = 10 |> :crypto.strong_rand_bytes() |> Base.encode64() |
| 170 | + |
| 171 | + policies = [ |
| 172 | + "script-src 'self' 'nonce-#{nonce}';", |
| 173 | + "style-src 'self' 'nonce-#{nonce}';" |
| 174 | + ] |
| 175 | + |
| 176 | + conn |
| 177 | + |> Plug.Conn.assign(:my_csp_nonce, "#{nonce}") |
| 178 | + |> Plug.Conn.put_resp_header("content-security-policy", Enum.join(policies, " ")) |
| 179 | + end |
| 180 | +end |
| 181 | + |
| 182 | +defmodule ErrorTrackerDev.Telemetry do |
| 183 | + require Logger |
| 184 | + |
| 185 | + def start do |
| 186 | + :telemetry.attach_many( |
| 187 | + "error-tracker-events", |
| 188 | + [ |
| 189 | + [:error_tracker, :error, :new], |
| 190 | + [:error_tracker, :error, :resolved], |
| 191 | + [:error_tracker, :error, :unresolved], |
| 192 | + [:error_tracker, :occurrence, :new] |
| 193 | + ], |
| 194 | + &__MODULE__.handle_event/4, |
| 195 | + [] |
| 196 | + ) |
| 197 | + |
| 198 | + Logger.info("Telemtry attached") |
| 199 | + end |
| 200 | + |
| 201 | + def handle_event(event, measure, metadata, _opts) do |
| 202 | + dbg([event, measure, metadata]) |
| 203 | + end |
| 204 | +end |
| 205 | + |
| 206 | +Application.put_env(:phoenix, :serve_endpoints, true) |
| 207 | + |
| 208 | +Task.async(fn -> |
| 209 | + children = [ |
| 210 | + {Phoenix.PubSub, [name: ErrorTrackerDev.PubSub, adapter: Phoenix.PubSub.PG2]}, |
| 211 | + ErrorTrackerDev.Repo, |
| 212 | + ErrorTrackerDevWeb.Endpoint |
| 213 | + ] |
| 214 | + |
| 215 | + ErrorTrackerDev.Telemetry.start() |
| 216 | + |
| 217 | + {:ok, _} = Supervisor.start_link(children, strategy: :one_for_one) |
| 218 | + |
| 219 | + # Automatically run the migrations on boot |
| 220 | + Ecto.Migrator.run(ErrorTrackerDev.Repo, :up, all: true, log_migrations_sql: :debug) |
| 221 | + |
| 222 | + Process.sleep(:infinity) |
| 223 | +end) |
0 commit comments