This commit is contained in:
Haim Kortovich
2026-03-05 11:35:01 -05:00
commit 072dbf6e66
43 changed files with 1400 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
defmodule CustomerServiceWeb.ApiSpec do
alias OpenApiSpex.{OpenApi, Info, Server}
alias OpenApiSpex.{Info, OpenApi, Paths, Server}
alias CustomerServiceWeb.{Endpoint, Router}
@behaviour OpenApi
@impl OpenApi
def spec do
%OpenApi{
servers: [
# Populate the Server info from a phoenix endpoint
Server.from_endpoint(Endpoint)
],
info: %Info{
title: "Customer Service",
version: "1.0"
},
# Populate the paths from a phoenix router
paths: Paths.from_router(Router)
}
# Discover request/response schemas from path specs
|> OpenApiSpex.resolve_schema_modules()
end
end

View File

@@ -0,0 +1,84 @@
defmodule CustomerServiceWeb.Customer do
use CustomerServiceWeb, :controller
alias CustomerServiceWeb.Schemas.CreateCustomerRequest
alias CustomerServiceWeb.Schemas.CustomerResponse
alias CustomerService.Commands.CreateCustomer
alias CustomerService.CommandedApp
use OpenApiSpex.ControllerSpecs
tags ["Customers"]
operation :create,
summary: "Create customer",
request_body: {"Customer data", "application/json", CreateCustomerRequest},
responses: [
ok: {"Customer created", "application/json", CustomerResponse}
]
def create(conn, params) do
customer_id = Ecto.UUID.generate()
command =
%CreateCustomer{
id: customer_id,
first_name: params["first_name"],
last_name: params["last_name"],
birth_date: Date.from_iso8601!(params["birth_date"]),
gender: params["gender"],
email: params["email"],
phone: params["phone"]
}
case CommandedApp.dispatch(command, consistency: :strong) do
:ok ->
json(conn, %{id: customer_id})
{:error, reason} ->
conn
|> put_status(:unprocessable_entity)
|> json(%{error: inspect(reason)})
end
end
operation :show,
summary: "Get customer",
parameters: [
id: [in: :path, type: :string, description: "Customer ID"]
],
responses: [
ok: {"Customer", "application/json", CustomerResponse},
not_found: {"Not found", "application/json", nil}
]
def show(conn, %{"id" => id}) do
case CustomerService.Repo.get(CustomerService.Projections.Customer, id) do
nil ->
send_resp(conn, 404, "")
customer ->
json(conn, customer)
end
end
operation :index,
summary: "List customers",
responses: [
ok:
{"Customer list", "application/json",
%OpenApiSpex.Schema{
type: :array,
items: CustomerResponse
}}
]
def index(conn, _) do
case CustomerService.Repo.all(CustomerService.Projections.Customer) do
nil ->
send_resp(conn, 404, "")
customer ->
json(conn, customer)
end
end
end

View File

@@ -0,0 +1,21 @@
defmodule CustomerServiceWeb.ErrorJSON do
@moduledoc """
This module is invoked by your endpoint in case of errors on JSON requests.
See config/config.exs.
"""
# If you want to customize a particular status code,
# you may add your own clauses, such as:
#
# def render("500.json", _assigns) do
# %{errors: %{detail: "Internal Server Error"}}
# end
# By default, Phoenix returns the status message from
# the template name. For example, "404.json" becomes
# "Not Found".
def render(template, _assigns) do
%{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}}
end
end

View File

@@ -0,0 +1,49 @@
defmodule CustomerServiceWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :customer_service
# The session will be stored in the cookie and signed,
# this means its contents can be read but not tampered with.
# Set :encryption_salt if you would also like to encrypt it.
@session_options [
store: :cookie,
key: "_customer_service_key",
signing_salt: "2Ese9Ehj",
same_site: "Lax"
]
# socket "/live", Phoenix.LiveView.Socket,
# websocket: [connect_info: [session: @session_options]],
# longpoll: [connect_info: [session: @session_options]]
# Serve at "/" the static files from "priv/static" directory.
#
# When code reloading is disabled (e.g., in production),
# the `gzip` option is enabled to serve compressed
# static files generated by running `phx.digest`.
plug Plug.Static,
at: "/",
from: :customer_service,
gzip: not code_reloading?,
only: CustomerServiceWeb.static_paths(),
raise_on_missing_only: code_reloading?
# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
if code_reloading? do
plug Phoenix.CodeReloader
plug Phoenix.Ecto.CheckRepoStatus, otp_app: :customer_service
end
plug Plug.RequestId
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Phoenix.json_library()
plug Plug.MethodOverride
plug Plug.Head
plug Plug.Session, @session_options
plug CustomerServiceWeb.Router
end

View File

@@ -0,0 +1,15 @@
defmodule CustomerServiceWeb.Router do
use CustomerServiceWeb, :router
pipeline :api do
plug CORSPlug, origin: "*"
plug OpenApiSpex.Plug.PutApiSpec, module: CustomerServiceWeb.ApiSpec
end
scope "/api" do
pipe_through :api
resources "/customers", CustomerServiceWeb.Customer, only: [:create, :index, :show]
get "/openapi", OpenApiSpex.Plug.RenderSpec, []
end
end

View File

@@ -0,0 +1,38 @@
defmodule CustomerServiceWeb.Schemas do
defmodule CustomerResponse do
require OpenApiSpex
alias OpenApiSpex.Schema
OpenApiSpex.schema(%{
title: "Customer",
type: :object,
properties: %{
id: %Schema{type: :string, format: :uuid},
first_name: %Schema{type: :string},
last_name: %Schema{type: :string},
birth_date: %Schema{type: :string, format: :date},
gender: %Schema{type: :string},
email: %Schema{type: :string, format: :email},
phone: %Schema{type: :string}
}
})
end
defmodule CreateCustomerRequest do
require OpenApiSpex
alias OpenApiSpex.Schema
OpenApiSpex.schema(%{
title: "CreateCustomer",
type: :object,
properties: %{
first_name: %Schema{type: :string},
last_name: %Schema{type: :string},
birth_date: %Schema{type: :string, format: :date},
gender: %Schema{type: :string},
email: %Schema{type: :string, format: :email},
phone: %Schema{type: :string}
}
})
end
end

View File

@@ -0,0 +1,93 @@
defmodule CustomerServiceWeb.Telemetry do
use Supervisor
import Telemetry.Metrics
def start_link(arg) do
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
end
@impl true
def init(_arg) do
children = [
# Telemetry poller will execute the given period measurements
# every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics
{:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
# Add reporters as children of your supervision tree.
# {Telemetry.Metrics.ConsoleReporter, metrics: metrics()}
]
Supervisor.init(children, strategy: :one_for_one)
end
def metrics do
[
# Phoenix Metrics
summary("phoenix.endpoint.start.system_time",
unit: {:native, :millisecond}
),
summary("phoenix.endpoint.stop.duration",
unit: {:native, :millisecond}
),
summary("phoenix.router_dispatch.start.system_time",
tags: [:route],
unit: {:native, :millisecond}
),
summary("phoenix.router_dispatch.exception.duration",
tags: [:route],
unit: {:native, :millisecond}
),
summary("phoenix.router_dispatch.stop.duration",
tags: [:route],
unit: {:native, :millisecond}
),
summary("phoenix.socket_connected.duration",
unit: {:native, :millisecond}
),
sum("phoenix.socket_drain.count"),
summary("phoenix.channel_joined.duration",
unit: {:native, :millisecond}
),
summary("phoenix.channel_handled_in.duration",
tags: [:event],
unit: {:native, :millisecond}
),
# Database Metrics
summary("customer_service.repo.query.total_time",
unit: {:native, :millisecond},
description: "The sum of the other measurements"
),
summary("customer_service.repo.query.decode_time",
unit: {:native, :millisecond},
description: "The time spent decoding the data received from the database"
),
summary("customer_service.repo.query.query_time",
unit: {:native, :millisecond},
description: "The time spent executing the query"
),
summary("customer_service.repo.query.queue_time",
unit: {:native, :millisecond},
description: "The time spent waiting for a database connection"
),
summary("customer_service.repo.query.idle_time",
unit: {:native, :millisecond},
description:
"The time the connection spent waiting before being checked out for the query"
),
# VM Metrics
summary("vm.memory.total", unit: {:byte, :kilobyte}),
summary("vm.total_run_queue_lengths.total"),
summary("vm.total_run_queue_lengths.cpu"),
summary("vm.total_run_queue_lengths.io")
]
end
defp periodic_measurements do
[
# A module, function and arguments to be invoked periodically.
# This function must call :telemetry.execute/3 and a metric must be added above.
# {CustomerServiceWeb, :count_users, []}
]
end
end