Skip to content

Instantly share code, notes, and snippets.

View Wigny's full-sized avatar

Wígny Almeida Wigny

View GitHub Profile
Application.put_env(:example, Example.Endpoint,
server: true,
http: [ip: {127, 0, 0, 1}, port: 4001],
adapter: Bandit.PhoenixAdapter,
render_errors: [formats: [html: Example.ErrorHTML], layout: false],
live_view: [signing_salt: "aaaaaaaa"],
secret_key_base: String.duplicate("a", 64)
)
Application.put_env(:ex_money, :default_cldr_backend, Example.Cldr)
defmodule Decimal.Range do
defstruct [:first, :last, :step]
def new(first, last, step) do
%__MODULE__{first: first, last: last, step: step}
end
def size(range) do
range.last
|> Decimal.sub(range.first)
defmodule MyApp.Type.Duration do
use Ecto.Type
alias Postgrex.Interval
defguardp is_duration(value) when is_struct(value, Duration)
defguardp is_interval(value) when is_struct(value, Interval)
@impl true
def type, do: :interval
@Wigny
Wigny / duration.exs
Last active August 29, 2023 00:21
Ecto Duration ParameterizedType
Mix.install([
{:ecto_sql, "~> 3.10"},
{:timex, "~> 3.7"}
])
defmodule MyApp.Type.Duration do
use Ecto.ParameterizedType
alias Timex.Duration
defmodule DefMaybe do
defmacro defmaybe(fun) do
quote bind_quoted: [fun: Macro.escape(fun, unquote: true)], location: :keep do
{fun_name, fun_args} = Macro.decompose_call(fun)
resource = List.first(fun_args)
action = Keyword.get(opts, :action)
def unquote(:"maybe_#{fun_name}")({:ok, unquote_splicing(fun_args)}) do
{:ok, unquote(fun_name)(unquote_splicing(fun_args))}
end
defmodule DefBang do
defmacro def!(call, do: block) do
quote bind_quoted: [call: Macro.escape(call, unquote: true), block: Macro.escape(block)], location: :keep do
{name, args} = Macro.decompose_call(call)
def unquote(name)(unquote_splicing(args)) do
unquote(block)
end
def unquote(:"#{name}!")(unquote_splicing(args)) do
Mix.install([evision: "~> 0.1"],
system_env: [
EVISION_PREFER_PRECOMPILED: "false",
CMAKE_OPENCV_OPTIONS: "-D WITH_FFMPEG=ON"
]
)
alias Evision.VideoCapture
url = "rtsp://172.28.6.108:8554/mystream"
docker run -d --restart unless-stopped --name parking_lot_database -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=parking_lot -p 5432:5432 -v postgres:/var/lib/postgresql/data postgres
echo 'DATABASE_URL=ecto://postgres:[email protected]:5432/parking_lot' >> ~/parking_lot.env
echo 'SECRET_KEY_BASE=my_ramdom_secure_and_at_least_64_bits_long_secret_______________' >> ~/parking_lot.env
echo 'PHX_HOST=127.0.0.1' >> ~/parking_lot.env
echo 'CACHE_DIR=/data' >> ~/parking_lot.env
docker pull ghcr.io/wigny/parking_lot:latest
docker run --env-file ~/parking_lot.env -it ghcr.io/wigny/parking_lot bin/migrate
docker run --name parking_lot --restart unless-stopped --env-file ~/parking_lot.env -p 4000:4000 -v /tmp:/data -itd ghcr.io/wigny/parking_lot
@Wigny
Wigny / uploader.ex
Last active May 17, 2023 17:31
Phoenix external upload to GCS
Application.put_env(:uploader, Uploader.Endpoint,
http: [ip: {127, 0, 0, 1}, port: 4001],
server: true,
render_errors: [
formats: [html: Uploader.ErrorHTML],
layout: false
],
live_view: [signing_salt: "aaaaaaaa"],
secret_key_base: String.duplicate("a", 64)
)
defmodule Example.Authorization do
defprotocol Store do
def allowed?(resource, actor, action)
end
defmacro defauthorized(fun, opts) do
quote bind_quoted: [fun: Macro.escape(fun, unquote: true), opts: opts], location: :keep do
{fun_name, fun_args} = Macro.decompose_call(fun)
resource = List.first(fun_args)
action = Keyword.get(opts, :action)