Skip to content

Instantly share code, notes, and snippets.

View AndrewDryga's full-sized avatar

Andrew Dryga AndrewDryga

View GitHub Profile
@AndrewDryga
AndrewDryga / release_tasks.ex
Last active July 30, 2018 08:29
Elixir Release Tasks
defmodule MyAppAPI.ReleaseTasks do
alias Ecto.Migrator
@otp_app :myapp_api
@start_apps [:logger, :ssl, :postgrex, :ecto]
def migrate do
init(@otp_app, @start_apps)
run_migrations_for(@otp_app)
@AndrewDryga
AndrewDryga / changeset.ex
Created August 19, 2017 14:03
on_load Module callback example (before)
defmodule MyAppAPI.Changeset do
import Ecto.Changeset
@known_buckets Application.get_env(:myapp_api, :known_buckets)
# ...
def changeset(schema, attrs) do
schema
|> cast(attrs, @fields)
@AndrewDryga
AndrewDryga / changeset.ex
Last active August 24, 2017 12:56
on_load Module callback example
defmodule MyAppAPI.Changeset do
import Ecto.Changeset
@on_load :load_buckets
# ...
def load_buckets do
known_buckets = "KNOWN_BUCKETS" |> System.get_env() |> String.split(",", trim: true)
Application.put_env(:myapp_api, :known_buckets, known_buckets)
@AndrewDryga
AndrewDryga / endpoint.ex
Created August 19, 2017 13:59
System typles example for Phoenix
defmodule MyAppAPI.Endpoint do
use Phoenix.Endpoint, otp_app: :myapp_api
# ...
@doc """
Callback invoked for dynamically configuring the endpoint.
It receives the endpoint configuration and checks if
configuration should be loaded from the system environment.
"""
@AndrewDryga
AndrewDryga / repo.ex
Last active August 22, 2017 10:17
Ecto init callback example
defmodule MyAppAPI.Repo do
use Ecto.Repo, otp_app: :myapp_api
def init(_type, config) do
url = System.get_env("DATABASE_URL")
if url, do: {:ok, [url: url] ++ config}, else: {:ok, config}
end
end
@AndrewDryga
AndrewDryga / config.exs
Created August 19, 2017 13:56
REPLACE_OS_VARS example
config :myapp_api, :third_party_lib,
cloud_name: "${CLOUDINARY_CLOUD_NAME}",
bucket_name: "${CLOUDINARY_BUCKET_NAME}",
bucket_region: "${CLOUDINARY_BUCKET_REGION}",
frontend_url: "${FRONTEND_URL}"
@AndrewDryga
AndrewDryga / rel_config.exs
Last active August 19, 2017 13:55
Configuration and Deployment article examples: rel/config.ex
use Mix.Releases.Config,
# This sets the default release built by `mix release`
default_release: :default,
# This sets the default environment used by `mix release`
default_environment: :default
environment :default do
# Copy files into release instead of creating symlink for them
set dev_mode: false
# Do not include ERTS
@AndrewDryga
AndrewDryga / README.md
Created August 14, 2017 18:03
Elixir Refactoring Best Practice
  1. Revise folder sturcture and align everything without touching module names. Make sure everything is singular. Think about contexts. Don't forget about tests.
  2. Rename modules to match their file system path
  3. Refactor domain APIs to return {:ok, error} tuples, remove all error formatting from it
  4. Introduce fallback controller
  5. Go over all modules again and make sure naming is correct
  6. Remove all Elixir 1.5 and other warnings
  7. Bump deps and repeat
@AndrewDryga
AndrewDryga / ael_simplified.ex
Last active August 1, 2017 19:44
AEL: Simplified code for context that is responsible for Signing URL's
defmodule Ael.Secrets.API do
def get_secret_url(secret) do
%Secret{
action: action,
expires_at: expires_at,
bucket: bucket,
resource_id: resource_id,
resource_name: resource_name
} = secret
@AndrewDryga
AndrewDryga / keybase.md
Created March 22, 2017 19:08
keybase.md

Keybase proof

I hereby claim:

  • I am andrewdryga on github.
  • I am andrewdryga (https://keybase.io/andrewdryga) on keybase.
  • I have a public key ASCO83B78ctJpdof-fS5nIL6Gdvlenz5NAZvWbbgbKmhJAo

To claim this, I am signing this object: